为什么在页面上实例化 WPF 用户控件时在设计器中加载 resources/resources.xaml 时出错?



我只在设计器中收到此错误;我的游戏运行良好。

错误为"找不到资源资源/资源.xaml"。它只发生在我引用用户控件的视图上,并且只发生在设计器中 - 不是在我设计用户控件本身时,也不是在运行时;即使我有这个错误,我也能够很好地编译我的游戏。

这是我声明我的用户控件实例的位置:

<local:StatsView Grid.ColumnSpan="2" x:Name="stats"/>

以下是我的用户控件的相关部分:

<UserControl x:Class="Wormholes.Views.StatsView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:Wormholes.Views" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <UserControl.Resources> <ResourceDictionary Source="/Resources/Resources.xaml" /> </UserControl.Resources> <Grid> ... </Grid> </UserControl>

除了从用户控件之外,我还在每个页面中引用了相同的 Resources.xaml;这会导致错误吗?如果是这样,我该如何解决它?

您使用了相对 URI,因此它将在您使用 UserControl 的位置查找您的资源文件。它找不到你的资源文件,因为你的资源文件不在绝对路径中。

AbsolutePath = CurrentPath (r.g 您使用 UserControl 的位置( + RelativePath

所以使用绝对路径:

<ResourceDictionary Source="pack://application:,,,/{YourAssemblyName};component/Resources/Resources.xaml" />

最新更新