• MVVMLight leaning note


    Learning Note For MvvmLight

    MvvmLight quitstart

    refer link1 : MVVMLight HelloWorld


    mc:Ignorable usage.

    we have a lot of places using mc:Ignorable in our xaml. it is used to Comment code in xaml.how to use it in our xaml, we can refer this topic.


    SimpleIOC

    ViewModelLocator.cs Analysis

    1. when we use mvvmlight template, we will have a file called ViewModelLocator.cs General Structor here:

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

      namespace mvvmtemplate.ViewModel
      {
      ///


      /// This class contains static references to all the view models in the
      /// application and provides an entry point for the bindings.
      ///

      public class ViewModelLocator
      {
      ///
      /// Initializes a new instance of the ViewModelLocator class.
      ///

      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
           }
       }
      

      }

    three key point: ServiceLocator.SetLocatorProvider(()=>SimpleIoc.Default) SimpleIoc.Default.Register() ServiceLocator.Current.GetInstance()

    1. for usage of SimpleIoc.Default.Register<>(), we can see the dll of the MvvmLight, a lot of overload functions about this. It is used to register the class to the SimpleIoc. Note: T class should only have one Contructor function, otherwise, it will report a error.

    2. ServiceLocator.Current.GetInstance(), it is used to get a instance of the Class T. it is used to for singleInstance mode, other overload functions definited in dll.


    Messager

    it is used to send specified message between MVVM. Simple usage.

    1. Register a specified message to an object.
      Sample code:

      Messenger.Default.Register(object, Action);

    Summary:

    Registers a recipient for a type of message TMessage. The action parameter will be executed when a corresponding message is sent.
    Registering a recipient does not create a hard reference to it, so if this recipient is deleted, no memory leak is caused.

    1. Send a specified message.
      Sample code:

      public virtual void Send(TMessage message){}

    Summary:
    Sends a message to registered recipients. The message will reach all recipients that registered for this message type using one of the Register methods.

    Type Parameters:
    TMessage: The type of message that will be sent.

    Parameters:
    message: The message to send to registered recipients.

    How to use:

    Messenger.Default.Send(new TMessage(s));
  • 相关阅读:
    titanium开发教程0206创建多行的选择器
    titanium开发教程0210创建的文本字段与嵌入的按钮
    titanium开发教程0209配置文本域和文本区键盘类型
    titanium开发教程0301理解tab group
    titanium开发教程0205创建单行的选择器
    linux shell删除文本每行末尾的单个或者多个空格或者制表符
    R语言中scale函数的用法
    R语言中批量提取当前目录中指定类型的文件
    python中提取包含指定字符的行、提取以指定字符开头、指定字符结尾的行
    python中如何统计文件的行数
  • 原文地址:https://www.cnblogs.com/kongshu-612/p/5469733.html
Copyright © 2020-2023  润新知