应用范围内的可观察集合 [英] App-wide Observable Collection

查看:69
本文介绍了应用范围内的可观察集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将Observable集合放在单独的.cs(类)文件中,并在我的 MainPage.xaml 中使用它,该如何使所有存储在该文件中的数据相同以后可以从其他XAML页面中访问Observable Collection吗?

If I put an Observable Collection inside a separate .cs (class) file and use it in my MainPage.xaml, how can I make it so that all the data stored inside that same Observable Collection is accessible from within a different XAML page at a later time?

例如

MyClass.cs:
public ObservableCollection<String> oC = new ObservableCollection<String>();

MainPage.xaml.cs:
// add to observable collection

SecondPage.xaml.cs:
// access and use data stored in ObservableCollection

推荐答案

我认为您只想要这个:

MyClass.cs:
public static ObservableCollection<String> oC = new ObservableCollection<String>();

MainPage.xaml.cs:
// add to observable collection

SecondPage.xaml.cs:
// access and use data stored in ObservableCollection

一旦oC是静态的,您就可以从任何类/页面中一次又一次地引用它.

Once the oC is static, you can reference it again and again from any class / page.

但是,如果您想绑定到它(由于无法绑定到Windows 8应用程序中的字段或静态属性),则需要让XAML页在一个简单的属性中引用您的静态属性:

If however you want to bind to it (since you cannot bind to fields OR to static properties in Windows 8 apps) you need to have that XAML page reference your static property in a simple property:

SecondPage.xaml.cs:
public static ObservableCollection<String> oC { get { return MyClass.oC; } }

我希望这是有道理的!

这篇关于应用范围内的可观察集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆