wpf 在MVVM中使用IOC思想通过注册窗体的方法实现在ViewModel里面打开其他窗口的方法

理论:在MVVM中,View通过添加ViewModel的引用来指定DataContext,也就是View可以引用ViewModel,但ViewModel不可以引用View,而ViewModel在实现逻辑过程中如果需要访问其他的窗体该怎么办呢,比如打开一个对话框,这里通过IOC思想来解决该问题。

1 新建wpf应用

2 新建Views,Models,ViewModels文件夹

3 新建Base文件夹,在其里面添加CommandBase类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WpfApp1.Base
{
    public class CommandBase : ICommand
    {
        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            DoExecute?.Invoke();
        }

        public Action DoExecute { get; set; }


        public CommandBase(Action doExecute)
        {
            this.DoExecute = doExecute;
        }
    }
}

4 在Base里面新建WindowManager类,该类就是用IOC思想创建的第三方类,通过其来解耦ViewModel和View,该类通过一个注册方法来注册,并通过显示方法来获取并显示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp1.Base
{
    public class WindowManager
    {
        private static Dictionary<string, Type> _Windows = new Dictionary<string, Type>();

        public static void Register(Type type,string key)
        {
            if (!_Windows.ContainsKey(key))
            {
                _Windows.Add(key, type);
            }
        }

        public static bool ShowWindow(string key)
        {
            if (_Windows.ContainsKey(key))
            {
                Type type = _Windows[key];
                Window win = (Window)Activator.CreateInstance(type);
                return win.ShowDialog() == true;
            }
            return false;
        }
    }
}

5 在Views里面添加一个窗体Window1,实现对话框功能

<Window x:Class="WpfApp1.Views.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1.Views"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800">
    <Grid>
        <Button Content="ok" Click="Button_Click" />
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp1.Views
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }
    }
}

6 在ViewModels里面新建MainViewModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using WpfApp1.Base;
using WpfApp1.Models;

namespace WpfApp1.ViewModels
{
    public class MainViewModel
    {

        public ICommand btnCommand { get; set; }


        public MainViewModel()
        {
            btnCommand = new CommandBase(() =>
            {
                WindowManager.ShowWindow("window1");
            });
        }
    }
}

7 修改MainWindow

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        xmlns:vm="clr-namespace:WpfApp1.ViewModels"
        mc:Ignorable="d" FontSize="30"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <vm:MainViewModel />
    </Window.DataContext>
    <Grid>
        <Button Content="test" Command="{Binding btnCommand}" />
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApp1.Base;
using WpfApp1.Views;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            WindowManager.Register(typeof(Window1), "window1");
        }
    }
}

注意:在MainWindow的构造函数中将Window1注册到WindowManager中,然后就可以在MainViewModel中通过WindowManger拿到并显示出来

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF (Windows Presentation Foundation) 是一种用于创建 Windows 应用程序的技术,它提供了强大的用户界面设计工具和丰富的功能。而MVVM (Model-View-ViewModel) 是一种设计模式,用于处理应用程序的逻辑和用户界面的分离。它通过将应用程序的数据和行为逻辑与用户界面分离开来,使得开发更加容易。 开源框架是指可以免费使用和修改的编程工具和库。WPF MVVM 开源框架是基于 WPFMVVM 设计模式的开源软件,通过提供封装好的代码和组件,帮助开发人员更加高效地开发 WPF 应用程序。 WPF MVVM 开源框架通常包含以下特点: 1. 数据绑定:允许开发人员将数据模型与用户界面元素绑定在一起,使数据的变化自动反映在用户界面上。 2. 命令:提供了一种方便的方式来处理用户界面上的交互行为,如按钮点击、菜单选择等。 3. 事件聚合:帮助解决在 MVVM 模式处理事件的复杂性,使得 ViewModel 更加简单和可测试。 4. IoC 容器:提供了一种依赖注入的机制,使开发人员能够更灵活地管理和组织应用程序的各个部分。 5. 组件库:通常会包含一些常用的 UI 控件和样式,可以帮助快速构建用户界面。 目前有一些知名的 WPF MVVM 开源框架,如 Prism、Caliburn.Micro、MVVMLight 等。这些框架都有各自的特点和使用方式,开发人员可以根据自己的需求选择适合的框架。 总之,WPF MVVM 开源框架为开发人员提供了一种更高效和可维护的方式来开发 WPF 应用程序,通过封装好的代码和组件,帮助开发人员更快速地完成开发任务。同时,开源框架也为开发人员提供了学习和分享的平台,可以促进技术的进步和共享。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值