ソース掲示板




すべてから検索

キーワード   条件 表示 現行ログ 過去ログ トピックス 名前 本文
Windows Phone バインド <= JSON
日時: 2012/10/02 14:58
名前: lightbox



{
    "CheckBoxSetting": true,
    "ListBoxSetting": 2,
    "ListBoxContent": ["Times New Roman", "Arial", "Comic Sans MS"],
    "RadioButton1Setting": false,
    "RadioButton2Setting": true,
    "RadioButton3Setting": false
}
<phone:PhoneApplicationPage 
	x:Class="PhoneApp7.MainPage"
	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"
	xmlns:local="clr-namespace:PhoneApp7"
	mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
	FontFamily="{StaticResource PhoneFontFamilyNormal}"
	FontSize="{StaticResource PhoneFontSizeNormal}"
	Foreground="{StaticResource PhoneForegroundBrush}"
	SupportedOrientations="Portrait" Orientation="Portrait"
	shell:SystemTray.IsVisible="True">

	<!--<phone:PhoneApplicationPage.Resources>
		<local:Class2 x:Key="class1" CheckBoxSetting="false"></local:Class2>
	</phone:PhoneApplicationPage.Resources>-->

	<!--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">
			<Grid.DataContext>
				<local:Class2/>
			</Grid.DataContext>

			<CheckBox
				Content="CheckBox Setting"
				Height="Auto"
				HorizontalAlignment="Left"
				Margin="60,20,0,0"
				Name="checkBoxSetting"
				VerticalAlignment="Top"
				IsChecked="{
					Binding
					CheckBoxSetting,
					Mode=TwoWay
				}"/>

			<ListBox
				Height="140" 
				HorizontalAlignment="Left" 
				Margin="70,150,0,0" 
				Name="listBoxSetting" 
				VerticalAlignment="Top"
				Width="360"
				SelectedIndex="{Binding Path=ListBoxSetting, Mode=TwoWay}">
				<ListBoxItem
					Content="{Binding ListBoxContent[0]}"
					FontSize="24" 
					FontFamily="Times New Roman" />
				<ListBoxItem
					Content="{Binding ListBoxContent[1]}"
					FontSize="24"
					FontFamily="Arial" />
				<ListBoxItem 
					Content="{Binding ListBoxContent[2]}"
					FontSize="24"
					FontFamily="Comic Sans MS" />
			</ListBox>

            <RadioButton
				Content="Choice One"
				Height="Auto"
				HorizontalAlignment="Left"
				Margin="60,320,0,0"
				Name="radioButton1"
				VerticalAlignment="Top"
				GroupName="GroupOne"
				IsChecked="{
					Binding
					Path=RadioButton1Setting,
					Mode=TwoWay
				}" />
            <RadioButton
				Content="Choice Two"
				Height="Auto"
				HorizontalAlignment="Left"
				Margin="60,380,0,0"
				Name="radioButton2"
				VerticalAlignment="Top"
				GroupName="GroupOne"
				IsChecked="{
					Binding
					Path=RadioButton2Setting,
					Mode=TwoWay
				}"/>
            <RadioButton
				Content="Choice Three"
				Height="Auto"
				HorizontalAlignment="Left"
				Margin="60,440,0,0"
				Name="radioButton3"
				VerticalAlignment="Top"
				GroupName="GroupOne"
				IsChecked="{
					Binding
					Path=RadioButton3Setting,
					Mode=TwoWay
				}"/>

        </Grid>
	</Grid>

</phone:PhoneApplicationPage>
メンテナンス

JsonData.cs ( No.1 )
日時: 2012/10/02 14:54
名前: lightbox


日時: 2012/10/02 14:54
名前: lightbox
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace PhoneApp7
{
	public class JsonData
	{
		public JsonData()
		{
			// 3つ入る場所を確保(中は null)
			this.ListBoxContent = new string[3];
		}
		public bool CheckBoxSetting { get; set; }
		public int ListBoxSetting { get; set; }
		public string[] ListBoxContent { get; set; }

		public bool RadioButton1Setting { get; set; }
		public bool RadioButton2Setting { get; set; }
		public bool RadioButton3Setting { get; set; }
		
	}
}
このアーティクルの参照用URLをクリップボードにコピー メンテナンス
Class2.cs ( No.2 )
日時: 2012/10/02 14:55
名前: lightbox
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Diagnostics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace PhoneApp7
{
	public class Class2 : Class1
	{
		public int ListBoxSetting { get; set; }
		public string[] ListBoxContent { get; set; }
		public bool RadioButton1Setting { get; set; }
		public bool RadioButton2Setting { get; set; }
		public bool RadioButton3Setting { get; set; }

		public Class2()
		{
			// 3つ入る場所を確保(中は null)
			ListBoxContent = new string[3];

		}

		protected override void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
		{
			// オーバーライド元を実行
			base.webClient_DownloadStringCompleted(sender, e);

			if (e.Error != null)
			{
				Deployment.Current.Dispatcher.BeginInvoke(() =>
				{
					Debug.WriteLine(e.Error.Message);
				});
			}
			else
			{

				// デシリアライズ
				JsonData jsonObject = JsonConvert.DeserializeObject<JsonData>(e.Result);

				// プロパティにセット
				this.CheckBoxSetting = jsonObject.CheckBoxSetting;
				this.ListBoxSetting = jsonObject.ListBoxSetting;

				this.ListBoxContent[0] = jsonObject.ListBoxContent[0];
				this.ListBoxContent[1] = jsonObject.ListBoxContent[1];
				this.ListBoxContent[2] = jsonObject.ListBoxContent[2];

				this.RadioButton1Setting = jsonObject.RadioButton1Setting;
				this.RadioButton2Setting = jsonObject.RadioButton2Setting;
				this.RadioButton3Setting = jsonObject.RadioButton3Setting;

				// 全てセットした事を通知
				NotifyPropertyChanged(String.Empty);

			}
		}
	}
}
このアーティクルの参照用URLをクリップボードにコピー メンテナンス
Class1.cs ( No.3 )
日時: 2012/10/02 14:56
名前: lightbox
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Diagnostics;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace PhoneApp7
{
	public class Class1 : INotifyPropertyChanged
	{

		// チェックボックス用プロパティ
		// ※ TwoWay でセットされる様子をチェックする為にコードを記述しています
		private bool _CheckBoxSetting;
		public bool CheckBoxSetting {
			get {
				return _CheckBoxSetting;
			}
			set {
				_CheckBoxSetting = value;
				Debug.WriteLine("CheckBoxSetting にデータがセットされました" + value);
			} 
		}

		public Class1()
		{
			WebClient webClient = new WebClient();

			webClient.DownloadStringCompleted += 
				new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
			// 外部サービスから文字列を取得
			// {"CheckBoxSetting": true, "ListBoxSetting": 2}
			webClient.DownloadStringAsync(new System.Uri("http://textt.net/sworc/20120929111044.txt"));
		}

		protected virtual void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
		{
			if (e.Error != null)
			{
				Deployment.Current.Dispatcher.BeginInvoke(() =>
				{
					Debug.WriteLine(e.Error.Message);
				});
			}
			else
			{
				Debug.WriteLine("オーバーライド元を実行");

			}
		}

		public event PropertyChangedEventHandler PropertyChanged;
		public void NotifyPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this,
					new PropertyChangedEventArgs(propertyName));
			}
		}
	}

}
このアーティクルの参照用URLをクリップボードにコピー メンテナンス