問題描述
我的問題與在頁面之間傳遞數據有完全相同的問題,但僅適用于 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 在頁面之間傳遞數據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!