1

分離ストレージを使用して Windows Phone 7 アプリケーションを作成しました。このアプリケーションでは、btnRead という名前のボタン、txtRead という名前のテキストブロック、txtWrite という名前のテキスト ボックスを使用しました。テキストボックス(txtWrite)に何かを書き込んでボタン(btnRead)をクリックした場合。次に、テキストブロック(txtRead)は、テキストボックスに書いたものを表示または保存します(これらはすべて単一のMainPage.xamlで作成されます)。ここで、別の page1.xaml を作成し、txtShow という名前のテキストブロックを作成しました。しかし、Textblock(txtShow) に、MainPage.xaml にあるテキストボックスに書いたすべてのものを表示したいのです。プロジェクトもアップロードしました - https://skydrive.live.com/redir.aspx?cid=ea5aaefa4ad2307a&resid=EA5AAEFA4AD2307A!133&parid=EA5AAEFA4AD2307A!109

以下は、私が使用した MainPage.xaml.cs ソースです -:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
        myStore.CreateDirectory("Bookmark");

        using (var isoFileStream = new IsolatedStorageFileStream("Bookmark\\myFile.txt", FileMode.OpenOrCreate, myStore))
        {
            //Write the data
            using (var isoFileWriter = new StreamWriter(isoFileStream))
            {
                isoFileWriter.WriteLine(txtWrite.Text);
            }
        }

        try
        {
            // Specify the file path and options.
            using (var isoFileStream = new IsolatedStorageFileStream("Bookmark\\myFile.txt", FileMode.Open, myStore))
            {
                // Read the data.
                using (var isoFileReader = new StreamReader(isoFileStream))
                {
                    txtRead.Text = isoFileReader.ReadLine();
                }
            }
        }
        catch
        {
            // Handle the case when the user attempts to click the Read button first.
            txtRead.Text = "Need to create directory and the file first.";
        }
    }
4

1 に答える 1

2

同じページのTextBlockにTextBoxからのテキストを表示している場合は、バインディングを使用して表示する方が簡単です。

<TextBox x:Name="txtWrite"/>
<TextBlock  Text="{Binding Text, ElementName=txtWrite}"/>

この情報を次のページに配置するには、NavigationContextに配置して、次のページに渡すことができます。

    // Navigate to Page1 FROM MainPage 
    // This can be done in a button click event
    NavigationService.Navigate(new Uri("/Page1.xaml?text=" + txtWrite.Text, UriKind.Relative));

// Override OnNavigatedTo in Page1.xaml.cs
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    string text;
    NavigationContext.QueryString.TryGetValue("text", out text);
    txtRead.Text = text;
}

IsoStorageを使用する場合は、上記のOnNavigatedToメソッドで行っているように読み取りを行うことができます。

于 2012-05-10T20:41:32.293 に答える