欢迎来到山村网

windows phone页面间数据共享

2019-03-02 09:58:49浏览:992 来源:山村网   
核心摘要:可以通过静态属性Application.Current可以返回当前程序的Application对象,然后可以简单地将其转换成App类型。这意味着可以使用A

可以通过静态属性Application.Current可以返回当前程序的Application对象,然后可以简
单地将其转换成App类型。这意味着可以使用App类来存储用于程序中多个页面共享的数据。
下面例子演示如何利用App类实现页面间数据共享:
在Silverlight项目的App类定义一个简单的公共属性:
public partial class App : Application
{
//用于在页面间共享数据的公共属性
public Color? SharedColor { set; get; }//这个属性定义为可空的(nullable)Color
对象,而不仅仅是一般的Color对象
...
}
源页面MainPage如下所示:
MainPage.xaml中包含TextBlock:
<TextBlock HorizontalAlignment="Center" Name="txt1" Text="navigate to 2nd page" VerticalAlignment="Center" ManipulationStarted="txt1_ManipulationStarted" />
MainPage.xaml.cs代码如下:
namespace PhoneApp2
{
public partial class MainPage : PhoneApplicationPage
{
Random rand = new Random();
// 构造函数
public MainPage()
{
InitializeComponent();
}
private void txt1_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
String destination = "/Page1.xaml";
if (this.ContentPanel.Background is SolidColorBrush)
{
(Application.Current as App).SharedColor = (this.ContentPanel.Background as SolidColorBrush).Color;//在导航到Page1页面之前首先将Color对象保存到App类的属性中
}
this.NavigationService.Navigate(new Uri(destination, UriKind.Relative));//导航至指定页面
e.Complete();
e.Handled = true;
}
protected override void onManipulationStarted(ManipulationStartedEventArgs e)
{
//当触摸到页面里textblock以外的部分时,contentpanel的背景会变成随机颜色。
this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(255), (byte)rand.Next(255), (byte)rand.Next(255)));//设置背景颜色
base.onManipulationStarted(e);
}
protected override void onNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)//该函数被调用时,页面的构造函数已经执行完毕,但还没有执行其他的方法
{
Color? sharedColor = (Application.Current as App).SharedColor;//访问公共属性
if (sharedColor != null)
{
this.ContentPanel.Background = new SolidColorBrush(sharedColor.Value);
}
base.onNavigatedTo(e);
}
}
}
目标页面Page1如下所示:
Page1.xaml中包含TextBlock:
<TextBlock HorizontalAlignment="Left" Margin="157,212,0,0" Name="txt2" Text="go back to 1st page" VerticalAlignment="Top" ManipulationStarted="txt2_ManipulationStarted" />Page1.xaml.cs代码如下所示:
namespace PhoneApp2
{
public partial class Page1 : PhoneApplicationPage
{
Random rand = new Random();
public Page1()
{
InitializeComponent();
}
protected override void onNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)//该函数被调用时,页面的构造函数已经执行完毕,但还没有执行其他的方法
{
Color? shareColor = (Application.Current as App).SharedColor;//访问公共属性
if (shareColor != null)
{
this.ContentPanel.Background = new SolidColorBrush(shareColor.Value);
}
base.onNavigatedTo(e);
}
private void txt2_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
if (this.ContentPanel.Background is SolidColorBrush)
{
(Application.Current as App).SharedColor = (this.ContentPanel.Background as SolidColorBrush).Color;//在返回到MainPage页面之前将当前Color对象保存到App类的属性中
}
this.NavigationService.GoBack();
e.Complete();
e.Handled = true;
}
protected override void onManipulationStarted(ManipulationStartedEventArgs e)
{
//当触摸到页面里textblock以外的部分时,contentpanel的背景会变成随机颜色。
this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(255), (byte)rand.Next(255), (byte)rand.Next(255)));//设置背景颜色
base.onManipulationStarted(e);
}
}
}
运行程序你就会发现在页面之间进行导航时,页面总是共享相同的颜色。
其实你在App类所定义的属性在这里相当于一个全局变量,整个应用程序皆可访问。

你可以通过静态属性PhoneApplicationService.Current来获取现有PhoneApplicationService的实例,如下所示:
PhoneApplicationService appService = PhoneApplicationService.Current;
PhoneApplicationService类定义在命名空间Microsoft.Phone.Shell中,它的实例在标准的App.xaml文件中创建:
<Application.ApplicationLifetimeObjects>
<!--处理应用程序的生存期事件所需的对象-->
<shell:PhoneApplicationService
Launching="Application_Launching" Closing="Application_Closing"
Activated="Application_Activated" Deactivated="Application_Deactivated"/>
</Application.ApplicationLifetimeObjects>
PhoneApplicationService包含一个State属性(类型为IDictionary<String,object>)可用于保存和恢复数据。但该State字典只适用于存储程序同一次运行过程中的临时数据。如果需要在程序多次执行过程之间保存数据,建议使用独立存储。访问State属性内容类似下面所示:
Color clr = (Color)appService.State["Color"];
appService.State["Color"] = clr;

(责任编辑:豆豆)
下一篇:

windows phone页面间传递数据

上一篇:

Java_Jdk 环境变量配置

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com