首页 > Windows开发 > 详细

【Windows Phone 8】使用MVVMLight框架

时间:2014-01-26 17:56:50      阅读:515      评论:0      收藏:0      [点我收藏+]

【简介】

1.作者文章:http://www.galasoft.ch/mvvm/

2.可以通过Nuget下载MVVLight

 

【对比引用文件】

bubuko.com,布布扣

普通WindowsPhone引用

 

bubuko.com,布布扣

使用MVVMLight的WindowsPhone程序引用

 

Microsoft.Practices.ServiceLocation:依赖注入机制的服务本地化程序集。该程序集能够通过为依赖注入提供抽象层整合任何适合的依赖注入容器。

Systems.Windows.interactivity:事件,交互

 

【安装完MVVMLight之后的文件结构】

bubuko.com,布布扣

 

bubuko.com,布布扣
<Application x:Class="MVVMLight学习.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
             xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:vm="clr-namespace:MVVMLight学习.ViewModel"
             d1p1:Ignorable="d" >
    <!--应用程序资源-->
    <Application.Resources>
        <local:LocalizedStrings xmlns:local="clr-namespace:MVVMLight学习" x:Key="LocalizedStrings" />
        <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
    </Application.Resources>
    <Application.ApplicationLifetimeObjects>
        <!--处理应用程序的生存期事件所需的对象-->
        <shell:PhoneApplicationService 
            Launching="Application_Launching" 
            Closing="Application_Closing" 
            Activated="Application_Activated" 
            Deactivated="Application_Deactivated" />
    </Application.ApplicationLifetimeObjects>
</Application>
App.Xaml

App.Xaml中使用了ViewModelLocator进行依赖注入,代码如下:

bubuko.com,布布扣
/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocator xmlns:vm="clr-namespace:MVVMLight学习"
                           x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"

  You can also use Blend to do all this with the tool‘s support.
  See http://www.galasoft.ch/mvvm
*/

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;

namespace MVVMLight学习.ViewModel
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// </summary>
    public class ViewModelLocator
    {
        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            ////if (ViewModelBase.IsInDesignModeStatic)
            ////{
            ////    // Create design time view services and models
            ////    SimpleIoc.Default.Register<IDataService, DesignDataService>();
            ////}
            ////else
            ////{
            ////    // Create run time view services and models
            ////    SimpleIoc.Default.Register<IDataService, DataService>();
            ////}

            SimpleIoc.Default.Register<MainViewModel>();
        }

        public MainViewModel Main
        {
            get
            {
                return ServiceLocator.Current.GetInstance<MainViewModel>();
            }
        }

        public static void Cleanup()
        {
            // TODO Clear the ViewModels
        }
    }
}
ViewModelLocator

【Windows Phone 8】使用MVVMLight框架

原文:http://www.cnblogs.com/fb-boy/p/3533716.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!