当前位置: 首页 > news >正文

【WPF】IOC控制反转的应用:弹窗但不互相调用ViewModel

全称:Inversion of Control,控制反转
场景:A页面需要调用B/C页面等,防止直接在VM中新建别的页面实例,使用IOC设计架构;
创建Service,在Service中实现页面的实例创建和定义页面输入输出参数。
在MainView中注册A、B、C页面的Service。
A需要调用B时,调用B的Service。

此架构思路可以在MVVM基础上,减少不同模块的耦合。
可以所有模块的页面都注册服务。让VM之间不存在互相调用

手动实现

IOC类

public static class IoC
{
	private static readonly Dictionary<Type, object> MAP;

	static IoC()
	{
		MAP = new Dictionary<Type, object>();
	}

	public static void Register<T>(T instance)
	{
		MAP[typeof(T)] = instance;
	}

	public static T Provide<T>()
	{
		if (MAP.TryGetValue(typeof(T), out object obj) && obj is T t)
		{
			return t;
		}

		throw new Exception("No registered service of type " + typeof(T));
	}
}

IView接口

public interface IView
{
    /// <summary>
    /// Close the view with the given result
    /// </summary>
    void CloseDialog(bool result);
}

页面B实现IView

public partial class ChildValuePopup : Window, IView
{
    public ChildValueEditPopupViewModel ViewModel => (ChildValueEditPopupViewModel)this.DataContext;

    public ChildValuePopup()
    {
        InitializeComponent();
        DataContext = new ChildValueEditPopupViewModel(this);
    }

    public void CloseDialog(bool result)
    {
        this.DialogResult = result;
        this.Close();
    }
}

IViewBService页面B的Service接口

public interface IChildValueEditPopupService
{
//打开B页面方法,及其输入输出参数
    Task<ChildValueEditPopupResult> ChildValueEditPopupOpenAsync(GirdDataModel data);

    ChildValueEditPopupResult ChildValueEditPopupOpen(GirdDataModel data);
}

//输出参数类定义
//IsSuccess是否成功打开B页面
public class ChildValueEditPopupResult : ObservableObject
{
    public bool IsSuccess { get; set; }

    private object _setValue;

    public string code { get; set; }

    public object setValue { get=>_setValue; set=>OnPropertyChanged(ref _setValue,value); }
}

ViewBService页面B的Service实现

internal class ChildValueEditPopupService : IChildValueEditPopupService
{
	public ChildValueEditPopupResult ChildValueEditPopupOpen(GirdDataModel data)
	{
		var popup = new ChildValuePopup();
		popup.ViewModel.Code = data.code;
		popup.ViewModel.ChildValues = Copy.DeepCopy( data.childValues);
		popup.ViewModel.SetValue = data.setValue;

		bool result = popup.ShowDialog() == true;
		if (!result) {
			return new ChildValueEditPopupResult() { IsSuccess = false};
		}

		return new ChildValueEditPopupResult()
		{
			IsSuccess = true,
			code = popup.ViewModel.Code,
			setValue = popup.ViewModel.SetValue,
		}; 
	}

	public async Task<ChildValueEditPopupResult> ChildValueEditPopupOpenAsync(GirdDataModel data) {
		return await Application.Current.Dispatcher.InvokeAsync(() => {
			return ChildValueEditPopupOpen(data);
		});
	}
}

注册服务,全局页面MainView

public MainWindow()
{
    InitializeComponent();
    IoC.Register<IChildValueEditPopupService>(new ChildValueEditPopupService());
}

使用服务,页面A打开页面B

private void ChildValueEditPopupOpen(GirdDataModel data) {
    IChildValueEditPopupService service = IoC.Provide<IChildValueEditPopupService>();
    ChildValueEditPopupResult res = service.ChildValueEditPopupOpen(data);
    if (res.IsSuccess) {
        data.setValue = res.setValue;
    }
}
http://www.dtcms.com/a/121762.html

相关文章:

  • 构建实时、融合的湖仓一体数据分析平台:基于 Delta Lake 与 Apache Iceberg
  • 基于机器视觉的多孔零件边缘缺陷检测(源码C++、opencv、凸包、凸缺陷检测)
  • eplan许可证的用户权限管理
  • 4.实战篇-延迟约束
  • 基于MCP协议调用的大模型agent开发02
  • 11. 盛最多水的容器
  • Linux系统之rm命令的基本使用
  • leetcode每日一题:子数组异或查询
  • 主键索引和唯一索引的区别
  • linux安装mysql常出现的问题
  • 【Linux】进程信号(下)
  • 显示背光发烫异常解析
  • SQL语法进阶篇(一),数据库复杂查询——子查询
  • Redis过期key处理、内存淘汰策略与缓存一致性策略实践方案
  • PG:数据库表年龄大和表大的解决方案
  • Vue 框架组件间通信方式
  • Matplotlib图表坐标轴中文标签显示问题
  • 打印大X | 第六届蓝桥杯省赛C++C组
  • TDengine 数据模型设计:多列模式与单列模式对比(二)
  • PowerBI之DAX 2:聚合、统计、关系、表操作函数
  • 力扣题解:142. 环形链表 II
  • 柳宗元经典的10首唐诗
  • 指定运行级别
  • 【补题】Educational Codeforces Round 150 (Rated for Div. 2) C. Ranom Numbers
  • 计算机中的单位
  • 基于php扩展加密的一个简单逆向
  • 深入 C++ 线程库:从创建到同步的探索之旅
  • 天基光学图像仿真原理简介
  • 一个很好用的vue2在线签名组件
  • 《系统分析师-基础篇-7-9章总结》