vs2015
mvvmlight(nuget 下载最新版)express
在个人程序中跑了一个新线程
Thread.Start()框架
关闭wpf窗口时,线程里的程序没跑完,则线程继续在后台跑
那就在主窗体的.cs文件中加入mvvm
protected override void OnClosed(EventArgs e) { base.OnClosed(e); Environment.Exit(0); }
在要用到mvvmlight的工种中添加mvvmlight以后
项目中会多出一个ViewModel文件夹
里面有两个文件:
MainViewModel.cs
ViewModelLocator.cside
而原先的App.xaml里也会多出几行东东:函数
<Application x:Class="WpfApp2.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp2" StartupUri="View/WelcomeView.xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" d1p1:Ignorable="d" xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006"> <Application.Resources> <ResourceDictionary> <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:WpfApp2.ViewModel" /> </ResourceDictionary> </Application.Resources> </Application>
即上面代码中的学习
<ResourceDictionary>
标签里的内容
这个先无论
直接运行一下,会报错
在ViewModel文件夹下的ViewModelLocator.cs里会报错
这个错误经过搜索发现了解决方法:
将这行报错的引用注释掉或删掉,添加另外一行引用
即this
using CommonServiceLocator;
这样就不报错了spa
类继承ObservableObject
固然要引用相应的包线程
using GalaSoft.MvvmLight;
这个按个人理解就是继承了这个类,属性的变化就可响应,而且从UI中反应出来
即属性有变更时,UI中也相应更新code
在窗体的xaml.cs中,在构造函数里要定义数据绑定时的上下文
this.DataContext = ……;
在xaml文件里绑定
<TextBlock Text="{Binding Welcom.Introduction}" FontSize="30"></TextBlock>
开一个新线程,里面隔必定时间,给绑定的数据更新,这样在窗口中的TextBlock里也会随之更新内容