想要在另一个页面中显示一个页面的结果? [英] Want to show the results of a page in another page?

查看:39
本文介绍了想要在另一个页面中显示一个页面的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用独立存储创建了一个 Windows Phone 7 应用程序.在这个应用程序中,我使用了一个名为 btnRead 的按钮、一个名为 txtRead 的文本块和一个名为 txtWrite 的文本框.如果我向文本框(txtWrite)写一些东西并点击按钮(btnRead).然后 textblock(txtRead) 显示或保存我在文本框上写的任何内容(所有这些都是在单个 MainPage.xaml 中创建的).现在我创建了另一个 page1.xaml 并创建了一个名为 txtShow 的文本块.但我希望 textblock(txtShow) 显示我在 MainPage.xaml 中的文本框上写的所有内容.我还上传了我的项目 - https://skydrive.live.com/redir.aspx?cid=ea5aaefa4ad2307a&resid=EA5AAEFA4AD2307A!133&parid=EA5AAEFA4AD2307A!109

I've created a windows phone 7 application using Isolated Storage. In this application i've used a button named as btnRead, a textblock named as txtRead and a text box named as txtWrite. If i write something to the textbox(txtWrite) and clicked on the button(btnRead). Then the textblock(txtRead) shows or saves whatever i write on textbox(All these are created in a single MainPage.xaml). Now I have created another page1.xaml and created a textblock named as txtShow. But I want the textblock(txtShow) to show all the things that i write on textbox which is in MainPage.xaml. I have also uploaded my project- https://skydrive.live.com/redir.aspx?cid=ea5aaefa4ad2307a&resid=EA5AAEFA4AD2307A!133&parid=EA5AAEFA4AD2307A!109

以下是我使用过的 MainPage.xaml.cs 源代码 -:

Below is MainPage.xaml.cs source that i have used -:

    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.";
        }
    }

推荐答案

如果您在同一页面的 TextBlock 中显示来自 TextBox 的文本,通过绑定来实现会更容易

If you are displaying the text from the TextBox in a TextBlock in the same page, it would be easier to do that through binding

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

要将此信息放入下一页,您可以将其放入 NavigationContext 以传递到下一页

To put this information into the next page you could put it into the NavigationContext to pass to the next page

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

If you like using IsoStorage, you could do the read like you are doing above in the OnNavigatedTo method.

这篇关于想要在另一个页面中显示一个页面的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆