1

关于我正在阅读的教程,我有 2 个问题。

Q1。

通过教程,他们使用数据源

使用应用程序中的数据

若要在应用程序中使用数据,请在 App.xaml 中创建数据源实例作为资源。您将实例命名为 feedDataSource。

BR211380.wedge(zh-cn,WIN.10).gif向应用程序添加资源

Double-click App.xaml in Solution Explorer. The file opens in the XAML editor.
Add the resource declaration, <local:FeedDataSource x:Key="feedDataSource"/>, to the root ResourceDictionary, after the
MergedDictionaries collection.

然后他们在 OnLaunch 方法中使用它。

 var connectionProfile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();
 if (connectionProfile != null)
 {
     FeedDataSource feedDataSource = (FeedDataSource)App.Current.Resources["feedDataSource"];
     if (feedDataSource != null)
     {
         if (feedDataSource.Feeds.Count == 0)
         {
             await feedDataSource.GetFeedsAsync();
         }
     }
 }

我想知道他们为什么将它存储在资源中?为什么不只创建一个类的实例并从中获取结果?

Q2。

在文章的后面,他们将此数据源项与“网格视图项”一起使用。我在他们的其他模板项目中看到了这一点。我想知道是否有制作界面的标准方法?

起初我想也许只是在屏幕上放一些图像按钮并连接他们的点击事件,但现在我不确定。

4

1 回答 1

0

The XAML Resource essentially does create an instance for you and makes it available in the Resources collection, so you could instantiate the class yourself. Having it as a resource keeps this object around and makes it accessible across the various pages in your application. You could certainly create the class explicitly, and if you enforce the singleton pattern on it, it would be semantically equivalent.

I'm not sure I see the context of your second question in the tutorial, but in general the pattern you are seeing is Model-View-ViewModel (MVVM), which is the de facto standard pattern for Windows Store apps. feedDataSource is providing the model and portions of that are assigned to DefaultViewModel, which is the DataContext for all of the binding markup in the XAML pages, which are the views. The idea behind this to separate your data from your model, so that when you do things like load a new data feed, etc., all you need to do is change the data source, and all of the data binding markup will automatically reflect the new data in your user interface.

If you find yourself writing code that looks like TextBox.Text = "My text", then you're deviating from the pattern.

于 2013-01-24T01:47:19.640 回答