本文將為大家詳細(xì)介紹mvvm框架中icommand的用法,代碼詳細(xì)步驟清晰,細(xì)節(jié)處理妥當(dāng),希望大家通過(guò)這篇文章有所收獲,我們先來(lái)看看ICommand接口類(lèi)的實(shí)現(xiàn):
創(chuàng)新互聯(lián)建站2013年開(kāi)創(chuàng)至今,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、成都做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元阿巴嘎做網(wǎng)站,已為上家服務(wù),為阿巴嘎各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):028-86922220
public class RelayCommand : ICommand
{
private Action<object> _execute;
private Predicate<object> _canExecute;
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
this._execute = execute;
this._canExecute = canExecute;
}
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
}
remove
{
CommandManager.RequerySuggested -= value;
}
}
public bool CanExecute(object parameter)
{
return _canExecute(parameter);
}
public void Execute(object parameter)
{
_execute(parameter);
}
}
在viewmodel中添加
void UpdateExecute()
{
Console.WriteLine("ICommandExecute");
}
bool CanUpdateExecute()
{
return true;
}
private ICommand _doSomething;
public ICommand DoSomething
{
get
{
if (_doSomething == null)
{
_doSomething = new RelayCommand(p => this.UpdateExecute(), p => this.CanUpdateExecute());
}
return _doSomething;
}
}
在xaml中用Command來(lái)綁定
假設(shè)我們用的是RadioButton
<RadioButton Content="{Binding Content}" IsChecked="{Binding IsCheck}" GroupName="RadioButtons"
Command="{Binding DataContext.DoSomething,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=StackPanel}}"></RadioButton>
注意:
Binding DataContext.DoSomething
這里要用DataContext.
然后要設(shè)置一下RelativeSource
不然找不到這個(gè)方法會(huì)輸出錯(cuò)誤信息
關(guān)于ICommand接口類(lèi)的實(shí)現(xiàn)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
文章名稱(chēng):mvvm框架中icommand的用法
本文來(lái)源:http://www.chinadenli.net/article10/iijido.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、網(wǎng)站收錄、云服務(wù)器、建站公司、App開(kāi)發(fā)、軟件開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)