我一直在接触 WinUI 3(来自 WPF),并首先制作一个带有图标和文本的简单用户控件(配置为依赖属性)。当我使用该控件并将属性绑定到资源字典中的资源时,我在 App.g.i.cs 中运行时收到以下异常:
数值未落在预期范围内。
如果我替换资源字典中对几何图形的引用并直接插入,则一切正常。在花了时间进行实验和研究之后,我还没有发现为什么会出现这种情况,所以我想我应该在这里联系。
我确信我正在做一些愚蠢的事情(无论是疏忽还是只是缺乏对 WinUI 3 中的工作和设置方式的理解)。任何帮助将不胜感激。
MainWindow.xaml
<?xml version="1.0" encoding="utf-8"?>
<Window
x:Class="Winui3ResourcesTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="using:Winui3ResourcesTest"
mc:Ignorable="d" Title="Winui3ResourcesTest">
<StackPanel Orientation="Horizontal" Padding="5"
HorizontalAlignment="Center" VerticalAlignment="Center"
Background="PaleGreen">
<!-- This works -->
<!--<local:UserControl1
HorizontalAlignment="Center" VerticalAlignment="Center"
Text="{StaticResource Control1Text}"
Icon="M3,6H21V8H3V6 M3,11H21V13H3V11 M3,16H21V18H3V16Z"/>-->
<!-- This doesn't work -->
<local:UserControl1
HorizontalAlignment="Center" VerticalAlignment="Center"
Text="{StaticResource Control1Text}"
Icon="{StaticResource Control1Icon}"/>
</StackPanel>
</Window>
MyControl.cs
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace Winui3ResourcesTest;
public sealed class MyControl : Control
{
public static DependencyProperty TextProperty = DependencyProperty.Register(
nameof(Text),
typeof(string),
typeof(MyControl),
new PropertyMetadata(default(string)));
public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public static DependencyProperty IconProperty = DependencyProperty.Register(
nameof(Icon),
typeof(Geometry),
typeof(MyControl),
new PropertyMetadata(default(Geometry)));
public Geometry Icon
{
get => (Geometry)GetValue(IconProperty);
set => SetValue(IconProperty, value);
}
public MyControl()
{
this.DefaultStyleKey = typeof(MyControl);
}
}
主题\Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="using:Winui3ResourcesTest">
<Style TargetType="local:MyControl" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyControl">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel Orientation="Vertical">
<TextBlock Text="{TemplateBinding Text}"/>
<PathIcon Data="{TemplateBinding Icon}"/>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
ResourceDictionary1.xaml
<?xml version="1.0" encoding="utf-8"?>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<x:String x:Key="Control1Text">Hello, World!</x:String>
<Geometry x:Key="Control1Icon">M3,6H21V8H3V6 M3,11H21V13H3V11 M3,16H21V18H3V16Z</Geometry>
</ResourceDictionary>
项目文件
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<RootNamespace>Winui3ResourcesTest</RootNamespace>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>x86;x64;ARM64</Platforms>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<PublishProfile>win-$(Platform).pubxml</PublishProfile>
<UseWinUI>true</UseWinUI>
<EnableMsixTooling>true</EnableMsixTooling>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Remove="ResourceDictionary1.xaml" />
<None Remove="UserControl1.xaml" />
</ItemGroup>
<ItemGroup>
<Content Include="Assets\SplashScreen.scale-200.png" />
<Content Include="Assets\LockScreenLogo.scale-200.png" />
<Content Include="Assets\Square150x150Logo.scale-200.png" />
<Content Include="Assets\Square44x44Logo.scale-200.png" />
<Content Include="Assets\Square44x44Logo.targetsize-24_altform-unplated.png" />
<Content Include="Assets\StoreLogo.png" />
<Content Include="Assets\Wide310x150Logo.scale-200.png" />
</ItemGroup>
<ItemGroup>
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>
<!--
Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging
Tools extension to be activated for this project even if the Windows App SDK Nuget
package has not yet been restored.
-->
<ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<ProjectCapability Include="Msix" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.241114003" />
</ItemGroup>
<ItemGroup>
<Page Update="UserControl1.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Page Update="ResourceDictionary1.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<!--
Defining the "HasPackageAndPublishMenuAddedByProject" property here allows the Solution
Explorer "Package and Publish" context menu entry to be enabled for this project even if
the Windows App SDK Nuget package has not yet been restored.
-->
<PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
</PropertyGroup>
<!-- Publish Properties -->
<PropertyGroup>
<PublishReadyToRun Condition="'$(Configuration)' == 'Debug'">False</PublishReadyToRun>
<PublishReadyToRun Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRun>
<PublishTrimmed Condition="'$(Configuration)' == 'Debug'">False</PublishTrimmed>
<PublishTrimmed Condition="'$(Configuration)' != 'Debug'">True</PublishTrimmed>
<DefaultLanguage>en-US</DefaultLanguage>
</PropertyGroup>
</Project>
你可以尝试以下方法来代替几何吗?
<x:String x:Key="Control1Icon">M3,6H21V8H3V6 M3,11H21V13H3V11 M3,16H21V18H3V16Z</x:String>