Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

uwp - C# Navigating Between Pages Without losing Data

One of my assignments is to pass data to another page which I understand. But how do I get the data to stay when I go back to the Page? Can someone explain how this works.

 public sealed partial class MainPage : Page
    {
       string userName;
       public MainPage()
     {

        this.InitializeComponent();
    }

    private void btnPage2_Click(object sender, RoutedEventArgs e)
    {
        Frame.Navigate(typeof(Page2) ,  userName);
    }

    private void btnName_Click(object sender, RoutedEventArgs e)
    {
        userName = Convert.ToString(txtname.Text);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have answered a similar question here. The answer there applies to this problem as well, however in your case you are especially focusing on the data part in question, so here you should pay special attention to the second part of the answer - using a ViewModel that will outlive the page itself would be the most beneficial solution. However using NavigationCacheMode will be sufficient as well, as the page class itself along with its fields will be preserved.

There is a third option available here, which is using static for the field you want to keep, or creating a static or singleton class that will hold the data. This however comes with a disadvantage when compared to the ViewModel solution as it does not easily allow having a single page multiple times in the navigation stack with different state data.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...