简体   繁体   中英

How to pass data between two pages in Windows Phone 8.1 application ?

I have a Windows phone 8.1 application where I have two pages displaying data. The first page has a listview displaying all of the data from an sqlite database, and when the user taps on one item in the listview, the second page opens with a listview that contains more details about that specific record from the database. My question is how to achieve this in Windows Phone 8.1 using c# ?

in windows phone 8.1 there are two ways to do it:

     void SelectionChanged(object sender, SelectionChangedEventArgs    e)
    {
      var list= sender as Listview;
      var taped=list.SelectedItem as Model;
      //here you send your parameter to the second page
      Frame.Navigate(Typeof(SecondPage),taped);
    }

or

    public static Model taped;
     void SelectionChanged(object sender, SelectionChangedEventArgs    e)
    {
      var list= sender as Listview;
      taped=list.SelectedItem as Model;


    }

and in the SecondPage you can acces to it via FirstPage.taped;

On first page you should write something like this:

Frame.Navigate(typeof(SecondPage), someData);

For second page you should override OnNavigatedTo( NavigationEventArgs e ). For example:

protected override void OnNavigatedTo( NavigationEventArgs e )
{
    MyData data = e.Parameter as MyData.
}

Several options:

  • To directly pass data, you can put it in the URI as a querystring parameter (like a web page URL). So, for example, instead of navigating to secondpage.xaml , you could navigate to secondpage.xaml?param1=abc123&param2=<base64 data> . You pick up the navigation parameter by overriding the onNavigatedTo function, same as you would do for checking the parameters that an app was launched with (but not, presumably, on the start page). Note that this may be Silverlight-specific; I haven't tried it with non-Silverlight apps.
  • To just pass data around the app in general, you can use any number of global objects. You can add a public field to the App class (as defined in App.xaml.cs ) that you read and write from other classes. You can add a static public field in any class, and read and write from it. Globals are considered a bad design, but sticking them on the specific classes that are supposed to access them is reasonable. Note that different pages might sometimes be processed on different threads.
  • Use persistent storage. You can write to the isolated storage settings dictionary, or just write out a file and have the other page look for it. That's probably overkill for what you're trying to achieve, but it would be a viable solution if, for example, you wanted to have one part of the app record a lot of video from the camera, and another part process it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM