コメント |
@DIV
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)
{
}
}
}
@END |