WPF 高级篇 MVVM (MVVMlight) 依赖注入使用Messagebox
原文:WPF 高级篇 MVVM (MVVMlight) 依赖注入使用Messagebox
MVVMlight 实现依赖注入 把弹框功能 和接口功能注入到各个插件中
使用依赖注入 先把所有的ViewModel都注册到到容器中 MVVMlight SimpleIoc 来实现注册
-
-
public ViewModelLocator()
-
{
-
SimpleIoc.Default.Register<MainViewModel>();
-
SimpleIoc.Default.Register<EditBookViewModel>();
-
SimpleIoc.Default.Register<IDialogService, DialogService>();
-
}
-
-
-
private MainViewModel mainViewModel ;
-
-
public MainViewModel MainViewModel
-
{
-
get { return mainViewModel = SimpleIoc.Default.GetInstance<MainViewModel>(Guid.NewGuid().ToString()); }
-
set { mainViewModel = value; }
-
-
}
-
-
private EditBookViewModel editBookViewModel ;
-
-
public EditBookViewModel EditBookViewModel
-
{
-
get { return editBookViewModel = SimpleIoc.Default.GetInstance<EditBookViewModel>(Guid.NewGuid().ToString()); }
-
set { editBookViewModel = value; }
-
}
Message 接口
-
public interface IDialogService
-
{
-
void ShowMessage(string message, string title = "提示");
-
bool Confirm(string message, string title = "询问");
-
}
实现类
-
public class DialogService:IDialogService
-
{
-
public void ShowMessage(string message, string title = "提示")
-
{
-
MessageBox.Show(message, title, MessageBoxButton.OK, MessageBoxImage.Information);
-
}
-
-
public bool Confirm(string message, string title = "询问")
-
{
-
var result = MessageBox.Show(message, title, MessageBoxButton.OKCancel, MessageBoxImage.Question);
-
if (result == MessageBoxResult.OK)
-
{
-
return true;
-
}
-
else
-
{
-
return false;
-
}
-
}
-
}
在每一个View Model 的构造函数里 定义接口对象
-
public EditBookViewModel(IDialogService dialogService)
-
{
-
-
DialogService = dialogService;
-
}
-
private IDialogService dialogService;
-
-
public IDialogService DialogService
-
{
-
get { return dialogService; }
-
set { dialogService = value; }
-
}
并使用方法
DialogService.ShowMessage(EditBookArgs.IsEdit ? "编辑成功" : "新增成功");
WPF 高级篇 MVVM (MVVMlight) 依赖注入使用Messagebox的更多相关文章
- WPF 高级篇 MVVM 附加属性
原文:WPF 高级篇 MVVM 附加属性 WPF 特性之一 附加属性 在本文里实现文本框内容的验证 public class TextBoxHelper:DependencyObject { publ ...
- .NET Core基础篇之:依赖注入DependencyInjection
依赖注入已经不是什么新鲜话题了,在.NET Framework时期就已经出现了各种依赖注入框架,比如:autofac.unity等.只是在.net core微软将它搬上了台面,不用再依赖第三方组件(那 ...
- java框架篇---spring IOC依赖注入
spring依赖注入的方式有4种 构造方法注入 属性注入 工厂注入 注解注入 下面通过一个实例统一讲解: User.java package com.bjsxt.model; public class ...
- dotnetcore3.1 WPF 中使用依赖注入
dotnetcore3.1 WPF 中使用依赖注入 Intro 在 ASP.NET Core 中默认就已经集成了依赖注入,最近把 DbTool 迁移到了 WPF dotnetcore 3.1, 在 W ...
- Spring依赖注入的三种方式
看过几篇关于Spring依赖注入的文章,自己简单总结了一下,大概有三种方式: 1.自动装配 通过配置applicationContext.xml中的标签的default-autowire属性,或者标签 ...
- 依赖注入[2]: 基于IoC的设计模式
正如我们在<控制反转>提到过的,很多人将IoC理解为一种"面向对象的设计模式",实际上IoC自身不仅与面向对象没有必然的联系,它也算不上是一种设计模式.一般来讲,设计模 ...
- 在 XUnit 中使用依赖注入
在 XUnit 中使用依赖注入 Intro 之前写过一篇 xunit 的依赖注入相关的文章,但是实际使用起来不是那么方便 今天介绍一个基于xunit和微软依赖注入框架的"真正"的依 ...
- Spring系列4:依赖注入的2种方式
本文内容 基于构造器的依赖注入 基于setter的依赖注入 基于构造器的依赖注入 案例 定义2个简单的bean类,BeanOne 和 BeanTwo,前者依赖后者. package com.crab. ...
- 【.NET6+WPF】WPF使用prism框架+Unity IOC容器实现MVVM双向绑定和依赖注入
前言:在C/S架构上,WPF无疑已经是"桌面一霸"了.在.NET生态环境中,很多小伙伴还在使用Winform开发C/S架构的桌面应用.但是WPF也有很多年的历史了,并且基于MVVM ...
随机推荐
- swift混编
http://blog.csdn.net/fengsh998/article/details/34440159
- ios路线
http://www.cocoachina.com/ios/20150303/11218.html
- Arcgis CreateFishnet工具,生成到FileGDB中要素类的格网大小不一致
我的第一篇博客!哈哈 最近在做一些关于创建渔网的工作,发现一些问题,做个总结. 1.问题描述:如图1,设置好渔网的必要参数,输出目录为gdb里的矢量图层,(行列数比较大,渔网的地理范围较小),输出的格 ...
- 团队展示&选题 (白衣天使队)
作业详见此地址: https://www.cnblogs.com/bbplus/p/11735449.html
- Rust中的迭代器
和闭包一样,练代码 struct Counter { count: u32, } impl Counter { fn new() -> Counter { Counter {count: } } ...
- 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 ...
- c# 第六节 c#的程序结构,以及vs的文件结构
本节内容: 1:c#的程序结构 2:深入了解vs的文件 1:c#的程序结构 实例: 2:深入了解vs的文件 三者的关系: 3:命令空间是什么 使用别名:
- Fishing Master (思维+贪心)
题目网站:http://acm.hdu.edu.cn/showproblem.php?pid=6709 Problem Description Heard that eom is a fishing ...
- nexus php composer host 模式repo 试用
前边有介绍以及运行过基于nexus proxy 模式的php composer(其中也有一些坑),以下是关于host 模式的 简单使用 环境准备 docker-compose 文件 version ...
- <DFS & BFS> 130 127
130. Surrounded Regions BFS: 把所有边界上的O先换成A(标记),再深度遍历周围的点. 最后把O换成X(表示不符合要求),所有的A换回O class Solution { p ...