2010-11-29 83 views
3

價值傳遞到另一個頁面的XAML可以輕鬆如何將一個對象從一個xaml頁面傳遞給另一個?

NavigationService.Navigate(new Uri("/SecondPage.xaml?msg=" + textBox1.Text, UriKind.Relative));

做不過,這只是爲字符串值。我想將一個對象傳遞給xaml頁面。我怎麼做?

在SO和WP7論壇上發現了類似的問題。解決方案是使用全局變量(不是最好的解決方案)。

WP7: Pass parameter to new page?

http://social.msdn.microsoft.com/Forums/en-US/windowsphone7series/thread/81ca8713-809a-4505-8422-000a42c30da8

回答

3

看一看創建的默認密碼,當你開始一個新的數據綁定項目。它顯示了將選定對象的引用傳遞給詳細信息頁面的方法。

+0

謝謝。默認項目通過查詢字符串間接傳遞對象,查詢字符串然後從App.ViewModel訪問。這將工作,但我希望有一個更直接傳遞對象的更優雅的解決方案。 – samwize 2010-11-30 03:08:14

1

我建議在看Caliburn.Micro!

http://caliburnmicro.codeplex.com

+0

謝謝我將研究這個框架(聽說過幾次)。你知道它是否可以傳遞對象到頁面嗎? – samwize 2010-11-30 02:59:20

+0

綁定到ViewModel/Presenter/Controller/Screen應該可以管理這個! – 2010-11-30 06:20:07

5

使用OnNavigatedFrom方法當我們調用NavigationService.Navigate方法

OnNavigateFrom被調用。它具有一個NavigationEventArgs對象作爲參數,返回目標頁面的Content屬性,我們可以通過該屬性訪問目標頁面的屬性「DestinationPage.xaml.cs」

首先,在目標頁面「DestinationPage.xaml。 CS 「申報財產 」SomeProperty「:

public ComplexObject SomeProperty { get; set; } 

現在,在 」MainPage.xaml.cs中「,覆蓋OnNavigatedFrom方法:

protected override void OnNavigatedFrom(NavigationEventArgs e) 
{ 
// NavigationEventArgs returns destination page "DestinationPage" 
    DestinationPage dPage = e.Content as DestinationPage; 
    if (dPage != null) 
    { 
     // Change property of destination page 
     dPage.SomeProperty = new ComplexObject(); 
    } 
} 

現在,拿在SomeProperty值」 DestinationPage。 xaml.cs「:

private void DestinationPage_Loaded(object sender, RoutedEventArgs e) 
{ 
    // This will display a the Name of you object (assuming it has a Name property) 
    MessageBox.Show(this.SomeProperty.Name); 
} 
相關問題