如何在cs页面之间传递变量?

时间:2013-08-30 11:51:29

标签: c# windows-phone-8

我在gotoWeb.xaml.cs收集了用户输入,并且已将收集的输入存储在隔离存储中。我需要将存储的变量传递给MainPage.xmal.cs。我不知道该怎么做?

我需要在MainPage.xaml中使用此var Page1var Page2。怎么做?

gotoWeb.xaml

中的代码
IsolatedStorageSettings.ApplicationSettings["Page1"] = site;
IsolatedStorageSettings.ApplicationSettings.Save();
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
//store it in the settings 
if (!settings.Contains("qsPage1"))
{
//if setting has not been created, add it
settings.Add("qsPage1", site);
}
else
{
//store a the page in the setting
settings["qsPage1"] = site;
}
var Page1 = IsolatedStorageSettings.ApplicationSettings["qsPage1"];
// and by using same isolated settings method, created one more varible
var Page2 = IsolatedStorageSettings.ApplicationSettings["qsPage2"];

3 个答案:

答案 0 :(得分:0)

您可以查看Messenger for MVVM Pattern。 你可以传递所有内容,而且很容易实现。

答案 1 :(得分:0)

使用ApplicationSettings时,即使退出应用程序,数据也会保留。如果这是您想要的,并且如果在应用程序重新启动后进入MainPage时此数据也将被加载,则只需覆盖MainPage的OnNavigatedTo以从ApplicationSettings加载数据。

答案 2 :(得分:-1)

非常快速和肮脏:

page1.xml:
public class values
    {
        public static var value1 = 123;
        public static var value2 = 234;
        public static var value3 = 345;


    }

 page2.xml:

 var localvalue1 = page1.values.value1;
 var localvalue2 = page1.values.value2;
 var localvalue3 = page1.values.value3;