wof mvvmlight 实现mvvm框架工程加代码事例
时间: 2023-12-07 08:18:20 浏览: 129
好的,以下是一个使用 WOF MVVMLight 实现 MVVM 框架的 WPF 工程示例:
1. 创建 WPF 项目,添加 WOF MVVMLight NuGet 包。
2. 创建 ViewModel 类,继承自 ViewModelBase,实现相应的业务逻辑。
```csharp
using GalaSoft.MvvmLight;
namespace MVVMLightDemo.ViewModels
{
public class MainViewModel : ViewModelBase
{
private string _text;
public string Text
{
get { return _text; }
set { Set(ref _text, value); }
}
}
}
```
3. 创建 View 类,使用 XAML 编写界面,并绑定 ViewModel。
```xaml
<Window x:Class="MVVMLightDemo.Views.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:MVVMLightDemo.Views"
xmlns:vm="clr-namespace:MVVMLightDemo.ViewModels"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBox Text="{Binding Text, Mode=TwoWay}" Margin="10"/>
</Grid>
</Window>
```
```csharp
using GalaSoft.MvvmLight.Messaging;
using System.Windows;
namespace MVVMLightDemo.Views
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModels.MainViewModel();
Messenger.Default.Register<NotificationMessage>(this, nm =>
{
if (nm.Notification == "ShowMessage")
{
MessageBox.Show(nm.Content.ToString());
}
});
}
}
}
```
4. 在 View 中使用 WOF MVVMLight 提供的命令绑定、消息传递等工具,实现与 ViewModel 的交互。
```xaml
<Window x:Class="MVVMLightDemo.Views.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:MVVMLightDemo.Views"
xmlns:vm="clr-namespace:MVVMLightDemo.ViewModels"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:cmd="http://www.galasoft.ch/mvvmlight"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBox Text="{Binding Text, Mode=TwoWay}" Margin="10"/>
<Button Content="Show Message" Margin="10" HorizontalAlignment="Right" VerticalAlignment="Bottom">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cmd:EventToCommand Command="{Binding ShowMessageCommand}" CommandParameter="Hello, MVVMLight!"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
</Window>
```
```csharp
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
namespace MVVMLightDemo.ViewModels
{
public class MainViewModel : ViewModelBase
{
private string _text;
public string Text
{
get { return _text; }
set { Set(ref _text, value); }
}
public RelayCommand<string> ShowMessageCommand { get; private set; }
public MainViewModel()
{
ShowMessageCommand = new RelayCommand<string>(msg =>
{
Messenger.Default.Send(new NotificationMessage(msg, "ShowMessage"));
});
}
}
}
```
5. 在 App.xaml 中配置 ViewModelLocator,以便于在 View 中使用 ViewModel。
```xaml
<Application x:Class="MVVMLightDemo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:MVVMLightDemo.ViewModels"
StartupUri="Views/MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True"/>
</ResourceDictionary>
</Application.Resources>
</Application>
```
6. 在 Main 方法中启动应用程序,并设置使用 ViewModelLocator 创建主窗口。
```csharp
using MVVMLightDemo.Views;
using System.Windows;
namespace MVVMLightDemo
{
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow mainWindow = new MainWindow();
mainWindow.DataContext = new ViewModels.MainViewModel();
mainWindow.Show();
}
}
}
```
7. 运行程序,查看效果。
以上就是一个使用 WOF MVVMLight 实现 MVVM 框架的 WPF 工程示例,可以根据需要进行自定义和扩展。
阅读全文