【问题标题】:How to implement MVVM in Windows Phone 8.1 app (xaml)?如何在 Windows Phone 8.1 应用程序 (xaml) 中实现 MVVM?
【发布时间】:2014-10-04 16:59:35
【问题描述】:

谁能给出一个实现的例子?是通过 ViewModel 实现 INotifyPropertyChanged (并引发事件,就像在 Silverlight 中所做的那样)或其他方式完成的? ViewModel 是如何绑定到视图的?

到目前为止,我发现的所有示例都不完整或已过时(请参阅 Silverlight 应用,不是 Xaml 应用)。

【问题讨论】:

  • 如果您有兴趣,我的博客上有simple MVVM example。正如 Abdurrahman 所说,MVVM 是一种模式,因此无论您使用的是 WPF 还是 Silverlight,该模式都是相同的。
  • 值得研究一些 MVVM 框架,Caliburn Micro 现在支持 WP8.1 和 Universal 应用程序。
  • @HenkHolterman 感谢您的建议,会检查一下
  • @Rachel AFAIK MVVM 在 Silverlight 中开箱即用。这意味着没有任何类型的外部依赖,你可以很容易地自己连接它。 XAML 应用中的 API 有所不同。
  • @Arnthor 我不确定您所说的“开箱即用”是什么意思。 MVVM 是设计应用程序时要遵循的一种模式,无需第三方工具即可轻松与 WPF 和 Silverlight 一起使用。我在第一条评论中添加的链接是一个不使用任何第三方组件的简单示例。听起来你找到了你要找的东西,祝你的项目好运。

标签: windows-phone-8 mvvm windows-phone-8.1


【解决方案1】:

如果是 Windows RT,我建议查看PRISM。它提供了非常好的现代开发实践。您将获得合适的导航服务、应用程序生命周期管理、强大的 MVVM 支持以及非常灵活的视图和 ViewModels 解析机制。 您可以通过NuGet 轻松地将其添加到您的项目中。 它有很好的文档,所以如果您有任何问题,您可以在MSDN 上找到答案,甚至可以下载免费书籍Prism for the Windows Runtime。我们的团队在使用 PRISM 构建项目方面拥有成功经验。

【讨论】:

    【解决方案2】:

    我目前在自己的 Universal/W8.1/WP8.1 应用程序中使用以下方法。这种方法使用RArcher WinRT Toolkit,这是一个基于 MVVM 模式的实验性工具包。 它提供了一种维护应用程序状态的方法,您可以使用ViewModelBase 来实现 INPC。 它还使用Unity dependency injection container

    我首先将ViewModelLocator 设为应用程序范围的资源,以便我的视图可以轻松访问它。

    <Application.Resources>
         <vm:ViewModelLocator x:Key="ViewModelLocator" />
    </Application.Resources>
    

    视图可以这样使用它:

    <Page.DataContext>
         <Binding Source="{StaticResource ViewModelLocator}" Path="MainViewModel" />
    </Page.DataContext>
    

    ViewModelLocator 看起来像这样:

    public sealed class ViewModelLocator
    {
         public ViewModelLocator()
         {
             RegisterIoCBindings();
         }
    
         public IMainViewModel MainViewModel
         {
             get { return IocContainer.Get<IMainViewModel>(); }
         }
    
         private void RegisterIoCBindings()
         {
             IocContainer.Container.RegisterType(typeof(IMainViewModel), typeof(MainViewModel), 
             null, new ContainerControlledLifetimeManager());
         }
    }
    

    MainViewModelViewModelBase 作为基类并实现IMainViewModel

    public sealed class MainViewModel : ViewModelBase, IMainViewModel
    {
         private string myText;
         [AutoState] 
         public string MyText
         {
             get { return myText; }
             set 
             { 
                 myText = value;
                 OnPropertyChanged();
             }
         }
    
         public MainViewModel() // You can use constructor injection here
         {             
         }
    }
    

    这是基本设置。正如其他人所说,MVVM 是一种模式,并且有很多方法可以使用它。我会说,使用感觉良好的东西;-)

    如果您想了解有关此方法的更多信息,请查看工具包和 unity DI。

    【讨论】:

    • 感谢您的详细回答,我会去检查一些框架,就像你描述的那样。我真的很失望,如果没有办法让 MVVM 开箱即用。
    • 您当然可以自己接线,如果这就是您所说的“开箱即用”的话。只需在您的视图模型上实现 INPC,您就差不多完成了。
    • 我会小心使用那个 ServiceLocator 模式。如前所述,框架包含统一性,因此您不妨将视图模型注入视图,而不是让视图接受这些依赖命中。
    【解决方案3】:

    没有区别,都是一样的。因为 MVVM 是一种模式。您可以轻松地将其实施到您的 Windows Phone 应用程序中。我正在为我的 wp 应用程序和 EventToCommand 行为使用 MVVM Light 来引发事件。我有一个在GitHub 上开源的应用程序,你可以查看它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-06
      • 2016-06-10
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多