理论:在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文章来源:https://www.toymoban.com/news/detail-530423.html
<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拿到并显示出来文章来源地址https://www.toymoban.com/news/detail-530423.html
到了这里,关于wpf 在MVVM中使用IOC思想通过注册窗体的方法实现在ViewModel里面打开其他窗口的方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!