MVVM Light Toolkit

本文探讨了Messenger组件在MVVM设计模式中的作用及其带来的挑战,同时介绍了DispatcherHelper的使用方法,以及如何通过ServiceLocator和SimpleIoC实现依赖注入。此外,还详细解释了RaisePropertyChanged方法、EventToCommand绑定等关键技术。

Messager:

 

A Word of Caution About Messenger

 

Messenger is a powerful component that can greatly facilitate the task of communication, but it also makes the code more difficult to debug because it is not always clear at first sight which objects are receiving a message. Use with care!

 

DispatcherHelper 

因为 ViewModel 是一个 POCO,它不能访问 Dispatcher 属性,因此我需要通过另一种方式来访问主线程,以将操作加入队列中。这是 MVVM Light DispatcherHelper 组件的作用。

CheckBeginInvokeOnUI:

顾名思义,此方法首先执行检查。如果此方法的调用方已经在主线程上运行,则无需进行调度。在这种情况下会直接在主线程上立即执行委托。但如果此调用方是在后台线程上,则执行调度。

 

RaisePropertyChanged with CallerMemberName (.net 4.5 only):

 

protected void RaisePropertyChanged([CallerMemberName]string propertyName = "")
        {
            base.RaisePropertyChanged(propertyName);
        }

 

ServiceLocator and SimpelIoc:

App.xaml.cs:

 

<vm:ViewModelLocator x:Key="Locator"
                            d:IsDataSource="True" />

 

Mainwindow.xaml:

...
DataContext="{Binding Main, Source={StaticResource Locator}}">

ViewModelLocator.cs:

static ViewModelLocator()
       {
           ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
 
           if (ViewModelBase.IsInDesignModeStatic)
           {
               SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
           }
           else
           {
               SimpleIoc.Default.Register<IDataService, DataService>();
           }
 
           SimpleIoc.Default.Register<MainViewModel>();
       }
public MainViewModel Main
        {
            get
            {
                return ServiceLocator.Current.GetInstance<MainViewModel>();
            }
        }

 

Use the SET-Methode:

public class ViewModelBaseEx : ViewModelBase
{
    protected void Set<T>(ref T field, T newValue, [CallerMemberName] string propertyName = "")
    {
        Set(propertyName, ref field, newValue);
    }
}

public T Property
{
    get { return _property; }
    set { Set(ref _property, value); }
}

 

EventToCommand:


xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

xmlns:command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8"
...                         
<StackPanel Background="Transparent">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Tap">
                        <command:EventToCommand Command="{Binding Main.NavigateToArticleCommand, Mode=OneWay, Source={StaticResource Locator}}"
                                                CommandParameter="{Binding Mode=OneWay}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>

 

 

 

转载于:https://www.cnblogs.com/leihdm/p/3716355.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值