专业开发网站的公司,深圳海外推广,网站设计步骤详解,怎样建立自己的网站卖东西文章目录 前言专栏和Gitee仓库依赖属性实战#xff1a;缩小#xff0c;全屏#xff0c;关闭按钮依赖属性操作封装主窗口传递this本身给TitleView标题控件主要代码MainWindow.xmalMainWindow.cs依赖属性方法封装TitleView.csTitleViewModelTitleViewModel实现效果 前言
这次… 文章目录 前言专栏和Gitee仓库依赖属性实战缩小全屏关闭按钮依赖属性操作封装主窗口传递this本身给TitleView标题控件主要代码MainWindow.xmalMainWindow.cs依赖属性方法封装TitleView.csTitleViewModelTitleViewModel实现效果 前言
这次我们来讲解一下WPF 的组件化开发流程组件化开发是是可以极大降低我们页面开发难度降低代码耦合的方法。这让我们可以将任意WPF界面进行拆解。因为我写过Vue所以我会按照Vue的逻辑将WPF进行组件化开发。
专栏和Gitee仓库 WPF仿网易云 Gitee仓库 WPF仿网易云 CSDN博客专栏 依赖属性
WPF为了提高性能限制了Binding的使用。需要将属性提前注册为依赖属性或者附加属性才能解决使用Binding语法。原因是每个能binding的属性需要在内存中开辟存储空间。WPF默认不能Binding需要主动声明才可以。
这个就是为什么Elelctron,Fullter等内存开销那么大是因为他们的将可能没用的的内存空间也设置了。 博客园 痕迹g WPF依赖属性详解 B站 十月的寒流 WPF 中依赖属性及附加属性的概念及用法 B站 微软系列技术教程 WPF依赖属性详解 实战缩小全屏关闭按钮
这里我讲解一下Window和UserControl两者的区别。Window就是整个窗口UserControl就是控件。Window负责窗口的一些方法比如拖拽缩小放大。而我们是组件化开发我们就需要将主窗口的this传给子组件
依赖属性操作封装
这里先去看我这个总结的博客。 WPF 用户控件依赖属性赋值 主窗口传递this本身给TitleView标题控件
因为我们是View和ViewModel开发所有的View只有传递参数和暴露依赖属性的作用实际的业务是ViewModel去做的。 所以我们传递的方向是 #mermaid-svg-kBvVvOlA3lFUo3jf {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-kBvVvOlA3lFUo3jf .error-icon{fill:#552222;}#mermaid-svg-kBvVvOlA3lFUo3jf .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-kBvVvOlA3lFUo3jf .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-kBvVvOlA3lFUo3jf .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-kBvVvOlA3lFUo3jf .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-kBvVvOlA3lFUo3jf .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-kBvVvOlA3lFUo3jf .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-kBvVvOlA3lFUo3jf .marker{fill:#333333;stroke:#333333;}#mermaid-svg-kBvVvOlA3lFUo3jf .marker.cross{stroke:#333333;}#mermaid-svg-kBvVvOlA3lFUo3jf svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-kBvVvOlA3lFUo3jf .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-kBvVvOlA3lFUo3jf .cluster-label text{fill:#333;}#mermaid-svg-kBvVvOlA3lFUo3jf .cluster-label span{color:#333;}#mermaid-svg-kBvVvOlA3lFUo3jf .label text,#mermaid-svg-kBvVvOlA3lFUo3jf span{fill:#333;color:#333;}#mermaid-svg-kBvVvOlA3lFUo3jf .node rect,#mermaid-svg-kBvVvOlA3lFUo3jf .node circle,#mermaid-svg-kBvVvOlA3lFUo3jf .node ellipse,#mermaid-svg-kBvVvOlA3lFUo3jf .node polygon,#mermaid-svg-kBvVvOlA3lFUo3jf .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-kBvVvOlA3lFUo3jf .node .label{text-align:center;}#mermaid-svg-kBvVvOlA3lFUo3jf .node.clickable{cursor:pointer;}#mermaid-svg-kBvVvOlA3lFUo3jf .arrowheadPath{fill:#333333;}#mermaid-svg-kBvVvOlA3lFUo3jf .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-kBvVvOlA3lFUo3jf .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-kBvVvOlA3lFUo3jf .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-kBvVvOlA3lFUo3jf .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-kBvVvOlA3lFUo3jf .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-kBvVvOlA3lFUo3jf .cluster text{fill:#333;}#mermaid-svg-kBvVvOlA3lFUo3jf .cluster span{color:#333;}#mermaid-svg-kBvVvOlA3lFUo3jf div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-kBvVvOlA3lFUo3jf :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} MainWindow MainWindowViewModel TitleView的MainWindow依赖属性 TitleViewModel的MainWindow 主要代码
MainWindow.xmal Window x:ClassBlankApp1.Views.MainWindow......!--需要主动设置名称不然会Binding错误--Window.DataContextViewModels:MainWindowViewModel x:NameMainWindowViewModel //Window.DataContextDockPanel LastChildFillTrue!--其它代码--Grid DockPanel.DockTopMouseLeftButtonDownGrid_MouseLeftButtonDownHeightauto!--手动指定DataContext--Views:TitleView MainWindow{Binding MainWindow, ElementNameMainWindowViewModel} //Grid/DockPanel
/WindowMainWindow.cs public partial class MainWindow : Window{public MainWindowViewModel ViewModel { get; set; }public MainWindow(){InitializeComponent();//重定向ViewModelViewModel (MainWindowViewModel)DataContext;ViewModel.MainWindow this;}}
依赖属性方法封装
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;namespace BlankApp1.Utils
{public class MyWpfExtensionView where View : class{/// summary/// 简化依赖注入代码/// /summary/// typeparam nameView/typeparam/// typeparam nameValue/typeparam/// param namename/param/// param nameaction/param/// returns/returnspublic DependencyProperty DependencyPropertySetValue(string name, ActionView, 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;}}
}
TitleView.cs namespace BlankApp1.Views
{/// summary/// TitleView.xaml 的交互逻辑/// /summarypublic partial class TitleView : UserControl{//这个只是为了代码提示不涉及逻辑public MainWindow MainWindow { get; set; }//初始化依赖属性构造器public static readonly MyWpfExtensionTitleView MyWpfExtension new MyWpfExtensionTitleView();//这个是简化后的依赖属性public static readonly DependencyProperty MainWindowProperty MyWpfExtension.DependencyPropertySetMainWindow(MainWindow, (view, value) {//通过依赖属性来获取MainWindow的对象view.TitileViewModel.MainWindow value;});/// summary/// DataContext的数据/// /summarypublic TitileViewModel TitileViewModel { get; set; }public TitleView(){InitializeComponent();//拿到DataContext数据重定向TitileViewModel (TitileViewModel)DataContext;}}
}
TitleViewModel
using BlankApp1.Models;
using BlankApp1.Views;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Windows;namespace BlankApp1.ViewModels
{public partial class TitileViewModel:ObservableObject{public RelayCommand CloseWindow { get; set; }public RelayCommand MaxOrNormalWindow { get; set; }public RelayCommand MiniWindow { get; set; }public MainWindow MainWindow { get; set; }public TitileViewModel() {//.......其它代码CloseWindow new RelayCommand(() {MainWindow.Close();Debug.WriteLine(关闭窗口);});MaxOrNormalWindow new RelayCommand(() {if(MainWindow.WindowState WindowState.Normal){MainWindow.WindowState WindowState.Maximized;MainWindow.MaxHeight SystemParameters.MaximizedPrimaryScreenHeight;MainWindow.MaxWidth SystemParameters.MaximizedPrimaryScreenWidth;}else{MainWindow.WindowState WindowState.Normal;}Debug.WriteLine(最大化或正常窗口);});MiniWindow new RelayCommand(() {MainWindow.WindowState WindowState.Minimized;Debug.WriteLine(缩小窗口);});}}
}
TitleViewModel
就是绑定按钮事件我就不放了 详细代码看我的Gitee仓库地址 WPF仿网易云 Gitee仓库 实现效果