zoukankan      html  css  js  c++  java
  • WPF UnityContainer traditional using Microsoft.Practices.Unity;

    1.Install-Package Unity -Version 4.0.1

    2.

    using Microsoft.Practices.Unity;
    using WpfApp9.IModels;
    using WpfApp9.Models;
    using WpfApp9.ViewModel;
    
    namespace WpfApp9
    {
        /// <summary>
        /// Interaction logic for App.xaml
        /// </summary>
        public partial class App : Application
        {
            protected override void OnStartup(StartupEventArgs e)
            {
                base.OnStartup(e);
                IUnityContainer unityContainer = new UnityContainer();
                unityContainer.RegisterType<IDateModel, DateModel>();
                var mainVM = unityContainer.Resolve<MainVM>();
                var window = new MainWindow
                {
                    DataContext = mainVM
                };
                window.Show();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace WpfApp9.IModels
    {
        interface IDateModel
        {
            string GetDateTime();
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using WpfApp9.IModels;
    namespace WpfApp9.Models
    {
        public class DateModel : IDateModel
        {
            public string DateTimeNow { get; set; }
    
            public DateModel()
            {
                DateTimeNow = GetDateTime();
            }
    
            public string GetDateTime()
            {
                return DateTime.Now.ToString("yyyyMMddHHmmssffff");
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using WpfApp9.Models;
    
    namespace WpfApp9.ViewModel
    {
        public class MainVM
        {
            private DateModel dateModel;
            public MainVM()
            {
            }
    
            public MainVM(DateModel dateHelper)
            {
                dateModel = dateHelper;
            }
        }
    }
  • 相关阅读:
    [RxSwift]3.3、数据绑定(订阅)
    [RxSwift]3.2、函数式编程 -> 函数响应式编程
    [RxSwift]2、Hello RxSwift!:我的第一个 RxSwift 应用程序
    [RxSwift]1、为什么要使用 RxSwift ?
    [RxSwift]RxSwift: ReactiveX for Swift
    [Swift]UIViewController
    104. 二叉树的最大深度
    103. 二叉树的锯齿形层次遍历
    102. 二叉树的层序遍历
    98. 验证二叉搜索树
  • 原文地址:https://www.cnblogs.com/Fred1987/p/13994669.html
Copyright © 2011-2022 走看看