wpf知识点总结 (wpf组件推荐)

在 WPF (Windows Presentation Foundation) 中,Window、Page、UserControl 和 ResourceDictionary 是四种不同的元素,每种元素都有其特定的用途和使用场景

1.Window

Window 是 WPF 应用中的顶级容器,代表一个窗口。一个 WPF 应用可以包含多个 Window,但通常有一个主 Window(启动窗口)。Window 可以包含菜单、工具栏、状态栏等,并且可以承载其他 UI 元素。

<Window x:Class="WpfDemo.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="MainWindow" Height="450" Width="800">

<!-- 窗口内容 -->

</Window>

2.Page

Page 用于导航应用程序,类似于网页。它通常用在基于导航的应用程序中,如使用 Frame 控件进行内容导航的应用。Page 可以包含用户界面元素,并且可以导航到其他 Page

<Page x:Class="WpfDemo.MainPage"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="MainPage">

<!-- 页面内容 -->

</Page>

3.UserControl

UserControl 用于封装一组控件和逻辑,以便在多个地方重用。它是创建自定义、可重用的组件的一种方式。UserControl 可以包含其他 UI 元素,并且可以通过属性和事件与外部交互。

<UserControl x:Class="WpfDemo.MyUserControl"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<!-- 控件内容 -->

</UserControl>

4.ResourceDictionary

ResourceDictionary 用于定义和存储可在整个应用程序中重用的资源,如样式、控件模板、颜色、转换器等。ResourceDictionary 可以被包含在 App.xaml 中以提供全局资源,也可以在特定的 Window、Page 或 UserControl 中定义以提供局部资源

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<!-- 资源定义 -->

</ResourceDictionary>

如何使用

  • Window:通常作为应用程序的主界面或弹出窗口使用。
  • Page:在需要页面导航功能的应用程序中使用,如向导或多步骤表单。
  • UserControl:当你有一组控件需要在多个 Window 或 Page 中重用时使用。

(1)在window里面使用

<Window x:Class="WpfDemo.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:local="clr-namespace:WpfDemo" <!-- 引用 UserControl 所在的命名空间 -->

Title="MainWindow" Height="450" Width="800">

<Grid>

<!-- 使用 UserControl -->

<local:MyUserControl />

</Grid>

</Window>

(2)在Page里面使用

<Page x:Class="WpfDemo.MainPage"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:local="clr-namespace:WpfDemo" <!-- 引用 UserControl 所在的命名空间 -->

Title="MainPage">

<Grid>

<!-- 使用 UserControl -->

<local:MyUserControl />

</Grid>

</Page>
  • ResourceDictionary:用于定义可在多个地方使用的样式和资源,以保持 UI 的一致性和减少代码重复。

(1)在 UserControl 中使用 ResourceDictionary

你可以在 UserControl 的 XAML 中直接定义一个 ResourceDictionary,或者引用一个外部定义的 ResourceDictionary。

直接定义资

<UserControl x:Class="WpfDemo.MyUserControl"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<UserControl.Resources>

<!-- 直接在这里定义资源 -->

<SolidColorBrush x:Key="MyBrush" Color="SkyBlue"/>

<ResourceDictionary.MergedDictionaries>

                <!-- 引用外部资源字典文件 -->

                <ResourceDictionary Source="MyResources.xaml"/>

            </ResourceDictionary.MergedDictionaries>

</UserControl.Resources>



<!-- 使用定义的资源 -->

<Grid Background="{StaticResource MyBrush}">

</Grid>

</UserControl>

引用外部 ResourceDictionary

<UserControl x:Class="WpfDemo.MyUserControl"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<UserControl.Resources>

<ResourceDictionary>

<ResourceDictionary.MergedDictionaries>

<!-- 引用外部定义的 ResourceDictionary -->

<ResourceDictionary Source="MyResources.xaml"/>

</ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

</UserControl.Resources>

</UserControl>

(2)在 Window 中使用 ResourceDictionary

使用方法与 UserControl 类似,你可以在 Window 的 XAML 中定义或引用 ResourceDictionary

<Window x:Class="WpfDemo.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="MainWindow" Height="450" Width="800">

<Window.Resources>

<!-- 在这里定义或引用资源 -->

<SolidColorBrush x:Key="WindowBrush" Color="LightGreen"/>

</Window.Resources>



<Grid Background="{StaticResource WindowBrush}">

</Grid>

</Window>

(3)在 Page 中使用 ResourceDictionary在 Page 中使用 ResourceDictionary 的方式也与 UserControl 和 Window 类似。

<Page x:Class="WpfDemo.MainPage"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="MainPage">

<Page.Resources>

<!-- 在这里定义或引用资源 -->

<SolidColorBrush x:Key="PageBrush" Color="Tomato"/>

</Page.Resources>

<Grid Background="{StaticResource PageBrush}">

</Grid>

</Page>

注意事项

  • 使用StaticResource时,资源查找会在资源被引用的时刻进行,因此被引用的资源必须在使用前定义。
  • 使用DynamicResource可以在运行时解析资源,这对于主题或可在运行时更改的资源特别有用。
  • 通过ResourceDictionary.MergedDictionaries可以组合多个资源字典,这在管理大型项目的资源时非常有用,允许你将资源组织在不同的文件中,以便更好地维护和重用
  • 成长在路上,贵在坚持[击掌]