问题描述
我的问题与在页面之间传递数据有完全相同的问题,但仅适用于 Windows Phone 8.1(相对于 Windows Phone 7).问题来了:
I have the exact same question as Passing data from page to page, but only for Windows Phone 8.1 (opposed to Windows Phone 7). Here is the question:
我正在寻找有关如何在页面之间传递数据的最佳实践.
I'm looking for the best practice on how to pass data from page to page.
在页面 A 中,我有一个触发页面 B 的按钮.在页面 B 我有 6 个文本框,允许用户输入信息.当用户完成后,点击一个按钮将他们带回页面 A.
In Page A I have a button that fires off Page B. On Page B I have 6 textboxes that allow the user to enter information. When the user is done, the click on a button that brings them back to Page A.
我想将该数据传递回页面 A.
I want to pass that data back to Page A.
我看到了以下建议:
构建 XML 文档并保存到独立存储使用 App 类在属性中存储信息像查询字符串一样传递它我正在寻找最佳实践.有没有一种是 Microsoft 推荐的,或者是公认的最佳方法?
build XML documents and save to Isolated Storage use the App class to store information in properties pass it like a query string I'm looking for the Best practice. Is there one that Microsoft recommends or one that is generally accepted as the best way?
谢谢
推荐答案
在 WP8.1 运行时 - 对于 Silverlight,WP8.0 中使用的方法 应该仍然有效 - 您有两个选择:
In WP8.1 Runtime - for Silverlight, the methods used in WP8.0 still should work - you have couple of choces:
第一种可能也是最简单的方法是使用 使用参数导航 - 如果它是可序列化类型,则不必将其转换为 string:
the first and probably the easiest way is to use Navigate with parameter - you don't have to convert it to string if it's a serializable type:
// let's assume that you have a simple class:
public class PassedData
{
public string Name { get; set; }
public int Value { get; set; }
}
// then you navigate like this:
Frame.Navigate(typeof(Page1), new PassedData { Name = "my name", Value = 10 });
// and in target Page you retrive the information:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
PassedData data = e.Parameter as PassedData;
}
您可以使用一些静态对象在应用程序中传递您的数据
you can use some static objects to pass your data along the App
请注意,您还必须处理应用程序暂停/恢复 - 因此在应用程序暂停时保存您的数据并在应用程序恢复时加载是合适的.您应该记住,当应用程序恢复时,OnNavigatedTo 不会被调用.
Note that you will also have to handle app suspending/resuming - so it will be suitable to save your data when the app is being suspended and load when it's resumed. You should remember that OnNavigatedTo is not being called when the app is resuming.
以上是关于正常导航(前进).如果你想在上一个页面中填充一些数据,那么你有几个选择:
The above was about normal navigation (forward). If you want to fill some data in previous Page, then you have a couple of options:
- 将处理程序传递给前一个页面,以便您可以从当前页面访问公共变量/属性,
- 使用静态变量/属性 - 可能是单例
- 再次使用文件/设置
请注意,前两种方法也有一个缺点,即应用程序在暂停后可能会崩溃.在这里保存到文件可能会更好,认为需要更多的工作和适当的处理.
Note that again the first two methods has a disadvantage that the app may crash after being suspended. Saving to files might be better here, thought needs some more work and proper handling.
这篇关于为 Windows Phone 8.1 在页面之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!