使用BasicPage
页面间传递复杂对象:
private void go2_Click(object sender, RoutedEventArgs e)
{
student stu = new student() { id = 1, name = "ggz" }; //类必须定义为public
var serializer = new DataContractSerializer(typeof(student));
MemoryStream stream = new MemoryStream();
serializer.WriteObject(stream, stu);
byte[] barray = stream.ToArray();
string param = Encoding.UTF8.GetString(barray,0, barray.Length);
System.Diagnostics.Debug.WriteLine(param);
Frame.Navigate(typeof(BasicPage1),param);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
byte[] buffer = Encoding.UTF8.GetBytes((string)e.Parameter);
MemoryStream stream = new MemoryStream(buffer);
DataContractSerializer serializer = new DataContractSerializer(typeof(student));
student stu= serializer.ReadObject(stream) as student;
navigationHelper.OnNavigatedTo(e);
if (stu != null)
this.textBox1.Text = stu.name;
}
步骤:
App:
OnLaunched中添加:
rootFrame = new Frame();
App12.Common.SuspensionManager.RegisterFrame(rootFrame, "appFrame");//第一句注册
// Set the default language
rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];
rootFrame.NavigationFailed += OnNavigationFailed;
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
await App12.Common.SuspensionManager.RestoreAsync();//第二句还原
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
OnSuspending中添加:
await App12.Common.SuspensionManager.SaveAsync();
MainPage:
private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
if (e.PageState != null && e.PageState.ContainsKey("input"))
put.Text = e.PageState["input"].ToString();
}
private void navigationHelper_SaveState(object sender, SaveStateEventArgs e)
{
e.PageState["input"] = put.Text;
}
使用本地设置:
保存:
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
//ApplicationDataContainer 存储数据的容器 ApplicationData 提供对应用程序数据存储区的访问
// localSettings 获取本地应用程序数据存储区中的应用程序设置容器。
localSettings.Values["value"] = nameinput.Text;
//将 nameinput.Text的内容以键值对的形式保存到ApplicationDataContainer
恢复:
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
if(localSettings.Values.ContainsKey(“value”))
{
nameinput.Text = localSettings.Values["value"].ToString();
}