因为win8应用同一时刻只允许一个应用在前台运行,即当切换到其他应用时当前应用将被挂起且停止运行,此时如果我们正在进行一些状态信息的录入那么我们就应该对这些信息进行临时保存,以便在需要的时候进行恢复。
ok,开始:新建一个空白应用程序,向项目中添加类StateDate,代码如下:
public class StateData:INotifyPropertyChanged { //保存状态信息的键名 private const string KEY_TITLE = "title"; private const string KEY_CONTENT = "content"; //当前类实例的静态属性 public static StateData CurrentInstance { get; set; } //标题 private string m_title; public string Title { get { return this.m_title; } set { if(value!=this.m_title) { this.m_title = value; onPropertyChanged("Title"); } } } //内容 private string m_content; public string Content { get { return this.m_content; } set { if(value!=this.m_content) { this.m_content = value; onPropertyChanged("Content"); } } } //构造函数 private StateData() { this.m_title = string.Empty; this.m_content = string.Empty; } //静态构造函数 static StateData() { CurrentInstance = new StateData(); } /// <summary> /// 加载前先保存状态信息 /// </summary> public static void LoadState() { ApplicationDataContainer container = ApplicationData.Current.LocalSettings; if (container.Values.ContainsKey(KEY_TITLE)) { CurrentInstance.Title = (string)container.Values[KEY_TITLE]; } if(container.Values.ContainsKey(KEY_CONTENT)) { CurrentInstance.Content=(string)container.Values[KEY_CONTENT]; } } /// <summary> /// 保存状态信息 /// </summary> public static void SaveState() { ApplicationDataContainer container = ApplicationData.Current.LocalSettings; container.Values[KEY_TITLE] = CurrentInstance.Title; container.Values[KEY_CONTENT] = CurrentInstance.Content; } public event PropertyChangedEventHandler PropertyChanged; private void onPropertyChanged(string prop) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); } } }
然后打开MainPage.xaml,在页面上添加两个TextBox控件:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel Orientation="Vertical" Margin="25"> <TextBlock FontSize="24" Text="邮件标题:" Margin="2,2,0,3"/> <TextBox x:Name="txtTitle" Margin="3,0,5,3"/> <TextBlock FontSize="24" Text="邮件正文:" Margin="2,21,0,3"/> <TextBox x:Name="txtContent" Margin="3,0,5,3" Height="200" TextWrapping="Wrap"/> </StackPanel> </Grid>
接着MainPage的后置代码里,在OnNavigatedTo方法里加入如下代码:
protected override void OnNavigatedTo(NavigationEventArgs e) { //Title Binding myTitleBinding = new Binding(); myTitleBinding.Path = new PropertyPath("Title"); myTitleBinding.Source = StateData.CurrentInstance; myTitleBinding.Mode = BindingMode.TwoWay; this.txtTitle.SetBinding(TextBox.TextProperty, myTitleBinding); //Content Binding myContextBinding = new Binding(); myContextBinding.Path = new PropertyPath("Content"); myContextBinding.Source = StateData.CurrentInstance; myContextBinding.Mode = BindingMode.TwoWay; this.txtContent.SetBinding(TextBox.TextProperty, myContextBinding); }
打开App.xaml.cs文件,在OnLaunched方法中"TODO"注释后面加上以下代码:
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) { //TODO: 从之前挂起的应用程序加载状态 StateData.LoadState(); }
在OnSuspending方法中"TODO"注释后面添加以下代码:
StateData.SaveState();
注:1、有关文中提到的OnLaunched,OnSuspending等方法的用法详见MSDN:https://msdn.microsoft.com/zh-cn/library/windows/apps/windows.ui.xaml.application.onlaunched.aspx,这里不多家啰嗦。
2、LocalSettings:用于获取本地应用程序数据存储区中的应用程序设置容器。
按F5运行将看到这样的界面(我这里选择的是模拟器,看个人喜好)
在调试位置工具栏上选择“挂起并关闭”
然后再次按F5运行你会发现标题和内容依然存在。
时间: 2024-11-05 12:19:14