WPF框架系列课程(小白进阶选择)-WPF视频教程2023

woaiwodejia333 · · 383 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
WPF框架系列课程(小白进阶选择)-WPF视频教程2023 今天给大家讲解一下关于WPF的知识内容,希望对大家在学习wpf的过程中遇到的问题有所帮助,以下是参考资料下载: 1、什么是WPF WPF(Windows Presentation Foundation)是用于开发Windows应用程序的UI框架,它提供了一种更加现代化的方式来创建用户界面,并且支持各种不同的用户交互方式,包括触摸屏、鼠标和键盘。 使用WPF我们可以轻松的开发出Windows应用程序。 2、创建你的第一个WPF应用程序 首先,我们需要打开Visual Studio 2019。在Visual Studio 2019中,选择“创建新项目(N)” namespace DemoPrism.Second.ViewModels{ internal class SecondViewModel : BindableBase { #region 属性及构造函数 private int id; public int Id { get { return id; } set { SetProperty(ref id, value); } } /// <summary> /// 模块间交互 /// </summary> private readonly IEventAggregator eventAggregator; public SecondViewModel(IEventAggregator eventAggregator) { this.eventAggregator = eventAggregator; this.eventAggregator.GetEvent<DemoOneEvent>().Subscribe(DemoOneRecived); } #endregion private void DemoOneRecived(int id) { this.Id = id; } }} 3、解决方案资源管理器 所有项目文件、代码、窗口、资源都将显示在此窗格中。 4、属性 此窗格显示可以基于所选项进行配置的属性设置。 例如,如果从“解决方案资源管理器”中选择一个项,你会看到与该文件相关的属性设置。 如果在“设计器”中选择一个对象,你会看到该项目的设置。 5、工具箱 工具箱包含可添加到窗体的所有控件。 若要将控件添加到当前窗体,请双击控件或拖放控件。 6、XAML 设计器 这是 XAML 文档的设计器。 它是交互式的,可以从“工具箱”拖放对象。 通过在设计器中选择和移动项,可以直观地为应用构建用户界面 (UI)。 当设计器和编辑器都可见时,对设计器的更改会反映在编辑器中,反之亦然。 在设计器中选择项目时,“属性”窗格会显示有关该对象的属性和特性。 7、XAML 代码编辑器 这是 XAML 文档的 XAML 代码编辑器。 XAML 代码编辑器是一种无需设计器即可手动创建 UI 的方法。 将控件添加到设计器中时,设计器可以推断控件上的属性值。 XAML 代码编辑器则为你提供更多控制权。 当设计器和编辑器都可见时,对设计器的更改会反映在编辑器中,反之亦然。 在代码编辑器中导航文本插入点时,“属性”窗格会显示有关该对象的属性和特性。 在 WPF 中,UI 通常使用一种叫做 XAML(Extensible Application Markup Language)的标记语言来声明性地定义。使用 XAML 可以更容易地创建和管理复杂的 UI 布局,同时也提高了设计师和开发人员的协作效率。例如: <Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Button Content="Click Me" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75"/> </Grid> </Window> HelixToolkit框架 采用原生的框架进行开发是比较困难或麻烦的,所以考虑采用第三方框架进行开发。 HelixToolkit提供一些3D模型组件,可以方便用户使用 开源项目地址:GitHub - helix-toolkit/helix-toolkit: Helix Toolkit is a collection of 3D components for .NET 先看一段设计代码: <HelixToolkit:HelixViewport3D ShowFrameRate="True"                        ZoomExtentsWhenLoaded="True"                         ZoomAroundMouseDownPoint="True"                         RotateAroundMouseDownPoint="True"                         IsTopBottomViewOrientedToFrontBack="True"                         IsViewCubeEdgeClicksEnabled="True"> <HelixToolkit:SunLight /> <ModelVisual3D x:Name="model"></ModelVisual3D> <!-- You can also add elements here in the xaml --> <HelixToolkit:GridLinesVisual3D Width="180" Length="180" MajorDistance="10" MinorDistance="10" Thickness="0.1" /> </HelixToolkit:HelixViewport3D> 控件要求所有内容应包含在HelixToolkit:HelixViewport3D标签内,包括:光照、地平线和其他模型。 模型一般采用ModelVisual3D 标签,定义模型的方式有两种:标签或代码: 用标签定义: <ModelVisual3D> <ModelVisual3D.Content> <GeometryModel3D> <GeometryModel3D.Geometry> <MeshGeometry3D x:Name="meshMain" Positions="50 50 50 71 60 60 60 71 60 71 71 60 60 60 71 71 60 71 60 71 71 71 71 71" TriangleIndices="2 3 1 2 1 0 7 1 3 7 5 1 6 5 7 6 4 5 6 2 0 2 0 4 2 7 3 2 6 7 0 1 5 0 5 4"> </MeshGeometry3D> </GeometryModel3D.Geometry> <GeometryModel3D.Material> <DiffuseMaterial x:Name="matDiffuseMain"> <DiffuseMaterial.Brush> <SolidColorBrush Color="LightPink"/> </DiffuseMaterial.Brush> </DiffuseMaterial> </GeometryModel3D.Material> </GeometryModel3D> </ModelVisual3D.Content> </ModelVisual3D> XAML命名空间 开发语言会将常用功能以类的形式封装,开发人员根据自己的业务需求,也会封装满足自身业务需求的类,如果有序组织这些类?一方面,便于开发人员准确调用;另一方面,编译器可以有效识别具有相同命名的类,就引入了命名空间,简单的说,是通过类似树状结构来组织各种类,是一种较为有效的类名排列方式。 而XAML和.NET其他语言一样,也是通过命名空间有效组织起XAML内部的相关元素类,这里的命名空间与.NET中的命名空间不是一一对应的,而是一对多,一眼望去,都是“网址”,这里的网址,是遵循XAML解析器标准的命名规则,而不是真正的网址(在IE中根本打不开)。 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"对应一些与XAML语法和编译相关的CLR名称空间,例如 <Style x:key="buttonMouseOver" TargetType="{x:Type Button}"> 这里的xmlns和xmlns:x的区别在于x作为别名,在应用时,以前缀形式出现,而xmlns作为默认命名空间,不使用前缀标识的元素,来自该命名空间。 老的framework我喜欢使用MvvmLightLibs,但是目前用过的都知道该包已经被标记为弃用了,推荐使用微软的CommunityToolkit.Mvvm,我们可以在项目文件中添加该包的引用 < PackageReferenceInclude= "CommunityToolkit.Mvvm"Version= "8.0.0"/> 添加好了之后我们将我们的viewmodel继承ObservableObject具体使用可以看园子里的一篇文章https://www.cnblogs.com/dino623/p/building_MVVM_programs_using_MVVM_Toolkit.html示例代码: publicclassMainWindowViewModel: ObservableObject { ///<summary> ///属性变更通知 ///</summary> privatePage _currentPage; publicPage CurrentPage { get=> _currentPage; set=> SetProperty( ref_currentPage, value); } //命令绑定 publicRelayCommand ShiftWorkPageCommand { get; set; } publicRelayCommand OpenWorkSpaceCommand { get; set; } } } 元素失去获得 元素可以使用 CaptureMouse 方法获得,这可以用在拖动,一旦拖动出元素可以获得,得到拖动结束。 但是有时会失去获得,如果自己需要失去,可以使用 Mouse.Capture(null) 但是在没有自己使用的这个函数,失去获得,可以的是: 设置元素可命中false,如果看到元素失去交互,而且堆栈没有任何地方使用失去获得,那么可能就是存在设置元素可命中false。 如果有两个函数同时 获得 一个元素,会不会出现 失去获得?不会,如果同一个元素多次 获得,那么不会出现失去获得。如果这是让另一个获得,那么这个元素就是失去获得。可以通过元素.IsMouseCaptured 判断元素获得。 可以通过 Mouse.Captured 获得现在 Mouse 是否获得。如果返回是 null ,没有获得,但是元素获得存在一些问题,在失去焦点或其他,可能就失去获得。 public partial class App { #region Constants and Fields /// <summary>The event mutex name.</summary> private const string UniqueEventName = "{GUID}"; /// <summary>The unique mutex name.</summary> private const string UniqueMutexName = "{GUID}"; //这里需要加 Environment.UserName /// <summary>The event wait handle.</summary> private EventWaitHandle eventWaitHandle; /// <summary>The mutex.</summary> private Mutex mutex; #endregion #region Methods /// <summary>The app on startup.</summary> /// <param name="sender">The sender.</param> /// <param name="e">The e.</param> private void AppOnStartup(object sender, StartupEventArgs e) { bool isOwned; this.mutex = new Mutex(true, UniqueMutexName, out isOwned); this.eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, UniqueEventName); // So, R# would not give a warning that this variable is not used. GC.KeepAlive(this.mutex); if (isOwned) { // Spawn a thread which will be waiting for our event var thread = new Thread( () => { while (this.eventWaitHandle.WaitOne()) { Current.Dispatcher.BeginInvoke( (Action)(() => ((MainWindow)Current.MainWindow).BringToForeground())); } }); // It is important mark it as background otherwise it will prevent app from exiting. thread.IsBackground = true; thread.Start(); return; } // Notify other instance so it could bring itself to foreground. this.eventWaitHandle.Set(); // Terminate this instance. this.Shutdown(); } #endregion } 以下是我的心得总结,感谢大家的观看!! ![QQ截图20231120152048.png](http://static.itsharecircle.com/231120/f37d08cfd312e9ba9d886bfcabe9571d.png)
383 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传