本文將為大家詳細介紹mvvm框架中icommand的用法,代碼詳細步驟清晰,細節(jié)處理妥當,希望大家通過這篇文章有所收獲,我們先來看看ICommand接口類的實現(xiàn):
創(chuàng)新互聯(lián)建站2013年開創(chuàng)至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站建設(shè)、成都做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元阿巴嘎做網(wǎng)站,已為上家服務(wù),為阿巴嘎各地企業(yè)和個人服務(wù),聯(lián)系電話: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來綁定
假設(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
不然找不到這個方法會輸出錯誤信息
關(guān)于ICommand接口類的實現(xiàn)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
文章名稱:mvvm框架中icommand的用法
本文來源:http://aaarwkj.com/article10/iijido.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機、網(wǎng)站收錄、云服務(wù)器、建站公司、App開發(fā)、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)