国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

WPF采用MVVM模式(綁定:純前臺、命令:觸發(fā)器綁定命令)

2019-11-17 02:14:05
字體:
供稿:網(wǎng)友

WPF采用MVVM模式(綁定:純前臺、命令:觸發(fā)器綁定命令)

MVVM綁定

view-viewModel-model,模型介紹省略,就是創(chuàng)建類,添加字段封裝屬性。注:控件的綁定只能綁定到屬性上,不能綁定到字段上;

接下來就是代碼

(view):

 1 <Window x:Class="WpfBing.MainWindow" 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/

說明:

 xmlns:vm="clr-namespace:WpfBing"添加對命名空間的引用,主要是讓前臺頁面能夠?qū)ふ业絭iewmodel的命名空間;
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 添加wpf的命名控件引用,主要是用來提供使用該命名空間中的觸發(fā)器綁定命令該類的下載鏈接為:System.Windows.Interactivity
<Grid.DataContext>   <vm:ViewModel/> </Grid.DataContext> 數(shù)據(jù)源綁定,將該空間按的數(shù)據(jù)源綁定為vm空間下的ViewModel對象上;注:純前臺綁定的關(guān)鍵
 <i:Interaction.Triggers>    <i:EventTrigger EventName="TextChanged">       <i:InvokeCommandAction Command="{Binding NameChanged}" />     </i:EventTrigger></i:Interaction.Triggers>通過觸發(fā)器實現(xiàn)對控件事件的命令綁定,該代碼需要添加System.Windows.Interactivity.dll的引用

(BaseClass):

 1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using System.Windows.Input; 8  9 namespace WpfBing10 {11     public class RelayCommand : ICommand12     {13         #region 字段14         readonly Func<Boolean> _canExecute;15         readonly Action _execute;16         #endregion17 18         #region 構(gòu)造函數(shù)19         public RelayCommand(Action execute)20             : this(execute, null)21         {22         }23 24         public RelayCommand(Action execute, Func<Boolean> canExecute)25         {26             if (execute == null)27                 throw new ArgumentNullException("execute");28             _execute = execute;29             _canExecute = canExecute;30         }31         #endregion32 33         #region ICommand的成員34         public event EventHandler CanExecuteChanged35         {36             add37             {38 39                 if (_canExecute != null)40                     CommandManager.RequerySuggested += value;41             }42             remove43             {44 45                 if (_canExecute != null)46                     CommandManager.RequerySuggested -= value;47             }48         }49 50         [DebuggerStepThrough]51         public Boolean CanExecute(Object parameter)52         {53             return _canExecute == null ? true : _canExecute();54         }55 56         public void Execute(Object parameter)57         {58             _execute();59         }60         #endregion61     }62 }

說明:該段代碼主要實現(xiàn)ICommand命令,實現(xiàn)該命令接口,通過委托調(diào)用調(diào)用ViewModel中相應(yīng)的方法;

ICommand主要有兩個方法,Excute,CanExcute,一個是調(diào)用的實現(xiàn)方法,一個是判斷是否執(zhí)行該調(diào)用方法;

注:功能上可以用來控制按鈕或其他,控件狀態(tài)是否可用

(viewmodel):

using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Input;namespace WpfBing{    public class ViewModel:INotifyPropertyChanged    {        public event PropertyChangedEventHandler PropertyChanged;        public void Notify(string name)        {            if (PropertyChanged != null)            {                PropertyChanged(this, new PropertyChangedEventArgs(name));            }        }        private string name = "測試數(shù)據(jù)";        public string Name        {            get { return name; }            set            {                name = value;                Notify("Name");            }        }        void UpdateArtistNameExecute()        {            this.Name = "中孝介";        }        bool CanUpdateArtistNameExecute()        {            return true;        }        public ICommand UpdateData { get { return new RelayCommand(UpdateArtistNameExecute, CanUpdateArtistNameExecute); } }        public ICommand NameChanged { get { return new RelayCommand(NameChang); } }        private void NameChang()        {            string na = Name;        }    }}

說明:viewmodel中就是對一些事件流,數(shù)據(jù)流的控制了通過對數(shù)據(jù),控制可以實現(xiàn)刷新前臺數(shù)據(jù),命令控制,可以訪問業(yè)務(wù)層,等下層業(yè)務(wù)等;


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 灵山县| 和平区| 宜州市| 泊头市| 澎湖县| 阳西县| 兴化市| 怀集县| 琼中| 宝清县| 封丘县| 桐梓县| 诸暨市| 广饶县| 略阳县| 大兴区| 家居| 武强县| 泰宁县| 万全县| 镇坪县| 宁化县| 志丹县| 青阳县| 墨竹工卡县| 赤水市| 甘肃省| 天水市| 磐石市| 永安市| 桐柏县| 连平县| 永春县| 互助| 聂拉木县| 乐陵市| 宝兴县| 吐鲁番市| 九江县| 义乌市| 介休市|