ソース掲示板




すべてから検索

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



SkyDrive

以下のソースコードは、テンプレートをもとに、『S130510_LBOX_FileAccess』でプロジェクトを作成しています

Win8 ストア : ストアブランク テンプレートを元にファイルアクセス部分を実装しています
MainPage.xaml
拡張子:
<Page
	x:Class="S130510_LBOX_FileAccess.MainPage"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:local="using:S130510_LBOX_FileAccess"
	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="FileAccess テンプレート"
			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"
						/>
					
					<!--メールボタン-->
					<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 18:19
名前: lightbox


日時: 2013/05/10 18:19
名前: 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_FileAccess
{
	//クラス
	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)
			{
				// インスタンス作成
				var btn = new Button();
				// スタイル
				btn.Style = (Style)App.Current.Resources["DeleteAppBarButtonStyle"];
				// テキスト表示
				AutomationProperties.SetName(btn, "削除");
				// 非同期ラムダ式
				btn.Click += async (_sender, _e) =>
				{
					MsgBox.ShowAsync(this, "OK", "ファイルを削除します");
					try
					{
						StorageFile strageFile = await storageFolder.GetFileAsync("text.txt");
						// ファイル削除
						await strageFile.DeleteAsync();
						// メニュー削除
						LeftBar.Children.RemoveAt(2);

					}
					catch (Exception ex)
					{
						Debug.WriteLine(ex.Message);
					}

					// Navigate の引数作成
					App.MyParam NavigateArgs = new App.MyParam() { title = "リロード", type = 1 };
					// ページを再表示
					(App.Current as App).rootFrame.Navigate(typeof(MainPage), NavigateArgs);

				};
				LeftBar.Children.Add(btn);

				SaveButton.IsEnabled = true;
			}
			// ******************************************************
			// ファイルが存在しない。
			// 1) 作成ボタンを作成して処理を登録する
			// 2) 保存ボタンを選択できないようにする
			// ******************************************************
			else
			{
				// インスタンス作成
				var btn = new Button();
				// スタイル
				btn.Style = (Style)App.Current.Resources["AddAppBarButtonStyle"];
				// テキスト表示
				AutomationProperties.SetName(btn, "ファイル作成");
				// 非同期ラムダ式
				btn.Click += async (_sender, _e) =>
				{
					MsgBox.ShowAsync(this, "OK", "ファイルを作成します");
					try
					{
						await storageFolder.CreateFileAsync("text.txt");
						// メニュー削除
						LeftBar.Children.RemoveAt(2);

					}
					catch (Exception ex)
					{
						Debug.WriteLine(ex.Message);
					}

					// Navigate の引数作成
					App.MyParam NavigateArgs = new App.MyParam() { title = "リロード", type = 2 };
					// ページを再表示
					(App.Current as App).rootFrame.Navigate(typeof(MainPage), NavigateArgs);

				};
				LeftBar.Children.Add(btn);

				SaveButton.IsEnabled = false;

			}

		}

		// *************************************************
		// このページがフレームに表示されるときに呼び出されます。
		// *************************************************
		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をクリップボードにコピー メンテナンス
キャラクタをセット変換して処理するサンプル ( No.2 )
日時: 2013/05/10 19:45
名前: lightbox
拡張子:
// *************************************************
//設定ボタン
// *************************************************
private async void SettingButton_Click(object sender, RoutedEventArgs e)
{

	// 書き出データ( SHIFT_JIS のバイナリデータ )
	var text_data = Encoding.GetEncoding("SHIFT_JIS").GetBytes("あいうえお"+Environment.NewLine);

	// 書き出すフォルダ( マニフェストで使用宣言が必要 )
	StorageFolder storageFolder = KnownFolders.DocumentsLibrary;

	// ファイルオブジェクトを実体とともに作成( 存在していた場合は上書き )
	var textFile = await storageFolder.CreateFileAsync(
		"text.txt", 
		CreationCollisionOption.ReplaceExisting
		);

	// メモリストリーム
	var memoryStream = new InMemoryRandomAccessStream();
	// データライタ
	var dataWriter = new DataWriter(memoryStream);
	// バイト配列を書き込み
	dataWriter.WriteBytes(text_data);
	// オブジェクトからメモリを切り離す
	var textBuffer = dataWriter.DetachBuffer();
	// オブジェクトを解放( 本来は using を使う )
	dataWriter.Dispose();
	// オブジェクトを解放( 本来は using を使う )
	memoryStream.Dispose();

	// 非同期の Windows8 専用のファイル書き込み処理
	// string を直接書き込む場合は WriteTextAsync : 但し utf-8(n)
	await FileIO.WriteBufferAsync(textFile, textBuffer);

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