原文:WPF 高级篇 MVVM (MVVMlight) 依赖注入使用Messagebox

MVVMlight 实现依赖注入 把弹框功能 和接口功能注入到各个插件中

使用依赖注入 先把所有的ViewModel都注册到到容器中 MVVMlight SimpleIoc 来实现注册


  1. public ViewModelLocator()
  2. {
  3. SimpleIoc.Default.Register<MainViewModel>();
  4. SimpleIoc.Default.Register<EditBookViewModel>();
  5. SimpleIoc.Default.Register<IDialogService, DialogService>();
  6. }
  7. private MainViewModel mainViewModel ;
  8. public MainViewModel MainViewModel
  9. {
  10. get { return mainViewModel = SimpleIoc.Default.GetInstance<MainViewModel>(Guid.NewGuid().ToString()); }
  11. set { mainViewModel = value; }
  12. }
  13. private EditBookViewModel editBookViewModel ;
  14. public EditBookViewModel EditBookViewModel
  15. {
  16. get { return editBookViewModel = SimpleIoc.Default.GetInstance<EditBookViewModel>(Guid.NewGuid().ToString()); }
  17. set { editBookViewModel = value; }
  18. }

Message 接口


  1. public interface IDialogService
  2. {
  3. void ShowMessage(string message, string title = "提示");
  4. bool Confirm(string message, string title = "询问");
  5. }

实现类


  1. public class DialogService:IDialogService
  2. {
  3. public void ShowMessage(string message, string title = "提示")
  4. {
  5. MessageBox.Show(message, title, MessageBoxButton.OK, MessageBoxImage.Information);
  6. }
  7. public bool Confirm(string message, string title = "询问")
  8. {
  9. var result = MessageBox.Show(message, title, MessageBoxButton.OKCancel, MessageBoxImage.Question);
  10. if (result == MessageBoxResult.OK)
  11. {
  12. return true;
  13. }
  14. else
  15. {
  16. return false;
  17. }
  18. }
  19. }

在每一个View Model 的构造函数里  定义接口对象


  1. public EditBookViewModel(IDialogService dialogService)
  2. {
  3. DialogService = dialogService;
  4. }
  5. private IDialogService dialogService;
  6. public IDialogService DialogService
  7. {
  8. get { return dialogService; }
  9. set { dialogService = value; }
  10. }

并使用方法

  DialogService.ShowMessage(EditBookArgs.IsEdit ? "编辑成功" : "新增成功");

WPF 高级篇 MVVM (MVVMlight) 依赖注入使用Messagebox的更多相关文章

  1. WPF 高级篇 MVVM 附加属性

    原文:WPF 高级篇 MVVM 附加属性 WPF 特性之一 附加属性 在本文里实现文本框内容的验证 public class TextBoxHelper:DependencyObject { publ ...

  2. .NET Core基础篇之:依赖注入DependencyInjection

    依赖注入已经不是什么新鲜话题了,在.NET Framework时期就已经出现了各种依赖注入框架,比如:autofac.unity等.只是在.net core微软将它搬上了台面,不用再依赖第三方组件(那 ...

  3. java框架篇---spring IOC依赖注入

    spring依赖注入的方式有4种 构造方法注入 属性注入 工厂注入 注解注入 下面通过一个实例统一讲解: User.java package com.bjsxt.model; public class ...

  4. dotnetcore3.1 WPF 中使用依赖注入

    dotnetcore3.1 WPF 中使用依赖注入 Intro 在 ASP.NET Core 中默认就已经集成了依赖注入,最近把 DbTool 迁移到了 WPF dotnetcore 3.1, 在 W ...

  5. Spring依赖注入的三种方式

    看过几篇关于Spring依赖注入的文章,自己简单总结了一下,大概有三种方式: 1.自动装配 通过配置applicationContext.xml中的标签的default-autowire属性,或者标签 ...

  6. 依赖注入[2]: 基于IoC的设计模式

    正如我们在<控制反转>提到过的,很多人将IoC理解为一种"面向对象的设计模式",实际上IoC自身不仅与面向对象没有必然的联系,它也算不上是一种设计模式.一般来讲,设计模 ...

  7. 在 XUnit 中使用依赖注入

    在 XUnit 中使用依赖注入 Intro 之前写过一篇 xunit 的依赖注入相关的文章,但是实际使用起来不是那么方便 今天介绍一个基于xunit和微软依赖注入框架的"真正"的依 ...

  8. Spring系列4:依赖注入的2种方式

    本文内容 基于构造器的依赖注入 基于setter的依赖注入 基于构造器的依赖注入 案例 定义2个简单的bean类,BeanOne 和 BeanTwo,前者依赖后者. package com.crab. ...

  9. 【.NET6+WPF】WPF使用prism框架+Unity IOC容器实现MVVM双向绑定和依赖注入

    前言:在C/S架构上,WPF无疑已经是"桌面一霸"了.在.NET生态环境中,很多小伙伴还在使用Winform开发C/S架构的桌面应用.但是WPF也有很多年的历史了,并且基于MVVM ...

随机推荐

  1. swift混编

    http://blog.csdn.net/fengsh998/article/details/34440159

  2. ios路线

    http://www.cocoachina.com/ios/20150303/11218.html

  3. Arcgis CreateFishnet工具,生成到FileGDB中要素类的格网大小不一致

    我的第一篇博客!哈哈 最近在做一些关于创建渔网的工作,发现一些问题,做个总结. 1.问题描述:如图1,设置好渔网的必要参数,输出目录为gdb里的矢量图层,(行列数比较大,渔网的地理范围较小),输出的格 ...

  4. 团队展示&选题 (白衣天使队)

    作业详见此地址:    https://www.cnblogs.com/bbplus/p/11735449.html

  5. Rust中的迭代器

    和闭包一样,练代码 struct Counter { count: u32, } impl Counter { fn new() -> Counter { Counter {count: } } ...

  6. E04 【买衣服】Do you have this T-shirt in red?

    核心句型 Do you have this T-shirt in red? 这样的T恤衫,你们有红色的吗? 场景对话: A:Excuse me,do you have this T-shirt in ...

  7. c# 第六节 c#的程序结构,以及vs的文件结构

    本节内容: 1:c#的程序结构 2:深入了解vs的文件 1:c#的程序结构 实例: 2:深入了解vs的文件 三者的关系: 3:命令空间是什么 使用别名:

  8. Fishing Master (思维+贪心)

    题目网站:http://acm.hdu.edu.cn/showproblem.php?pid=6709 Problem Description Heard that eom is a fishing ...

  9. nexus php composer host 模式repo 试用

    前边有介绍以及运行过基于nexus proxy 模式的php composer(其中也有一些坑),以下是关于host 模式的 简单使用 环境准备 docker-compose 文件   version ...

  10. <DFS & BFS> 130 127

    130. Surrounded Regions BFS: 把所有边界上的O先换成A(标记),再深度遍历周围的点. 最后把O换成X(表示不符合要求),所有的A换回O class Solution { p ...