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;
            }
        }
    }
  • 相关阅读:
    PHP教程:PHPUnit学习笔记(三)测试方法进阶
    PHP教程:PHPUnit学习笔记(二)PHPUnit基本用法
    PHP教程:PHPUnit学习笔记(一)PHPUnit介绍及安装
    phpunit框架安装
    防注入(url)
    ssh 密钥详解
    JS判断登陆端是PC还是手机
    字节换算器
    gif 图片制作和拆解
    让你的网站秒开 为IIS启用“内容过期”
  • 原文地址:https://www.cnblogs.com/Fred1987/p/13994669.html
Copyright © 2011-2022 走看看