做a视频 免费网站,深圳网站建设公司哪里有,做外贸网站如何,腾讯企点是干嘛的前言
我一直想组件化得去开发WPF#xff0c;因为我觉得将复杂问题简单化是最好的
如何组件化开发 主窗口引用
Window x:ClassWpfApp1.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.…前言
我一直想组件化得去开发WPF因为我觉得将复杂问题简单化是最好的
如何组件化开发 主窗口引用
Window x:ClassWpfApp1.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WpfApp1xmlns:MDhttp://materialdesigninxaml.net/winfx/xaml/themesxmlns:Viewsclr-namespace:WpfApp1.Viewsmc:IgnorabledTitleMainWindow Height450 Width800 Window.ResourcesStyle x:Keymy_text TargetTypeTextBlockSetter PropertyFontSize Value30 /Setter PropertyMargin Value8 //Style/Window.ResourcesWindow.DataContext !--需要命名来指定数据源--local:MainWindowViewModel x:NameviewModel//Window.DataContextGrid!--不能直接写TitleValueBinding数据源会有问题--Views:ViewA Margin10Title{Binding ElementNameviewModel,PathTitleValue} //Grid
/Window
cs部分
namespace WpfApp1
{/// summary/// Interaction logic for MainWindow.xaml/// /summarypublic partial class MainWindow : Window{public MainWindow(){InitializeComponent();}}public class MainWindowViewModel{public string TitleValue { get; set; } 我是测试数据;}
}控件窗口 UserControl.DataContextlocal:ViewAViewModel //UserControl.DataContextGridTextBlock Text{Binding Title} //Grid/// summary
/// ViewA.xaml 的交互逻辑
/// /summary
public partial class ViewA : UserControl
{public static readonly DependencyProperty TitleProperty;/// summary/// 为了拿到数据源需要定义一下/// /summaryprivate ViewAViewModel ViewModel new ViewAViewModel();public ViewA(){InitializeComponent();ViewModel (ViewAViewModel)DataContext;}static ViewA(){//静态构造TitleProperty DependencyProperty.Register(Title, typeof(string), typeof(ViewA),new PropertyMetadata(,new PropertyChangedCallback((item, res) {//拿到数据再次赋值var model (ViewA)item;model.ViewModel.Title (string)res.NewValue;})));}/// summary/// 只是为了有代码提示添加依赖属性后不会被调用/// /summarypublic string Title { get; set; }}
public partial class ViewAViewModel : ObservableObject
{/// summary/// 通知更新/// /summary[ObservableProperty]private string title ViewA Title!;}依赖注入代码优化
我将复杂的依赖注入的代码进行了优化减少了重复内容的输入。
//原代码
TitleProperty DependencyProperty.Register(Title, typeof(string), typeof(ViewA), new PropertyMetadata(default,new PropertyChangedCallback((item, res) {//拿到数据再次赋值var model (ViewA)item;model.ViewModel.Title (string)res.NewValue;})));//新代码TitleProperty DependencyPropertySetViewA, string(Title, (view, value) {view.ViewModel.Title value;});/// summary
/// 简化依赖注入代码
/// /summary
/// typeparam nameView/typeparam
/// typeparam nameValue/typeparam
/// param namename/param
/// param nameaction/param
/// returns/returns
public static DependencyProperty DependencyPropertySetView,Value(string name,ActionView,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;
}