【6月日新计划05】WPF入門-Create Project
1.Create WPF Project
1.1 .NET Framework
在 Visual Studio 2019 中创建第一个 WPF 应用 - .NET Framework | Microsoft Learn
1.2 .NET 6
後續主要用這個方法
使用 Visual Studio 创建新应用教程 - WPF .NET | Microsoft Learn
1.3 Application.xaml
应用程序启动时自动显示UI(MainWindow.xaml)
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
1.4 MainWindow.xaml
要引入資源,也就是需要的類,就需要把根目錄的名稱空間引入
xmlns:local="clr-namespace:命名空间"
<Window x:Class="Names.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:Names"
mc:Ignorable="d"
Title="MainWindow" Height="180" Width="260">
<Grid>
</Grid>
</Window>
x:Class="Names.MainWindow" xaml編譯器將編譯結果和C#類結合到一起(partial class)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ExpenseIt
{
public partial class MainWindow : NavigationWindow
{
public MainWindow()
{
InitializeComponent();
}
}
}
2. Resources
资源是保存在可执行文件中的一种不可执行数据。在WPF的资源中,几乎可以包含图像、字符串等所有的任意CLR对象,只要对象有一个默认的构造函数和独立的属性。
静态资源(Static Resource),动态资源(Dynamic Resources)。
- 静态资源在第一次编译后即确定其对象或值,之后不能对其进行修改。
- 动态资源则是在运行时决定,当运行过程中真正需要时,才到资源目标中查找其值。
2.1 Static Scena
StaticResource用于在XAML中创建的一个静态资源.
- 常用的样式和控件,如按钮、标签等,因为它们在应用程序中会被多次使用,使用StaticResource可以提高性能和减少内存消耗。
- 程序中不会改变的资源,如图标、字体等,使用StaticResource可以加快应用程序启动速度。
- 在同一页面中多次使用的资源,如背景颜色、字体等,使用StaticResource可以避免重复定义。
2.2 Dynamic Scena
DynamicResource 适用于那些资源可能在运行时被更改或者需要动态更新的情况。
- 在运行时更改应用程序主题或控件样式时。
- 当应用程序需要动态更改资源时。
- 当你想要动态更改控件某些属性时,例如控件的前景色、背景色等。
2.3 Import
在xxx.xaml中導入資源
<Window.Resources>
<DataTemplate x:Key="MyItemTemplate">
<Grid>
<Border Height="46" Background="#ffffff" CornerRadius="4">
</Border>
</Grid>
</DataTemplate>
</Window.Resources>
3. 類型轉換器
TypeConvert定义四个与转换到字符串和从字符串进行转换以用于 XAML 处理目的的成员:
- CanConvertTo
- CanConvertFrom
- ConvertTo
- ConvertFrom
最重要的方法是 ConvertFrom 。 此方法将输入字符串转换为所需的对象类型。
3.1 Demo
public class Human
{
public string Name{set;get;}
public Human Child{set;get;}
}
public class MyConverter : TypeConverter
{
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string name = value.ToString();
Human child = new Human();
child.Name = name
return child;
}
要將這個轉換類以特性的形式附加到human上
[TypeConverterAttribute(typeof(MyConverter))]
public class Human
{
public string Name{set;get;}
public Human Child{set;get;}
}
4. Markup Extensions
大括号{ }表示标记扩展用法
WPF 应用编程中最常用的标记扩展是 Binding (用于数据绑定表达式)以及资源引用 StaticResource 和 DynamicResource 。
<Page.Resources>
<SolidColorBrush x:Key="MyBrush" Color="Gold"/>
<Style TargetType="Border" x:Key="PageBackground">
<Setter Property="Background" Value="Blue"/>
</Style>
</Page.Resources>
<StackPanel>
<Border Style="{StaticResource ResourceKey=PageBackground}">
</Border>
</StackPanel>