2016-05-16 99 views
0

我有一些簡單的變量在不同視圖之間共享的問題。如何將數據從一個視圖共享到另一個視圖?

我有第一個主視圖叫MainPage.xaml和第二個叫Page2.xaml

我要檢查MainPage.xaml中檢查其單選按鈕上併發送與日期變量Page2.xaml

的MainPage:

namespace IDG 
{ 
    /// <summary> 
    /// An empty page that can be used on its own or navigated to within a Frame. 
    /// </summary> 
    public sealed partial class MainPage : Page 
    { 


     public string choice; 

     public MainPage() 
     { 
      this.InitializeComponent(); 

     } 

     private void bt_start_Click(object sender, RoutedEventArgs e) 
     { 

      if (rb_choice1.IsChecked == true) 
      { 
       choice = "choice1"; 

      } 

      if (rb_quiz.IsChecked == true) 
      { 
       this.Frame.Navigate(typeof(Page2), choice); 

      } 

     } 
    } 
}  

而第2頁:

namespace IDG 
{ 
    /// <summary> 
    /// An empty page that can be used on its own or navigated to within a Frame. 
    /// </summary> 
    public sealed partial class Page2 : Page 
    { 

     private string x; 

     public Page2() 
     { 
      this.InitializeComponent(); 

     } 



     protected override void OnNavigatedTo(NavigationEventArgs e) 
     { 
      var param = e.Parameter as string; 
      x = param; 
      textBlock1.Text = x; 
     } 

     private void button_Click(object sender, RoutedEventArgs e) 
     { 
      this.Frame.Navigate(typeof(MainPage)); 
     } 
    } 
} 

而且我想這個參數存儲在主類,怎麼辦呢?

回答

0

頁2OnNavigatedTo事件retreive這樣的值:var param = e.Parameter as string

EDIT

的retreived參數分配給在的OnNavigatedTo正文塊。在構建頁面時,x的值是「」。

public sealed partial class Page2 : Page 
{ 

    private string x=""; 

    public Page2() 
    { 
     this.InitializeComponent(); 
    } 



    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     x = e.Parameter as string; 
     textBlock1.Text = x; 
    } 

    private void button_Click(object sender, RoutedEventArgs e) 
    { 
     this.Frame.Navigate(typeof(MainPage)); 
    } 
} 
+0

好吧,但如何確切地使用它保護覆蓋無效OnNavigatedTo(NavigationEventArgs e) {0} {0} {0}字符串passedParameter = e.Parameter.ToString(); x = passedParameter; } – VividDreaming

+0

幾乎正確,只是用我發佈的單行代碼替換該函數的主體;) – WPMed

+0

好吧,但是如何在此方法之外訪問此變量參數?我有VAR參數= e.Parameter作爲字符串; x =參數;而且我不能在這個方法之外訪問x或者param變量? – VividDreaming

0

最近,我正在WPF的項目,但我們用的DevExpress庫使用它,併爲您的問題,這是非常容易的DevExpress被固定使者。

要接收數據我們剛剛註冊的使者,

public class Message { 
    //... 
} 
public class Recipient { 
    public Recipient() { 
     Messenger.Default.Register<string>(this, OnMessage1); 
     Messenger.Default.Register<Message>(this, OnMessage2); 
    } 
    void SendMessages() { 
     Messenger.Default.Send("test"); 
     Messenger.Default.Send(new Message()); 
    } 
    void OnMessage1(string message) { 
     //... 
    } 
    void OnMessage2(Message message) { 
     //... 
    } 
} 

然後你就可以從另一種觀點認爲發送,

public class InheritedMessage : Message { 
    //... 
} 
public class Recipient { 
    public Recipient() { 
     //Inherited messages are not processed with this subscription 
     Messenger.Default.Register<Message>(
      recipient: this, 
      action: OnMessage); 
     //Inherited messages are processed with this subscription 
     Messenger.Default.Register<Message>(
      recipient: this, 
      receiveInheritedMessagesToo: true, 
      action: OnMessage); 
    } 
    void SendMessages() { 
     Messenger.Default.Send(new Message()); 
     Messenger.Default.Send(new InheritedMessage()); 
    } 
    void OnMessage(Message message) { 
     //... 
    } 
} 

有了它,你可以通過模塊之間的數據(或觀點,但推薦使用MVVM) 如果您想了解更多關於DevExpress的信息,請通過https://documentation.devexpress.com/#WPF/CustomDocument17474

希望能夠可以幫助你。 :)

+0

謝謝雅各:) – VividDreaming

相關問題