【萬里征程——Windows App開發】如何在多個頁面間讀取/儲存檔案【草稿】

nomasp發表於2015-06-19

首先在第二個頁面中

static public  StorageFolder  folder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile file = await folder.CreateFileAsync("New Document.txt", CreationCollisionOption.ReplaceExisting);

await FileIO.WriteTextAsync(file, "Write text to file4.");
if (this.Frame != null)
{
    this.Frame.Navigate(typeof(MainPage));
}

在主頁面中:

 private async void LoadSettings()
        {
            StorageFile file = await AddProject.folder.GetFileAsync("New Document.txt");
            tBlockProjectName.Text = await FileIO.ReadTextAsync(file);
        }

在主頁面的MainPage中:

   LoadSettings();

在主頁面的OnNavigationTo中:

  protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            LoadSettings()
            // TODO: Prepare page for display here.

            // TODO: If your application contains multiple pages, ensure that you are
            // handling the hardware Back button by registering for the
            // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
            // If you are using the NavigationHelper provided by some templates,
            // this event is handled for you.
        }

相關文章