概述
参考:
https://www.cnblogs.com/wzh2010/p/6285990.html
https://www.cnblogs.com/manupstairs/p/13661227.html
http://cn.voidcc.com/question/p-wuerhmow-rw.html
dll:
一、Models
1.StaffModel.cs
using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Text;
namespace WpfApp33.Models
{
public class StaffModel : ObservableObject
{
private string id;
/// <summary>
/// 身份证
/// </summary>
public string ID
{
get { return id; }
set { id = value; RaisePropertyChanged(() => ID); }
}
private string name;
/// <summary>
/// 姓名
/// </summary>
public string Name
{
get { return name; }
set { name = value; RaisePropertyChanged(() => Name); }
}
}
}
二、ViewModels
1.StaffViewModel.cs
using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Text;
using WpfApp33.Models;
namespace WpfApp33.ViewModels
{
public class StaffViewModel : ViewModelBase
{
public StaffViewModel()
{
Staff = new StaffModel() { ID = "xxx22119990815xxxx", Name = "张xx" };
}
#region 属性
private StaffModel staff;
/// <summary>
/// 职员属性
/// </summary>
public StaffModel Staff
{
get { return staff; }
set { staff = value; RaisePropertyChanged(() => Staff); }
}
#endregion
}
}
2.ViewModelLocator.cs
/*
In App.xaml:
<Application.Resources>
<vm:ViewModelLocator xmlns:vm="clr-namespace:MVVMLightDemo"
x:Key="Locator" />
</Application.Resources>
In the View:
DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
You can also use Blend to do all this with the tool's support.
See http://www.galasoft.ch/mvvm
*/
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using WpfApp33.ViewModels;
namespace WpfApp33.ViewModels
{
/// <summary>
/// This class contains static references to all the view models in the
/// application and provides an entry point for the bindings.
/// </summary>
public class ViewModelLocator
{
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
public ViewModelLocator()
{
#region Code Example
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>();
}
#endregion
//SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<StaffViewModel>();
}
#region 实例化
//public MainViewModel Main
//{
// get
// {
// return SimpleIoc.Default.GetInstance<MainViewModel>();
// }
//}
public StaffViewModel Staff
{
get
{
return SimpleIoc.Default.GetInstance<StaffViewModel>();
}
}
#endregion
public static void Cleanup()
{
// TODO Clear the ViewModels
}
}
}
三、View
1.StaffView.xaml
<Window x:Class="WpfApp33.Views.StaffView"
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"
mc:Ignorable="d"
DataContext="{Binding Source={StaticResource Locator},Path=Staff}"
Title="StaffView" Height="450" Width="800" >
<Grid>
<StackPanel>
<TextBlock Text="{Binding Staff.ID}" Height="30" Width="200" Background="LightBlue"/>
<TextBlock Text="{Binding Staff.Name}" Height="30" Width="200" Background="LightCoral"/>
</StackPanel>
</Grid>
</Window>
四、解决方法资源管理器
值得注意的是:
1.App.xaml 中需要将ViewModelLocator添加为应用程序资源"Locator",否则StaffView.xaml中无法直接静态引用。
2.App.xaml中需要设置StartupUri为Views/StaffView.xaml,否则看不到StaffView.xaml。
<Application x:Class="WpfApp33.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp33"
xmlns:vm="clr-namespace:WpfApp33.ViewModels"
StartupUri="Views/StaffView.xaml">
<Application.Resources>
<vm:ViewModelLocator x:Key="Locator" />
</Application.Resources>
</Application>
3.DLL路径:
C:Userswithyzu.nugetpackagesmvvmlightlibsstd105.4.1.1libnetstandard1.0GalaSoft.MvvmLight.dll
最后
以上就是勤奋鸵鸟为你收集整理的WPF .NET Core应用程序 简单使用MvvmLight的全部内容,希望文章能够帮你解决WPF .NET Core应用程序 简单使用MvvmLight所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复