A problem recurs
1. In the latest version of the v5.1 mvvmlight (in fact, the problem is very early), found that canexecute can not work very well. A simple project, only MainWindow and Mainwindowviewmodel, the code is as follows
MainWindow.xaml
<Windowx:class= "Mvvmlighterror.mainwindow"xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"Title= "MainWindow"Height= " the"Width= "525"> <Grid> <ButtonWidth= "+"Height= "+"Content= "OK"Command="{Binding Okcommand}"></Button> <ButtonWidth= "+"Height= "+"Margin= "0,100,0,0"Content= "Changeenable"Command="{Binding Changeenablecommand}"></Button> </Grid></Window>
MainWindow.xaml.cs
Public Partial class Mainwindow:window { public MainWindow () { InitializeComponent (); This New Mainwindowviewmodel (); } }
MainWindowViewModel.cs
Public classMainwindowviewmodel:viewmodelbase {Private BOOL_isenabled; PublicRelaycommand Okcommand {Get; Set; } PublicRelaycommand Changeenablecommand {Get; Set; } PublicMainwindowviewmodel () {Okcommand=NewRelaycommand (Okcommandhanlder, Canokcommandexecute); Changeenablecommand=NewRelaycommand (Changeenablecommandhandler); } Private voidOkcommandhanlder () {}Private BOOLCanokcommandexecute () {return_isenabled; } Private voidChangeenablecommandhandler () {_isenabled= !_isenabled; } }
However, to execute the code, click the Changeenable button and the OK button will not change the available state.
OK, let's not change the code, change the assembly target framework from the original. net4.5 to. net4.0, remove the reference to Mvvmlighttoolkit accordingly, and re-add the corresponding version of Mvvmlighttoolkit
Pm> Install-package Mvvmlight
To execute the code, click the Changeenable button and the OK button can change the available status accordingly.
Two issues
The first is to Mvvmlighttoolkit official website to find relevant information, and then the release of the log to see the following important records
There's a solution, but why?
So I went to Mvvmlight's official website.
Find a description of the two key sections below
As a result of the humility of the foreigner, and the reason for the problem, the author wants to transplant the mvvmlight framework into a PCL, but the PCL has a lot of limitations and there is no commandmanager, so it is not possible to provide a "command whether control control is available" function, but then gives a solution
That's where. Modify the namespace GalaSoft.MvvmLight.Command to GALASOFT.MVVMLIGHT.COMMANDWPF when Net4.5 or later uses the corresponding Mvvmlight version.
Here, the ins and outs and the solution are clear.
Record, hoping to help the same pit students.
http://mvvmlight.codeplex.com/workitem/7659
Demo Download: Mvvmlighterror.rar
Mvvmlight ToolKit. Net4.5 version CanExecute cannot refresh interface bug