日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

C# wpf编程CM框架快速入门项目实例

發(fā)布時(shí)間:2023/12/4 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# wpf编程CM框架快速入门项目实例 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

01

事件連接

這會(huì)自動(dòng)將控件上的事件關(guān)聯(lián)到ViewModel上的方法。

常規(guī)約定:

<Button x:Name="Save">

這將導(dǎo)致按鈕的單擊事件調(diào)用ViewModel上的“Save”方法。

簡(jiǎn)短語(yǔ)法:

<Button cal:Message.Attach="Save">

這將再次導(dǎo)致按鈕的“Click”事件調(diào)用ViewModel上的“Save”方法。

可以像這樣使用不同的事件:

<Button cal:Message.Attach="[Event MouseEnter] = [Action Save]">

可以向方法傳遞不同的參數(shù),如下所示:

<Button cal:Message.Attach="[Event MouseEnter] = [Action Save($this)]">

長(zhǎng)語(yǔ)法

<UserControl x:Class="Caliburn.Micro.CheatSheet.ShellView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:cal="http://www.caliburnproject.org"> <StackPanel> <TextBox x:Name="Name" /><Button Content="Save"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <cal:ActionMessage MethodName="Save"> <cal:Parameter Value="{Binding ElementName=Name, Path=Text}" /> </cal:ActionMessage> </i:EventTrigger> </i:Interaction.Triggers> </Button> </StackPanel> </UserControl>

此語(yǔ)法表達(dá)式對(duì)于Blend?比較友好。

02


數(shù)據(jù)綁定

這將自動(dòng)將控件上的依賴項(xiàng)屬性綁定到ViewModel上的屬性。

常規(guī)約定:

<TextBox x:Name="FirstName" />

將導(dǎo)致TextBox的“Text”屬性綁定到ViewModel的“FirstName”屬性。

明確的寫法:

<TextBox Text="{Binding Path=FirstName, Mode=TwoWay}" />

這是綁定屬性的正常方式。

03


項(xiàng)目實(shí)例

前臺(tái)XAML文件:

<Window x:Class="WpfApp8.StartView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:cal="http://www.caliburnproject.org" xmlns:local="clr-namespace:WpfApp8"mc:Ignorable="d"Title="StartView" Height="300" Width="600" WindowStartupLocation="CenterScreen"><StackPanel><TextBox Name="TextContent"/><Button x:Name="testBtn" Content="testBtn" Background="LightCyan"/><ListBox Name="ListBoxItems" MinHeight="230" Background="LightGray"cal:Message.Attach="[Event SelectionChanged] = [Action ListBoxItems_SelectionChanged($source,$eventArgs)];[Event MouseUp]=[ListBoxItems_MouseUp($source,$eventArgs)]" /></StackPanel> </Window>

后臺(tái)viemmodel

using Caliburn.Micro; using System.Collections.ObjectModel; using System.Windows; using System.Windows.Controls; using System.Windows.Input;namespace WpfApp8 {class StartViewModel : Screen{ public StartViewModel(){ListBoxItems = new ObservableCollection<string>() { };ListBoxItems.Add("dotNet編程大全");ListBoxItems.Add("Csharp編程大全");ListBoxItems.Add("dotnet工控上位機(jī)編程");}public ObservableCollection<string> ListBoxItems { get; set; }public string TextContent { get; set; }public void testBtn(){TextContent = "hello world!";NotifyOfPropertyChange(()=> TextContent);}public void ListBoxItems_MouseUp(object sender, MouseButtonEventArgs e){ListBox listbox = sender as ListBox;MessageBox.Show("當(dāng)前操作的控件名稱是:"+ listbox.Name);}public void ListBoxItems_SelectionChanged(object sender, SelectionChangedEventArgs e){TextContent = (sender as ListBox).SelectedItem.ToString();NotifyOfPropertyChange("TextContent");}} }

04


運(yùn)行結(jié)果

05


源碼下載

百度網(wǎng)盤鏈接:?? ? ?

鏈接:https://pan.baidu.com/s/1G8aOfOnZ03dGyVFErUiB6Q

提取碼:1314

小編微信:mm1552923 ??

公眾號(hào):dotNet編程大全? ? ??

總結(jié)

以上是生活随笔為你收集整理的C# wpf编程CM框架快速入门项目实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。