新时尚Windows8开发(三):应用程序状态
大家可能还记得,在WP开发中,我们曾讨论过应用程序状态。同样地,在Win8开发上,我们还可以适当地使用这玩意儿。上一节中,我们从应用程序周期中了解到,当我们的“板砖”应用程序不在前台运行时,就会被挂起。
而这个挂起行为其实和WP中的差不多,在WP中,其实应用程序的当前状态并不是说每次被放置到后台就会丢失,系统为我们维护五个应用程序的状态,一旦堆放在后台的应用程序数目超过五个,那就对不起了。呵,所以,我们应考虑在适当的时候保存应用程序状态。
在Win8也会类似这样的情况,不过,相对而言,没有WP控制那么严格,除非你手动把应用程序进程给干掉或者应用程序退出。
注意哦,咱们这里说的应用程序状态仅仅是在运行时有效,这些数据并不是保存到硬盘或存储设备上,只是存在于内存中,作用是方便我们传递数据罢了。
在过去的WinForm开发或者WPF开发中,我们会考虑使用一个静态类或在某类中声明一些静态字段来保存全局数据,即在声明的时候加上static关键字。
除了这种方法,今天,我们在Win8开发中,不妨用用这个类。
启动VS,打开“对象浏览器”,我们找到CoreApplication类,它位于Windows.ApplicationModel.Core命名空间。看图。
我在图上标注了,蓝色那圈圈,看到没?有个静态属性Properties,它其实就是一个字典结构。嗯,字典结构,应该懂了,就是一key一value那种。
现在就好办了,我们今天就是利用它来存储一些运行时信息,你可以把它看成是Asp.Net中的Session。
先简单这个示例是怎么玩的,玩法很简单,玩家一个,就你自己。
我们运行示例后,在第一个页面中分别输入两个值,然后把这两个值保存到CoreApplication.Properties字典中;
第二个页面和第三个页面分别取出这些值并显示出来。
为了能实现在同一个页面中查看多个视图,我在主页面中放一个Frame,再利用这个Frame来显示其它页面。主页的XAML如下。
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <ListBox Grid.Column="0" VerticalAlignment="Stretch" Width="200" FontSize="28" SelectionChanged="ListBox_SelectionChanged" SelectionMode="Single"> <ListBoxItem>页面一</ListBoxItem> <ListBoxItem>页面二</ListBoxItem> <ListBoxItem>页面三</ListBoxItem> </ListBox> <Frame x:Name="myFrame" Grid.Column="1"/> </Grid>
后台的C#代码就是根据ListBox中选择的页面来导航。
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ListBoxItem item = e.AddedItems[0] as ListBoxItem; if (item != null) { string str = item.Content as string; switch (str) { case "页面一": myFrame.Navigate(typeof(Page1)); break; case "页面二": myFrame.Navigate(typeof(Page2)); break; case "页面三": myFrame.Navigate(typeof(Page3)); break; default: break; } } }
下面,我们开始做第一个页面,就是用来输入数据并保存状态的。
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Margin="25" Text="第一个页面" Style="{StaticResource HeaderTextStyle}"/> <StackPanel Margin="15" Grid.Row="1"> <TextBlock Text="在本页输入两个状态值,第一个状态在页面二中获取;第二个状态值在页面三中获取。" Style="{StaticResource GroupHeaderTextStyle}"/> <TextBlock Text="输入第一个状态值:" Margin="0,13,0,0" Style="{StaticResource BodyTextStyle}"/> <TextBox Name="txt1" Margin="5,10,0,0"/> <TextBlock Text="输入第二个状态值:" Margin="0,18,0,0" Style="{StaticResource BodyTextStyle}"/> <TextBox Name="txt2" Margin="5,10,0,0"/> <Button Margin="12,20,0,0" Width="220" Content="保存状态" Click="onSave"/> </StackPanel> </Grid>
我们要处理保存按钮的单击事件。
private void onSave(object sender, RoutedEventArgs e) { if (string.IsNullOrWhiteSpace(txt1.Text) || string.IsNullOrWhiteSpace(txt2.Text)) { return; } Windows.ApplicationModel.Core.CoreApplication.Properties["value1"] = txt1.Text; Windows.ApplicationModel.Core.CoreApplication.Properties["value2"] = txt2.Text; }
后面两个页面几乎一样,就是从第一个页面保存的状态中读取。
页面二-XAML
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Margin="25" Text="第二个页面" Style="{StaticResource HeaderTextStyle}"/> <TextBlock Name="tb" FontSize="32" Margin="20,20,0,0" Grid.Row="1"/> </Grid>
页面二-C#
protected override void OnNavigatedTo(NavigationEventArgs e) { if (Windows.ApplicationModel.Core.CoreApplication.Properties.ContainsKey("value1")) { this.tb.Text = Windows.ApplicationModel.Core.CoreApplication.Properties["value1"] as string; } }
页面三-XAML
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Margin="25" Text="第三个页面" Style="{StaticResource HeaderTextStyle}"/> <TextBlock Name="tb" Grid.Row="1" Margin="20,20,0,0" FontSize="32"/> </Grid>
页面三-C#
protected override void OnNavigatedTo(NavigationEventArgs e) { if (Windows.ApplicationModel.Core.CoreApplication.Properties.ContainsKey("value2")) { this.tb.Text = Windows.ApplicationModel.Core.CoreApplication.Properties["value2"] as string; } }
运行一下吧。首先,在主页左边的列表中选页面一,输入两个数据,记得点保存按钮。
接着分别转到页面二和页面三,看看页面中显示了结果没?
这个简单吧?当然,在测试中我们也发现了,当挂起后再回到程序,文本框中输入的内容不见了,但已经保存到CoreApplication.Properties到中的内容仍然可以读取。
当然了,如果你把应用程序退出了,自然保存的状态也会丢失。因此,如果在程序挂起后保留文本框中的数据,各位应该知道怎么做了。
如果要在程序退出后,在下次运行时还能读取配置信息,那么就需要把数据保存到硬盘中了,这个嘛,下一篇文章再研究吧。
下面我们说说与运行时状态有关的另一个东东——页面参数传递。
各位一看到这个,一定会想起在WP中的页面传参数,而且那时候,我们还说了URI映射,记得否?忘了也关系,因为我们今天用不着URI映射。
在Win8“板砖”应用开发中,页面参数递参数是通过调用Frame类的Navigate方法,它有两个重载,其中一个是可以传参数的,即
public bool Navigate(System.Type sourcePageType, object parameter)
看这参数是Object类型的,这就让我们有了很大的发展空,我们可以传字符串,数值等,可以想多传一些数据,可以定义一个类,直接传递类实例也行。
废话少讲,我们来做个练习就懂了。
1、启动可爱的VS,新项目一个Windows Store应用,就是Win8应用了。
2、新建项目后,你会看到,默认打开了App.xaml.cs文件。
3、添加一个类,随你喜欢,反正有公共属性就行了。
public class Product { public string ProductName { get; set; } public string ProductID { get; set; } }
4、新建一个页面,命名为PageGet.xaml,名字你喜欢,自己记住就行了。
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <StackPanel Margin="30"> <TextBlock Text="通过页面传递的参数:" Style="{StaticResource HeaderTextStyle}"/> <StackPanel Orientation="Horizontal" Margin="5,80,0,10"> <TextBlock Text="产品编号:" FontSize="28"/> <TextBlock Name="tbProID" FontSize="28"/> </StackPanel> <StackPanel Orientation="Horizontal" Margin="5,13,0,0"> <TextBlock Text="产品名称:" FontSize="28"/> <TextBlock Name="tbProName" FontSize="28"/> </StackPanel> </StackPanel> </Grid>
在XAML文档中右击,从菜单中选择“查看代码”。
protected override void OnNavigatedTo(NavigationEventArgs e) { // 获取参数 Product product = e.Parameter as Product; if (product != null) { this.tbProID.Text = product.ProductID; this.tbProName.Text = product.ProductName; } }
5、回过头,打开MainPage.xaml,我们来布局一下。
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <StackPanel Margin="30"> <TextBlock Text="输入产品编号:" Style="{StaticResource SubheaderTextStyle}"/> <TextBox Name="txtID" Margin="5,10,5,0"/> <TextBlock Margin="0,20,0,0" Text="输入产品名称:" Style="{StaticResource SubheaderTextStyle}"/> <TextBox Name="txtName" Margin="5,10,5,0"/> <Button Content="跳转" Padding="35,10,35,10" Margin="15,20,0,0" Click="onNav"/> </StackPanel> </Grid>
处理按钮的单击事件。
private void onNav(object sender, RoutedEventArgs e) { // 取出当前窗口的根Frame if (Window.Current.Content is Frame && Window.Current.Content != null) { Frame myFrame = Window.Current.Content as Frame; Product prd = new Product() { ProductID = txtID.Text, ProductName = txtName.Text }; // 导航到目标页面并传递参数 myFrame.Navigate(typeof(PageGet), prd); } }
现在,我们运行一下。