应用范围内的可观察集合 [英] App-wide Observable Collection
问题描述
如果我将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屋!