简体   繁体   English

WP7 Mango上的中央datacontext与否?

[英]Central datacontext on WP7 Mango or not?

I'm starting to migrate one app to WP7 Mango now. 我现在开始将一个应用程序迁移到WP7 Mango。 One of the reasons is the database support :-) 其中一个原因是数据库支持:-)

Technically the database thing is really clear. 从技术上讲,数据库的事情非常清楚。 But I'm a bit unsure where should I put my datacontext. 但我有点不确定我应该把我的datacontext放在哪里。 I see primary two options: 我看到主要的两个选项:

  1. global datacontext for everything 一切的全球数据文字
  2. a separate datacontext instance per app feature 每个应用程序功能单独的datacontext实例

I would like to have some thoughts on that. 我想对此有一些想法。

Thanks! 谢谢!

The demo included with the SQLite2CE tool might help you in that it gives an example of how to use datacontext and ViewModels. SQLite2CE工具附带的演示可能会帮助您,它提供了如何使用datacontext和ViewModels的示例。 Visit http://sqlite2ce.codeplex.com/ and download the package, the linked articles on this page will also be a help. 访问http://sqlite2ce.codeplex.com/并下载该软件包,此页面上的链接文章也将是一个帮助。 As a bonus, if you haven't yet converted your database from SQLite then the tool will help you with that as well. 作为奖励,如果您还没有从SQLite转换数据库,那么该工具也可以帮助您。

If you take a look at the demo there's a MainViewModel (set as the datacontext of the application's MainPage.xaml) which contains an ObservableCollection for each referenced class of data. 如果您查看演示,则会有一个MainViewModel(设置为应用程序的MainPage.xaml的datacontext),其中包含每个引用的数据类的ObservableCollection。 A second ViewModel is used to compile a custom set of data from the database but is still referenced though an ObservableCollection in MainViewModel. 第二个ViewModel用于从数据库编译自定义数据集,但仍然通过MainViewModel中的ObservableCollection引用。 This way each control you wish to bind in XAML only needs to reference a column within the appropriate ObservableCollection object. 这样,您希望在XAML中绑定的每个控件只需要引用相应ObservableCollection对象中的列。

At the moment I'm would use multiple datacontext for the different features. 目前我将使用多个datacontext来实现不同的功能。 I'll tried to use a single one through the singleton pattern. 我会尝试通过单例模式使用单个。 This caused some exceptions because you'll need to syncronize the SubmitChanges() calls - especially if different threads are used. 这导致了一些异常,因为您需要同步SubmitChanges()调用 - 尤其是在使用不同的线程时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM