WPF项目学习.一
WPF項(xiàng)目搭建
版權(quán)聲明:本文為博主初學(xué)經(jīng)驗(yàn),未經(jīng)博主允許不得轉(zhuǎn)載。
一、前言
? 記錄在學(xué)習(xí)與制作WPF過(guò)程中遇到的解決方案。
使用MVVM的優(yōu)點(diǎn)是 數(shù)據(jù)和視圖分離,雙向綁定,低耦合,可重用行,相對(duì)獨(dú)立的設(shè)計(jì)和邏輯;
?
二、配置
系統(tǒng)環(huán)境:win10
開(kāi)發(fā)工具:Visual Studio 2017
開(kāi)發(fā)語(yǔ)言:C#.WPF (MVVM框架)
數(shù)據(jù)庫(kù):SQLiteStudio
?
三、附件
- vs_enterprise.exe? ?在線安裝 Visual Studio 2017 開(kāi)發(fā)工具;
- SQLiteStudio.zip? ? ?免安裝Sqlite輕量數(shù)據(jù)庫(kù)操作工具;
- WPF-MVVM-Work.zip 項(xiàng)目源代碼;
四、步驟
1. 創(chuàng)建項(xiàng)目;
2. 文件夾層級(jí);
建文件夾:Model, ViewModel, View, Resources, Lib, Service, Common;
Model是模型層,屬性類(lèi)存放的地方;也就是存放不涉及業(yè)務(wù)邏輯的代碼;
例如:列表元素,接口參數(shù),枚舉,數(shù)據(jù)交互的參數(shù)模型和View基礎(chǔ)元素屬性等等;
ViewModel是視圖模型層,是MVVM實(shí)現(xiàn)業(yè)務(wù)邏輯代碼的地方;
例如:操作視圖界面數(shù)據(jù)的呈現(xiàn),界面按鈕觸發(fā)的事件和操作數(shù)據(jù)庫(kù)前后對(duì)界面交互的事件;
View是視圖層,是窗體布局實(shí)現(xiàn)的地方;也就是呈現(xiàn)給用戶交互使用的界面窗體;
例如:登錄頁(yè)面,查詢頁(yè)面,新增和編輯的頁(yè)面等;
Resources是資源庫(kù)層,里面存放聲音,圖片和樣式布局等統(tǒng)一調(diào)用外部資源的地方;
Lib是引用層,放置一些第三方引用便于調(diào)用,也可以用nuget統(tǒng)一管理第三方dll;
Service是服務(wù)層,是實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)或者對(duì)站點(diǎn)接口的操作,進(jìn)行數(shù)據(jù)持久化和數(shù)據(jù)交互;
Common是工具層,是存放一些公共的代碼,統(tǒng)一調(diào)用,簡(jiǎn)潔業(yè)務(wù)邏輯代碼的冗余;
注:可以建文件夾,但更推薦新建類(lèi)庫(kù)放置以上層級(jí);
3. 控件的使用與布局;
3.1 從工具箱中直接拖拉控件;
3.2 直接在代碼中編輯控件代碼;
4. 代碼關(guān)聯(lián);
4.1 View與ViewModel的交互關(guān)聯(lián):
- View后臺(tái)的代碼關(guān)聯(lián)
public MainWindow() //類(lèi)名 {InitializeComponent();//后臺(tái)代碼有這句就實(shí)現(xiàn)了View和ViewModel的綁定DataContext = new MainWindowViewModel();
}
?
- 文本的綁定?
前端:<TextBox Text="{Binding TxtInput}"/>后端:
public string TxtInput
{
get => _txtInput;
set
{
_txtInput = value;
//用RaisePropertyChanged刷新前端綁定控件的輸入框文本內(nèi)容
RaisePropertyChanged("TxtInput");?
}
}
private string _txtInput;
?
- 事件的綁定
前端:<Button Content="添加" Command="{Binding BtnAddContent}"/>后端:
public MainWindowViewModel()?
{
//按鈕事件與業(yè)務(wù)的銜接? ?也可以在BtnAddContent的Set中寫(xiě)
BtnAddContent = new RelayCommand(AddContent);
}
//前端綁定的 添加按鈕 Command事件
public RelayCommand BtnAddContent { get; set; }
private void AddContent()
{
? //按鈕事件處理的業(yè)務(wù)邏輯代碼
}
?
- 樣式的綁定
<TextBox Style="{StaticResource TxbTrigger}" Tag="序號(hào)..." /><Button Content="添加" Template="{StaticResource DefaultButton}" />”TxbTrigger是輸入框水印樣式資源,DefaultButton是按鈕樣式資源模板;詳細(xì)樣式代碼,查閱源碼中的Resources-Style;“
4.2 數(shù)據(jù)綁定和命令綁定的代碼(如果用第三方框架引用,就不需要編寫(xiě)以下兩個(gè)方法類(lèi))
//數(shù)據(jù)綁定public class ViewModelBase : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;protected void RaisePropertyChanged<T>(Expression<Func<T>> action){var propertyName = GetPropertyName(action);RaisePropertyChanged(propertyName);}private static string GetPropertyName<T>(Expression<Func<T>> action){var expression = (MemberExpression)action.Body;var propertyName = expression.Member.Name;return propertyName;}public void RaisePropertyChanged(string propertyName){if (PropertyChanged != null)PropertyChanged(this,new PropertyChangedEventArgs(propertyName));}} //命令綁定
public class RelayCommand : ICommand{public Action ExecuteAction; //執(zhí)行方法public Action<object> ExecuteCommand; //執(zhí)行方法 帶參數(shù)public Func<object, bool> CanExecuteCommand; //執(zhí)行方法的條件public RelayCommand(Action action)// 執(zhí)行事件 {ExecuteAction = action;}
//執(zhí)行帶參數(shù)的事件public RelayCommand(Action<object> action) {ExecuteCommand = action;}
//根據(jù)條件執(zhí)行帶參數(shù)的事件public RelayCommand(Action<object> action, Func<object, bool> can) {ExecuteCommand = action;CanExecuteCommand = can;}
//當(dāng)命令可執(zhí)行狀態(tài)發(fā)生改變時(shí),應(yīng)被激活public event EventHandler CanExecuteChanged;
//用于判斷命令是否可以執(zhí)行
public bool CanExecute(object parameter) {if (ExecuteAction != null) return true;return CanExecuteCommand == null || CanExecuteCommand(parameter);}
//命令執(zhí)行public void Execute(object parameter) {if (ExecuteCommand != null) ExecuteCommand(parameter);else ExecuteAction();}}
ViewModel的業(yè)務(wù)類(lèi)需要繼承ViewModelBase
public class MainWindowViewModel : ViewModelBase前端的DataGrid綁定的數(shù)據(jù)需要用ObservableCollection類(lèi)型定義列表;
public ObservableCollection<AddModel> AddContent{get => _addContent;set{_addContent = value;RaisePropertyChanged("AddContent"); }}private ObservableCollection<AddModel> _addContent =new ObservableCollection<AddModel>();
"AddModel"是Model中的屬性類(lèi);代表DataGrid中綁定的列名指向;
DataGrid前端代碼:
<DataGrid x:Name="DgTimes"ItemsSource="{Binding AddContent}" AutoGenerateColumns="False"SelectedItem="{Binding SelectTime,UpdateSourceTrigger=PropertyChanged}"><DataGrid.InputBindings>
<!--雙擊事件--><MouseBinding Gesture="LeftDoubleClick" Command="{Binding DgDoubleClick}" CommandParameter="{Binding ElementName=DgTimes,Path=SelectedItem}"/><!--單擊事件,也可以在SelectedItem選中事件中的set屬性編輯業(yè)務(wù)代碼-->
<MouseBinding Gesture="LeftClick" Command="{Binding DgClick}"CommandParameter="{Binding ElementName=DgTimes,Path=SelectedItem}"/></DataGrid.InputBindings><DataGrid.Columns><DataGridTextColumn Header="序號(hào)" Binding="{Binding RowIndex}"/><DataGridTemplateColumn Header="勾選?" MinWidth="30"><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid>
<!--圖片的綁定,注意不能為null或者空值,不然會(huì)加載超慢--><Image Width="20" Height="20" Source="{Binding Photo}"/></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTextColumn Header="輸入內(nèi)容" Binding="{Binding AddContent}"/><DataGridTextColumn Header="時(shí)間" Binding="{Binding AddTime}"/></DataGrid.Columns> </DataGrid>
?注:由于整個(gè)制作項(xiàng)目的操作視頻文件過(guò)大,如有所需,留郵箱地址給博主;
5. 執(zhí)行效果;
?
五、注意事項(xiàng)
本次項(xiàng)目制作并沒(méi)有使用數(shù)據(jù)庫(kù)的操作;源代碼中也是沒(méi)有!
源碼中包括了對(duì)輸入框限制數(shù)字的輸入方法;
源碼中包括了Button,TextBox,CheckBox,DataGrid等等樣式資源代碼;
有些網(wǎng)友推薦 MVVMLight 或者 Prism 等第三方MVVM框架引用;
?
?六、下篇預(yù)告
在制作WPF過(guò)程中遇到的問(wèn)題以及解決方案;
其中包括:焦點(diǎn)的控制,鍵盤(pán)事件觸發(fā),輸入框的數(shù)字限制,異步處理,隱藏狀態(tài)可用狀態(tài),自定義屬性等等...
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/yjwlogs/p/8459751.html
總結(jié)
- 上一篇: 绝地求生大逃杀,改配置
- 下一篇: .Net开发人员应该下载的十种必备工具(