ソース掲示板




すべてから検索

キーワード   条件 表示 現行ログ 過去ログ トピックス 名前 本文
Windows8(C#) ストアアプリ : StoreBlank テンプレート
日時: 2013/05/10 15:38
名前: lightbox



SkyDrive

以下のソースコードは、テンプレートをもとに、『S130510_LBOX_StoreBlank』でプロジェクトを作成しています
MainPage.xaml
拡張子:
<Page
	x:Class="S130510_LBOX_StoreBlank.MainPage"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:local="using:S130510_LBOX_StoreBlank"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
	mc:Ignorable="d"
	Loaded="Page_Loaded">
	
	<Page.Resources>
		<local:TextResource
			x:Key="GlobalText"
			PageTitle="ストアブランク テンプレート"
			AppName="lightbox サンプルアプリケーション" />
	</Page.Resources>

	<!--AppBar の定義-->
	<Page.BottomAppBar>
		<AppBar>
			<Grid x:Name="AppBarGrid">
				<Grid.ColumnDefinitions>
					<ColumnDefinition/>
					<ColumnDefinition/>
				</Grid.ColumnDefinitions>
				
				<StackPanel
					x:Name="LeftBar"
					Orientation="Horizontal"
					HorizontalAlignment="Left"
					>

					<!--StandardStyles に定義されたスタイルをそのまま適用する-->
					
					<!--設定ボタン-->
					<Button
						x:Name="SettingButton"
						AutomationProperties.Name="設定"
						HorizontalAlignment="Stretch"
						VerticalAlignment="Stretch"
						Style="{StaticResource SettingsAppBarButtonStyle}"
						Click="SettingButton_Click"
						/>

					<!--お気に入りボタン-->
					<Button
						x:Name="FavoriteButton"
						AutomationProperties.Name="お気に入り"
						HorizontalAlignment="Stretch"
						VerticalAlignment="Stretch"
						Style="{StaticResource FavoriteAppBarButtonStyle}"
						Click="FavoriteButton_Click"
						/>

				</StackPanel>
				
				<StackPanel x:Name="RightBar"
					Grid.Column="1"
					HorizontalAlignment="Right"
					Orientation="Horizontal"
					>

					<!-- 保存ボタンを定義時に、表示文字列を変更する-->
					<Button
						x:Name="SaveButton"
						AutomationProperties.Name="保存"
						Grid.Column="1"
						HorizontalAlignment="Stretch"
						VerticalAlignment="Stretch"
						Style="{StaticResource SaveAppBarButtonStyle}"
						Click="SaveButton_Click"
						IsEnabled="False"
						/>
					
					<!--メールボタン-->
					<Button
						x:Name="MailButton"
						AutomationProperties.Name="メール"
						Grid.Column="1"
						HorizontalAlignment="Stretch"
						VerticalAlignment="Stretch"
						Style="{StaticResource MailAppBarButtonStyle}"
						Click="MailButton_Click" 
						/>

				</StackPanel>
			</Grid>
		</AppBar>
	</Page.BottomAppBar>

	<!--画面定義-->
	<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
		<Border BorderBrush="#FF999999" BorderThickness="3" HorizontalAlignment="Left" Height="546" Margin="76,126,0,0" VerticalAlignment="Top" Width="1122" Background="#FF323232" CornerRadius="20">
			<TextBox
				x:Name="TextData" 
				HorizontalAlignment="Left" 
				Height="209"
				Margin="36,37,0,0"
				TextWrapping="Wrap" 
				VerticalAlignment="Top"
				Width="580" 
				AcceptsReturn="True"/>
		</Border>
		<TextBlock
			DataContext="{StaticResource GlobalText}"
			HorizontalAlignment="Left"
			Height="61"
			Margin="76,60,0,0"
			TextWrapping="Wrap"
			Text="{Binding PageTitle}"
			VerticalAlignment="Top"
			Width="1090"
			FontSize="40"
			FontWeight="Bold"
			FontFamily="Meiryo"
			>
		</TextBlock>

	</Grid>
	
</Page>
メンテナンス

MainPage.xaml.cs ( No.1 )
日時: 2013/05/10 15:54
名前: lightbox


日時: 2013/05/10 15:54
名前: lightbox
拡張子:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Automation;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

//名前空間
namespace S130510_LBOX_StoreBlank
{
	//クラス
	public sealed partial class MainPage : Page
	{
		// ファイルが存在するかどうかを示すフラグ
		private bool bExist = false;

		//コンストラクタ
		public MainPage()
		{
			this.InitializeComponent();
			Debug.WriteLine("MainPage のコンストラクタが実行されました");
		}

		// *************************************************
		// ページのロード
		// *************************************************
		private async void Page_Loaded(object sender, RoutedEventArgs e)
		{
			Debug.WriteLine("ページがロードされました");

			// 対象フォルダ( マニフェストで使用宣言が必要 )
			// ※ 対象ファイルの存在チェック
			StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
			try
			{
				StorageFile strageFile = await storageFolder.GetFileAsync("text.txt");
				bExist = true;
			}
			catch (Exception ex)
			{
				Debug.WriteLine(ex.Message);
			}
			if (bExist)
			{
				Debug.WriteLine("対象ファイルは存在します");
			}
			else
			{
				Debug.WriteLine("対象ファイルは存在しません");
			}

			// XAML で使用不可にしているので、保存ボタンを使用可能にします
			SaveButton.IsEnabled = true;

		}

		// *************************************************
		// このページがフレームに表示されるときに呼び出されます。
		// *************************************************
		protected override void OnNavigatedTo(NavigationEventArgs e)
		{
			Debug.WriteLine("ページがフレームに表示されました");

			// Navigate が実行された時のパラメータを表示しています
			var mp = (App.MyParam)e.Parameter;
			Debug.WriteLine("Title : " + mp.title);
			Debug.WriteLine("Type : " + mp.type);
		}

		// *************************************************
		//設定ボタン
		// *************************************************
		private void SettingButton_Click(object sender, RoutedEventArgs e)
		{

		}

		// *************************************************
		//削除ボタン 
		// *************************************************
		private void DeleteButton_Click(object sender, RoutedEventArgs e)
		{

		}

		// *************************************************
		//お気に入りボタン
		// *************************************************
		private void FavoriteButton_Click(object sender, RoutedEventArgs e)
		{

		}

		// *************************************************
		//保存ボタン
		// *************************************************
		private void SaveButton_Click(object sender, RoutedEventArgs e)
		{
			MsgBox.ShowAsync(this, "OK", "Cancel", "データを保存しますか?", this.CommandInvokedHandler);
		}

		// *************************************************
		//メールボタン
		// *************************************************
		private void MailButton_Click(object sender, RoutedEventArgs e)
		{

		}

	}
}
このアーティクルの参照用URLをクリップボードにコピー メンテナンス
MsgBox.cs ( No.2 )
日時: 2013/05/10 15:39
名前: lightbox
拡張子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Popups;

namespace S130510_LBOX_StoreBlank
{
	class MsgBox
	{

		// **********************************************************
		// 選択肢を応答するダイアログ
		// **********************************************************
		public static async void ShowAsync(MainPage pg, string ok, string cancel, string message, UICommandInvokedHandler handler)
		{
			// Page.Resources から TextResource を取得
			var NameSpace = pg.GetType().Namespace;
			var trs = pg.Resources.Values.Where(Value => Value.ToString() == NameSpace + ".TextResource");
			TextResource tr = (TextResource)trs.First();
			var Title = tr.PageTitle;
			var AppName = tr.AppName;
			var messageDialog = new MessageDialog(message, Title + " (" + AppName + ")");

			// OK ボタンのイベントを定義する
			var OK_Handler = new UICommandInvokedHandler(handler);
			var OK_Command = new UICommand(ok, OK_Handler) { Id = 0 };
			messageDialog.Commands.Add(OK_Command);

			// Cancel ボタンのイベントを定義する
			var CANCEL_Handler = new UICommandInvokedHandler(handler);
			var CANCEL_Command = new UICommand(cancel, CANCEL_Handler) { Id = 1 };
			messageDialog.Commands.Add(CANCEL_Command);

			// Enter キーで反応するデフォルトボタン
			messageDialog.DefaultCommandIndex = 0;
			// ESC キーで反応するキャンセルボタン
			messageDialog.CancelCommandIndex = 1;

			await messageDialog.ShowAsync();

		}

		// **********************************************************
		// 確認をするだけのダイアログ
		// **********************************************************
		public static async void ShowAsync(MainPage pg, string ok, string message)
		{
			// Page.Resources から TextResource を取得
			var NameSpace = pg.GetType().Namespace;
			var tr = pg.Resources.Values.Where(Value => Value.ToString() == NameSpace + ".TextResource");
			var Title = (tr.First() as TextResource).PageTitle;
			var AppName = (tr.First() as TextResource).AppName;
			var messageDialog = new MessageDialog(message, Title + " (" + AppName + ")");

			// OK ボタンのイベントを定義する
			var OK_Command = new UICommand(ok, (command) => { });
			messageDialog.Commands.Add(OK_Command);

			// Enter キーで反応するデフォルトボタン
			messageDialog.DefaultCommandIndex = 0;
			// ESC キーで反応するキャンセルボタン
			messageDialog.CancelCommandIndex = 0;

			await messageDialog.ShowAsync();

		}

	}
}
このアーティクルの参照用URLをクリップボードにコピー メンテナンス
TextResource.cs ( No.3 )
日時: 2013/05/10 15:40
名前: lightbox
拡張子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace S130510_LBOX_StoreBlank
{
	// XAML に定義する固定文字列を処理するクラス
	public class TextResource
	{
		public string PageTitle { get; set; }
		public string AppName { get; set; }
	}

}
このアーティクルの参照用URLをクリップボードにコピー メンテナンス
MainPagePartial.cs ( No.4 )
日時: 2013/05/10 15:55
名前: lightbox
MainPage クラスの partial
拡張子:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Automation;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace S130510_LBOX_StoreBlank
{
	public sealed partial class MainPage : Page
	{

		private void CommandInvokedHandler(IUICommand command)
		{
			Debug.WriteLine("保存ボタンがクリックされました");
			Debug.WriteLine("応答ボタンのテキストは《" + command.Label + "》です");

			// ボタンの応答
			if (command.Id.Equals(0))
			{
				Debug.WriteLine("OK");
			}
			if (command.Id.Equals(1))
			{
				Debug.WriteLine("CANCEL");
			}

		}

	}

}
このアーティクルの参照用URLをクリップボードにコピー メンテナンス
App.xaml.cs ( No.5 )
日時: 2013/05/10 16:39
名前: lightbox
拡張子:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

//名前空間
namespace S130510_LBOX_StoreBlank
{
	//クラス
	sealed partial class App : Application
	{

		//コンストラクタ
		public App()
		{
			this.InitializeComponent();
			this.Suspending += OnSuspending;
		}

		// プログラム全体で共有する構造体の定義( Navigate の引数用 )
		public struct MyParam
		{
			public string title;
			public int type;
		}

		// ルートフレームの作成
		public Frame rootFrame = Window.Current.Content as Frame;

		// *************************************************
		// アプリケーションがエンド ユーザーによって正常に起動されたときに呼び出されます
		// *************************************************
		protected override void OnLaunched(LaunchActivatedEventArgs args)
		{
			// Navigate の引数作成
			MyParam NavigateArgs = new MyParam() { title = "アプリケーション開始", type = 0 };

			// ウィンドウに既にコンテンツが表示されている場合は、アプリケーションの初期化を繰り返さずに、
			// ウィンドウがアクティブであることだけを確認してください
			if (rootFrame == null)
			{

				Debug.WriteLine("ルートフレームを新しく作成しました");
				rootFrame = new Frame();

				if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
				{
					//TODO: 以前中断したアプリケーションから状態を読み込みます。
				}

				// フレームを現在のウィンドウに配置します
				Window.Current.Content = rootFrame;
			}

			if (rootFrame.Content == null)
			{

				// *************************************************
				// ナビゲーション スタックが復元されていない場合、最初のページに移動します。
				// このとき、必要な情報をナビゲーション パラメーターとして渡して、新しいページを
				// 構成します
				// *************************************************
				if (!rootFrame.Navigate(typeof(MainPage), NavigateArgs))
				{
					throw new Exception("Failed to create initial page");
				}

				Debug.WriteLine("ルートフレームにページをロードしました");

			}

			// 現在のウィンドウがアクティブにします
			Window.Current.Activate();
		}

		// *************************************************
		// アプリケーションの実行が中断されたときに呼び出されます。
		// *************************************************
		private void OnSuspending(object sender, SuspendingEventArgs e)
		{
			Debug.WriteLine("アプリケーションの実行が中断されました");

			var deferral = e.SuspendingOperation.GetDeferral();
			//TODO: アプリケーションの状態を保存してバックグラウンドの動作があれば停止します
			deferral.Complete();
		}
	}
}
このアーティクルの参照用URLをクリップボードにコピー メンテナンス