简体   繁体   English

如何在Windows Phone 8.1应用程序的两个页面之间传递数据?

[英]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. 我有一个Windows Phone 8.1应用程序,其中有两个页面显示数据。 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. 第一个页面具有一个列表视图,显示来自sqlite数据库的所有数据,并且当用户点击列表视图中的一个项目时,第二个页面将打开一个列表视图,其中包含有关该数据库中特定记录的更多详细信息。 My question is how to achieve this in Windows Phone 8.1 using c# ? 我的问题是如何使用c#在Windows Phone 8.1中实现此目标?

in windows phone 8.1 there are two ways to do it: 在Windows Phone 8.1中,有两种方法可以实现:

     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; 在SecondPage中,您可以通过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 ). 对于第二页,您应该重写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). 要直接传递数据,您可以将其作为查询字符串参数(如网页URL)放入URI中。 So, for example, instead of navigating to secondpage.xaml , you could navigate to secondpage.xaml?param1=abc123&param2=<base64 data> . 因此,例如,您可以导航至secondpage.xaml?param1=abc123&param2=<base64 data> ,而不是导航至secondpage.xaml 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). 您可以通过覆盖onNavigatedTo函数来获取导航参数,就像检查应用程序启动时所用的参数一样(但可能不在起始页上)。 Note that this may be Silverlight-specific; 请注意,这可能是Silverlight特定的。 I haven't tried it with non-Silverlight apps. 我没有在非Silverlight应用程序中尝试过。
  • 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. 您可以向从其他类读取和写入的App类(在App.xaml.cs定义)中添加一个公共字段。 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. 对于您要实现的目标来说,这可能是过高的选择,但是,例如,如果您想让应用程序的一部分记录来自摄像机的大量视频,而另一部分处理它,则这将是一个可行的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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