想要在另一个页面中显示一个页面的结果

本文关键字:一个 结果 显示 另一个 | 更新日期: 2023-09-27 18:33:59

>我使用独立存储创建了一个Windows Phone 7应用程序。在这个应用程序中,我使用了一个名为btnRead的按钮,一个名为txtRead的文本块和一个名为txtWrite的文本框。如果我在文本框(txtWrite)中写入某些内容并单击按钮(btnRead)。然后textblock(txtRead)显示或保存我在textbox上写的任何内容(所有这些都是在单个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.";
        }
    }

想要在另一个页面中显示一个页面的结果

如果要在同一页面的文本块中显示文本框中的文本,则通过绑定会更容易做到这一点

<TextBox x:Name="txtWrite"/>
<TextBlock  Text="{Binding Text, ElementName=txtWrite}"/>
要将此信息放入下一页

,您可以将其放入导航上下文以传递到下一页

    // 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方法中所做的那样进行读取。