概述
WPF MVVM Light使用示例
示例一:
三个控件同时绑定了一个变量(点击按钮通过MessageBox显示变量值),通过NuGet下载MVVM Light:
MainViewModel类代码:
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
namespace WPF0416.ViewModel
{
public class MainViewModel : ViewModelBase
{
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
Temp = 10;
getTemp = new RelayCommand(ShowTemp);
}
private int temp;
public int Temp
{
get { return temp; }
set { temp = value; RaisePropertyChanged();}
}
private RelayCommand getTemp;
public RelayCommand GetTemp
{
get { return getTemp; }
set { getTemp = value; }
}
private void ShowTemp()
{
System.Windows.MessageBox.Show(Temp.ToString());
}
}
}
主窗体XAML代码:
<Window x:Class="WPF0416.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<Button Content="Show" FontSize="20" Margin="5" Command="{Binding GetTemp}"></Button>
<Slider Margin="5" Minimum="0" Maximum="100" Value="{Binding Temp}"/>
<TextBox Margin="5" Text="{Binding Temp}"></TextBox>
</StackPanel>
</Grid>
</Window>
主窗体后台代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel();
}
}
示例二:绑定实体类数据
添加一个文件夹,在里面添加一个类MyData,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WPF0416.Models
{
public class MyData
{
public int NumberOne { get; set; }
public int NumberTwo { get; set; }
public int NumberThree { get; set; }
}
}
MainViewModel类:
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using WPF0416.Models;
namespace WPF0416.ViewModel
{
public class MainViewModel : ViewModelBase
{
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
Temp = new MyData() { NumberOne= 10,NumberTwo = 30,NumberThree = 60};
getTemp = new RelayCommand(ShowTemp);
}
private MyData temp;
public MyData Temp
{
get { return temp; }
set { temp = value; RaisePropertyChanged();}
}
private RelayCommand getTemp;
public RelayCommand GetTemp
{
get { return getTemp; }
set { getTemp = value; }
}
private void ShowTemp()
{
System.Windows.MessageBox.Show(Temp.NumberOne.ToString());
}
}
}
主窗体XAML代码:
<Window x:Class="WPF0416.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<StackPanel DataContext="{Binding Temp}">
<Slider Margin="5" Minimum="0" Maximum="100" Value="{Binding NumberOne}"/>
<Slider Margin="5" Minimum="0" Maximum="100" Value="{Binding NumberTwo}"/>
<Slider Margin="5" Minimum="0" Maximum="100" Value="{Binding NumberThree}"/>
<TextBox Margin="5" Text="{Binding NumberOne}"></TextBox>
</StackPanel>
<Button Content="Show" FontSize="20" Margin="5" Command="{Binding GetTemp}"></Button>
</StackPanel>
</Grid>
</Window>
主窗体后台代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel();
}
}
运行效果:
示例三:主窗体向子窗体传数据
添加一个子窗体MonitorWindow,XAML代码如下:
<Window x:Class="WPF0416.MonitorWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MonitorWindow" Height="300" Width="300">
<Grid>
<StackPanel>
<TextBlock Text="{Binding Data}" FontSize="20" Margin="10"></TextBlock>
</StackPanel>
</Grid>
</Window>
在ViewModel文件夹创建一个MonitorViewModel类,并添加一个属性:
using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WPF0416.ViewModel
{
public class MonitorViewModel : ViewModelBase
{
public MonitorViewModel()
{
}
private int data;
public int Data
{
get { return data; }
set { data = value; RaisePropertyChanged(); }
}
}
}
主窗体的MainViewModel类:
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.Threading.Tasks;
namespace WPF0416.ViewModel
{
public class MainViewModel : ViewModelBase
{
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
OpenWindow = new RelayCommand(ShowWindow);
int index = 10;
Task.Run(() =>
{
while(true)
{
System.Threading.Thread.Sleep(500);
index++;
MonitorModel.Data = index;
}
});
}
private RelayCommand openWindow;
public RelayCommand OpenWindow
{
get { return openWindow; }
set { openWindow = value; }
}
private void ShowWindow()
{
MonitorWindow objMonitor = new MonitorWindow();
objMonitor.DataContext = MonitorModel;
objMonitor.ShowDialog();
}
private MonitorViewModel monitorModel = new MonitorViewModel();
public MonitorViewModel MonitorModel
{
get { return monitorModel; }
set { monitorModel = value; }
}
}
}
主窗体XAML代码:
<Window x:Class="WPF0416.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<Button Content="New Window" FontSize="20" Margin="5" Command="{Binding OpenWindow}"></Button>
</StackPanel>
</Grid>
</Window>
主窗体后台代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel();
}
}
程序集文件:
运行效果:
点击NewWindow按钮,子窗体弹出,数据累加;
示例四、ViewModelLocator的使用
View层xaml代码:
<Window x:Class="MVVMDemo1109.Views.StudentView"
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:MVVMDemo1109.Views"
mc:Ignorable="d"
Title="StudentView" Height="250" Width="300"
DataContext="{Binding Source={StaticResource Locator},Path=Student}">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="{Binding StudentModel.Id}" FontSize="16"/>
<TextBlock Text="{Binding StudentModel.Name}" FontSize="16"/>
</StackPanel>
</Window>
Model层StudentModel类:
//数据对象
public class StudentModel:ObservableObject
{
private int _id;
public int Id
{
get => _id;
set { _id = value; RaisePropertyChanged(() => Id); }
}
private string _name;
public string Name
{
get => _name;
set { _name = value; RaisePropertyChanged(() => Name); }
}
}
ViewModel层StudentViewModel:
public class StudentViewModel: ViewModelBase
{
public StudentViewModel()
{
if (IsInDesignMode)
{
//设计模式
StudentModel = new Models.StudentModel() { Id = 1002, Name = "Danny" };
}
else
{
//从数据库获取
StudentModel = new Models.StudentModel() { Id = 1003, Name = "Eric" };
}
}
public Models.StudentModel StudentModel { get; set; }
}
在ViewModelLocator.cs中注册StudentViewModel元素:
public class ViewModelLocator
{
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
if (ViewModelBase.IsInDesignModeStatic)
{
// Create design time view services and models
SimpleIoc.Default.Register<IDataService, DesignDataService>();
}
else
{
// Create run time view services and models
SimpleIoc.Default.Register<IDataService, DataService>();
}
//注册实例对象,注册到ioc容器中
SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<StudentViewModel>();
}
public MainViewModel Main
{
get
{
//通过容器获取实例对象
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
public StudentViewModel Student
{
get
{
//通过容器获取实例对象
return ServiceLocator.Current.GetInstance<StudentViewModel>();
}
}
public static void Cleanup()
{
// TODO Clear the ViewModels
}
}
最后
以上就是含蓄滑板为你收集整理的WPF MVVM Light使用示例的全部内容,希望文章能够帮你解决WPF MVVM Light使用示例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复