日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Silverlight实用窍门系列:63.Silverlight中的Command,自定义简单Command

發布時間:2023/12/19 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Silverlight实用窍门系列:63.Silverlight中的Command,自定义简单Command 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

    在Silverlight中的MVVM模式下將前臺頁面和ViewModel界面交互分離開是通過本節所要講述的Command實現的。我們自定義一個Command需要繼承于ICommand接口并且實現這個接口。它有CanExecute()、Execute()方法和CanExecuteChanged事件組成。

    ? CanExecute():判斷是否繼續執行操作。

    ? Execute():執行操作的內容。

    ? CanExecuteChanged:當出現影響是否應執行該命令的更改時發生。

    首先:自定義的一個btnCommand。

public class btnCommand:ICommand
{
private bool canExe;
/// <summary>
/// 構造函數設置是否執行操作
/// </summary>
/// <param name="canexe"></param>
public btnCommand(bool canexe)
{
this.canExe = canexe;
}

/// <summary>
/// 判斷是否執行操作
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
public bool CanExecute(object parameter)
{
if (canExe)
{
return true;
}
return false;
}

/// <summary>
/// 是否執行操作的變更發生時
/// </summary>
public event EventHandler CanExecuteChanged;

/// <summary>
/// 執行操作的內容,可以變為Action行為
/// </summary>
/// <param name="parameter"></param>
public void Execute(object parameter)
{
if (parameter != null)
{ MessageBox.Show(parameter.ToString()); }
else
{
MessageBox.Show("未設置CommandParameter");
}
}
}

    其次:定義一個ViewModel,并且在構造函數中初始化兩個Command屬性。

public class BtnViewModel
{
// 設置兩個命令
public ICommand BtnCommand { get; set; }
public ICommand BtnCommandTrue { get; set; }
public BtnViewModel()
{
//初始化兩個命令值
BtnCommand = new btnCommand(false);
BtnCommandTrue = new btnCommand(true);
}
}

    再次將ViewModel初始化為頁面數據源

<UserControl.DataContext>
<local:BtnViewModel />
</UserControl.DataContext>

    最后:前臺的兩個按鈕綁定Command

<Grid x:Name="LayoutRoot" Background="White">
<Button Content="第一個" Height="23" HorizontalAlignment="Left"
Command="{Binding BtnCommand}" CommandParameter="第一個Command"
Margin="94,80,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
<Button Command="{Binding BtnCommandTrue}" CommandParameter="第二個Command"
Content="第二個" Height="23" HorizontalAlignment="Left" Margin="94,140,0,0"
Name="button2" VerticalAlignment="Top" Width="75" />
</Grid>

    如需源碼請點擊?SLICommand.zip 下載,下面是效果圖。有一個按鈕CanExecute,有一個不能點擊。

轉載于:https://blog.51cto.com/chengxingliang/835766

總結

以上是生活随笔為你收集整理的Silverlight实用窍门系列:63.Silverlight中的Command,自定义简单Command的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。