SourceChord

C#とXAML好きなプログラマの備忘録。最近はWPF系の話題が中心です。

Windows Phoneでデバッグ用にデバイス情報表示ページを作る

WP7用のアプリを作っていて、メモリ使用量とかをいつでも表示できるように、デバイス情報を表示するページを作りました。
他のアプリを作るときにも再利用しそうなので、クラスライブラリのdllにしてみました。


デバイス情報表示用のdll作成

  1. 新規プロジェクトで、DeviceStatusPageという名前の「Windows Phone クラスライブラリ」を作成
  2. 追加⇒新しい項目で、「Windows Phone 縦向きのページ」を追加
  3. xamlとコードビハインドを以下のように修正
DeviceStatusPage.xaml
<phone:PhoneApplicationPage 
    x:Class="DeviceStatusPage.DeviceStatusPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True"
    Language="ja-JP">

    <!--LayoutRoot は、すべてのページ コンテンツが配置されるルート グリッドです-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel は、アプリケーション名とページ タイトルを格納します-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="マイ アプリケーション" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="デバイス情報" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - 追加コンテンツをここに入力します-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <ListBox Name="lstDeviceInfo">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,2">
                            <TextBlock Text="{Binding InfoType}" TextWrapping="NoWrap" Margin="12,0,0,0" Style="{StaticResource PhoneTextLargeStyle}"/>
                            <TextBlock Text="{Binding Value}" TextWrapping="NoWrap" Margin="12,0,0,0" Style="{StaticResource PhoneTextAccentStyle}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </Grid>
 
    <!--ApplicationBar の使用法を示すサンプル コード-->
    <!--<phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
                <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>-->

</phone:PhoneApplicationPage>
DeviceStatusPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace DeviceStatusPage
{
    public class DeviceInfo
    {
        public string InfoType { get; set; }
        public string Value { get; set; }

        public DeviceInfo(string type, string v)
        {
            InfoType = type;
            Value = v;
        }
    }

    public partial class DeviceStatusPage : PhoneApplicationPage
    {
        public DeviceStatusPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            List<DeviceInfo> info = new List<DeviceInfo>();

            //  ここで、様々なデータを取得
            info.Add(new DeviceInfo("デバイス名", Microsoft.Phone.Info.DeviceStatus.DeviceName));
            info.Add(new DeviceInfo("製造元", Microsoft.Phone.Info.DeviceStatus.DeviceManufacturer));
            info.Add(new DeviceInfo("デバイスの総メモリ量", Microsoft.Phone.Info.DeviceStatus.DeviceTotalMemory.ToString() + "byte"));
            info.Add(new DeviceInfo("アプリの使用メモリ量", Microsoft.Phone.Info.DeviceStatus.ApplicationCurrentMemoryUsage.ToString() + "byte"));
            info.Add(new DeviceInfo("アプリの最大メモリ使用量", Microsoft.Phone.Info.DeviceStatus.ApplicationPeakMemoryUsage.ToString() + "byte"));
            info.Add(new DeviceInfo("割り当て可能メモリ最大量", Microsoft.Phone.Info.DeviceStatus.ApplicationMemoryUsageLimit.ToString() + "byte"));

            //  リストボックスに設定
            this.lstDeviceInfo.ItemsSource = info;
        }
    }
}

dllを使用する。

  1. デバイス情報を表示したいアプリのソリューションに、DeviceStatusPageのプロジェクトを追加
  2. 参照の追加で、プロジェクトタブを選択し、DeviceStatusPageを追加
  3. デバイス情報を表示したいタイミングで、以下のコードを実行
NavigationService.Navigate(new Uri("/DeviceStatusPage;component/DeviceStatusPage.xaml", UriKind.Relative));