Skip to main content
 首页 » 编程设计

c#之从头开始为 Windows 8.1 创建 MvvmCross Store 应用程序的过程

2024年02月13日14mate10pro

使用 Visual Studio 2013,我正在为基于 MvvmCross 的 Windows 8.1 创建一个新的 Windows 应用商店应用程序。

我首先根据 Core.txt 待办文件中的说明使用 View 模型创建我的 PCL .Core 类库。

接下来,我创建我的 .Store 应用程序,现在按照 Windows Store UI.txt 待办事项文件中的说明进行操作。我确实想使用导航框架,所以我将 FirstView.xaml 页面替换为 Basic Page FirstView.xaml。

根据说明,LayoutAwarePage类应该继承MvxStorePage,但是由于LayoutAwarePage.cs不再包含在VS2013/8.1 Store中项目,我改为将我的 FirstView.xaml 页面更改为 MvxStorePage 类型:

<views:MvxStorePage x:Name="pageRoot" x:Class="App.Store.Views.FirstView" 
 ... > 
... 
</views:MvxStorePage> 

通过此更改,当我启动应用程序时会显示 FirstView,但 View 模型绑定(bind)不起作用!在 Windows Store 8.1 应用程序中设置 MvvmCross View - View 模型绑定(bind)的正确过程是什么?

请您参考如下方法:

我似乎忘记了 Windows Store UI.txt 待办事项文件中的一部分说明:

Add a views folder and a view - xaml.cs and .xaml based on BasicPage - this will add 5 files to the Common folder.
- Change the Common/LayoutAwarePage.cs inheritance to Cirrious.MvvmCross.WindowsStore.Views.MvxStorePage
- Change the Common/LayoutAwarePage.cs - remove the OnNavigatedTo and OnNavigatedFrom handlers
- Add some content for your Xaml - e.g. <TextBlock Grid.Row="1" Text="{Binding Hello}"/>

如果我删除 OnNavigatedTo和 OnNavigatedFrom从 FirstView.xaml.cs 覆盖文件,FirstViewModel将正确绑定(bind)到 FirstView .

因此,据我所知,Windows 8.1 上的 Windows 应用商店应用说明应该是:

  • 在每个查看 .xaml 文件中,更改 <Page>至 <views:MvxStorePage> .
  • 在每个查看 .xaml.cs 文件中,删除 OnNavigatedTo和 OnNavigatedFrom覆盖。

编辑 此外,为了避免 View 模型混淆,删除 DefaultViewModel 也是一个好主意。属性(property)及相关defaultViewModel .xaml.cs 文件中的字段,并删除 DataContext来自 <views:MvxStorePage> 的属性.xaml 文件中的标记,因为相关的 DataContext无论如何都会由 MvvmCross 设置。