我有一些簡單的變量在不同視圖之間共享的問題。如何將數據從一個視圖共享到另一個視圖?
我有第一個主視圖叫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));
}
}
}
而且我想這個參數存儲在主類,怎麼辦呢?
好吧,但如何確切地使用它保護覆蓋無效OnNavigatedTo(NavigationEventArgs e) {0} {0} {0}字符串passedParameter = e.Parameter.ToString(); x = passedParameter; } – VividDreaming
幾乎正確,只是用我發佈的單行代碼替換該函數的主體;) – WPMed
好吧,但是如何在此方法之外訪問此變量參數?我有VAR參數= e.Parameter作爲字符串; x =參數;而且我不能在這個方法之外訪問x或者param變量? – VividDreaming