前言
我一直想組件化得去開(kāi)發(fā)WPF,因?yàn)槲矣X(jué)得將復(fù)雜問(wèn)題簡(jiǎn)單化是最好的
如何組件化開(kāi)發(fā)
主窗口引用
<Window x:Class="WpfApp1.MainWindow"
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:local="clr-namespace:WpfApp1"
xmlns:MD="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:Views="clr-namespace:WpfApp1.Views"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" >
<Window.Resources>
<Style x:Key="my_text" TargetType="TextBlock">
<Setter Property="FontSize" Value="30" />
<Setter Property="Margin" Value="8" />
</Style>
</Window.Resources>
<Window.DataContext >
<!--需要命名來(lái)指定數(shù)據(jù)源-->
<local:MainWindowViewModel x:Name="viewModel"/>
</Window.DataContext>
<Grid>
<!--不能直接寫TitleValue,Binding數(shù)據(jù)源會(huì)有問(wèn)題-->
<Views:ViewA Margin="10"
Title="{Binding ElementName=viewModel,Path=TitleValue}" />
</Grid>
</Window>
cs部分
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class MainWindowViewModel
{
public string TitleValue { get; set; } = "我是測(cè)試數(shù)據(jù)";
}
}
控件窗口
<UserControl.DataContext>
<local:ViewAViewModel />
</UserControl.DataContext>
<Grid>
<TextBlock Text="{Binding Title}" />
</Grid>
/// <summary>
/// ViewA.xaml 的交互邏輯
/// </summary>
public partial class ViewA : UserControl
{
public static readonly DependencyProperty TitleProperty;
/// <summary>
/// 為了拿到數(shù)據(jù)源需要定義一下
/// </summary>
private ViewAViewModel ViewModel = new ViewAViewModel();
public ViewA()
{
InitializeComponent();
ViewModel = (ViewAViewModel)DataContext;
}
static ViewA()
{
//靜態(tài)構(gòu)造
TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(ViewA),new PropertyMetadata("",
new PropertyChangedCallback((item, res) =>
{
//拿到數(shù)據(jù),再次賦值
var model =(ViewA)item;
model.ViewModel.Title = (string)res.NewValue;
})));
}
/// <summary>
/// 只是為了有代碼提示,添加依賴屬性后不會(huì)被調(diào)用
/// </summary>
public string Title { get; set; }
}
public partial class ViewAViewModel : ObservableObject
{
/// <summary>
/// 通知更新
/// </summary>
[ObservableProperty]
private string title = "ViewA Title!";
}
依賴注入代碼優(yōu)化
我將復(fù)雜的依賴注入的代碼進(jìn)行了優(yōu)化,減少了重復(fù)內(nèi)容的輸入。
//原代碼
TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(ViewA), new PropertyMetadata(default,
new PropertyChangedCallback((item, res) =>
{
//拿到數(shù)據(jù),再次賦值
var model = (ViewA)item;
model.ViewModel.Title = (string)res.NewValue;
})));
//新代碼
TitleProperty = DependencyPropertySet<ViewA, string>("Title", (view, value) =>
{
view.ViewModel.Title = value;
});
/// <summary>
/// 簡(jiǎn)化依賴注入代碼
/// </summary>
/// <typeparam name="View"></typeparam>
/// <typeparam name="Value"></typeparam>
/// <param name="name"></param>
/// <param name="action"></param>
/// <returns></returns>
public static DependencyProperty DependencyPropertySet<View,Value>(string name,Action<View,Value> action)
where View : class
{
var res= DependencyProperty.Register(name, typeof(Value), typeof(View), new PropertyMetadata(default,
new PropertyChangedCallback((item, res) =>
{
var model = item as View;
var value = (Value)res.NewValue;
if(model != null)
{
action(model, value);
}else
{
throw new Exception("model value is null");
}
})));
return res;
}
更新2023年12月9日
我現(xiàn)在依賴屬性擴(kuò)展封裝在一個(gè)靜態(tài)文件里面
namespace BlankApp1.Utils
{
public class MyWpfExtension<View> where View : class
{
/// <summary>
/// 簡(jiǎn)化依賴注入代碼
/// </summary>
/// <typeparam name="View"></typeparam>
/// <typeparam name="Value"></typeparam>
/// <param name="name"></param>
/// <param name="action"></param>
/// <returns></returns>
public DependencyProperty DependencyPropertySet<Value>(string name, Action<View, Value> action)
{
var res = DependencyProperty.Register(name, typeof(Value), typeof(View), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
new PropertyChangedCallback((item, res) =>
{
var model = item as View;
var value = (Value)res.NewValue;
if (model != null)
{
action(model, value);
}
else
{
throw new Exception("model value is null");
}
})));
return res;
}
}
}
簡(jiǎn)化后的依賴注入
記得UserControl.xaml里面綁定你ViewModel。這樣的話有代碼提示
<UserControl.DataContext>
<!--選擇你自己的ViewModel-->
<ViewModels:TitileViewModel />
</UserControl.DataContext>
namespace BlankApp1.Views
{
/// <summary>
/// TitleView.xaml 的交互邏輯
/// </summary>
public partial class TitleView : UserControl
{
//這個(gè)只是為了代碼提示,不涉及邏輯。屬性類型按照需求更改。
public MainWindow MainWindow { get; set; }
//初始化依賴屬性構(gòu)造器
public static readonly MyWpfExtension<TitleView> MyWpfExtension = new MyWpfExtension<TitleView>();
//這個(gè)是簡(jiǎn)化后的依賴屬性
public static readonly DependencyProperty MainWindowProperty =
MyWpfExtension.DependencyPropertySet<MainWindow>("MainWindow", (view, value) =>
{
view.TitileViewModel.MainWindow = value;
});
/// <summary>
/// DataContext的數(shù)據(jù)
/// </summary>
public TitileViewModel TitileViewModel { get; set; }
public TitleView()
{
InitializeComponent();
//拿到DataContext數(shù)據(jù)重定向
TitileViewModel = (TitileViewModel)DataContext;
TitileViewModel.UserName = "小王";
}
}
}
結(jié)尾
我后面要根據(jù)Vue的單向數(shù)據(jù)流,來(lái)構(gòu)思WPF 父子組件如何搭建。其實(shí)關(guān)鍵的就是數(shù)據(jù)流的問(wèn)題。根據(jù)Vue的單向數(shù)據(jù)流,父組件直接設(shè)置子組件的屬性,子組件通過(guò)回調(diào)函數(shù)回調(diào)變化。我打算寫個(gè)項(xiàng)目實(shí)戰(zhàn)一下。
WPF仿網(wǎng)易云 CSDN 博客專欄文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-722531.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-722531.html
到了這里,關(guān)于WPF 用戶控件依賴注入賦值的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!