【发布时间】:2020-11-04 04:31:04
【问题描述】:
在我的WPF 项目中,我使用WindowChrome 来自定义我的Mainwindow。当我尝试绑定Person.xaml.cs 的属性LastName 时出现以下错误Person.Xaml 文件的代码隐藏类如下所示:
异常:找不到名为“prs”的资源。资源名称区分大小写。
备注
- 该错误似乎是由于下面显示的
ResourceDictionary.xaml文件中的<local:Person x:Key="prs"/>引用而发生的。当'Person.xaml.csclass is instantiated in a button click event defined inResourceDictionary.xaml.cs`文件如下所示时,错误发生在Persona.xaml.cs文件的InitializeComponent();行。 - 如果我在
App.xaml文件中引用<local:Person x:Key="prs"/>,则会出现完全相同的错误。 - 但是,当我不使用
ResourceDictionary自定义MainwWindow而是在App.xaml 文件中引用<local:Person x:Key="prs"/>时,确实会出现错误不会。
问题:我可能做错了什么,我们如何在仍然使用 ResourceDictionary 自定义 MainWindow 的同时解决问题?
Person.xaml:
<Window x:Class="MyProject.Commands.Person"
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:MyProject">
<Grid>
<Label>Last Name:</Label>
<TextBox Name="txtLastName">
<TextBox.Text>
<Binding Path="LastName" Source="{StaticResource prs}" UpdateSourceTrigger="PropertyChanged">
</Binding>
</TextBox.Text>
</TextBox>
</Grid>
</Window
Person.xaml.cs
namespace MyProject.Commands
{
public partial class Person: Window
{
public Person()
{
InitializeComponent(); //the above ERROR occurs here
}
public string LastName
{
get { return txtLastName.Text; }
}
}
}
ResourceDictionary.xaml:第一行引用了数据源<local:Person x:Key="pers"/>
<ResourceDictionary x:Class="MyProject.WindowStyle"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProject.Commands">
<local:Person x:Key="pers"/>
<Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}">
<Setter Property="WindowChrome.WindowChrome">
<Setter.Value>
<WindowChrome CaptionHeight="30"
CornerRadius="4"
GlassFrameThickness="0"
NonClientFrameEdges="None"
ResizeBorderThickness="5"
UseAeroCaptionButtons="False" />
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="Background" Value="Gray" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Grid>
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="5,30,5,5">
<AdornerDecorator>
<ContentPresenter />
</AdornerDecorator>
</Border>
<DockPanel Height="30"
VerticalAlignment="Top"
LastChildFill="False">
<TextBlock Margin="5,0,0,0"
VerticalAlignment="Center"
DockPanel.Dock="Left"
FontSize="16"
Foreground="White"
Text="{TemplateBinding Title}" />
<Button x:Name="btnClose"
Width="15"
Margin="5"
Click="CloseClick"
Content="X"
DockPanel.Dock="Right"
WindowChrome.IsHitTestVisibleInChrome="True" />
<Button x:Name="btnRestore"
Width="15"
Margin="5"
Click="MaximizeRestoreClick"
Content="#"
DockPanel.Dock="Right"
WindowChrome.IsHitTestVisibleInChrome="True" />
<Button x:Name="btnMinimize"
Width="15"
Margin="5"
VerticalContentAlignment="Bottom"
Click="MinimizeClick"
Content="_"
DockPanel.Dock="Right"
WindowChrome.IsHitTestVisibleInChrome="True" />
<Button x:Name="btnAddPerson"
Width="15"
Margin="5"
VerticalContentAlignment="Bottom"
Click="btnAddPerson_Click"
Content="_"
DockPanel.Dock="Right"
WindowChrome.IsHitTestVisibleInChrome="True" />
</DockPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
ResourceDictionary.xaml.cs:
namespace SciDoxViewer
{
public partial class WindowStyle : ResourceDictionary
{
public WindowStyle()
{
InitializeComponent();
}
//Here: click events for other buttons in above ResourceDictionary.xaml file
...............
...............
private void btnAddPerson_Click(object sender, RoutedEventArgs e)
{
Person person = new Person(); //This line first takes you to `InitializeComponent();` of the constructor of `Person` class where the above error occurs.
if (person.ShowDialog() == true)
{
.....
}
}
}
}
更新 [日期:2020 年 11 月 5 日]
WindowStyle.xaml 在MainWindow(而不是Person.xaml)中被引用如下。
MainWindow.xaml:
<Window x:Class="MyProject.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:MyProject"
mc:Ignorable="d"
Style="{StaticResource CustomWindowStyle}">
<Grid>
....
</Grid>
</Window>
App.xaml:
<Application x:Class="MyProject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SciDoxViewer.Commands"
xmlns:main="clr-namespace:MyProject"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyProject;component/WindowStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
【问题讨论】:
-
我在您发布的代码中看不到任何将
WindowStyle资源字典放入您要引用资源的Person窗口的搜索路径的内容。是什么让你认为它应该起作用?另外,请不要省略代码示例的部分内容。如果省略的代码不重要,就不要提了。提供一个实际的 minimal reproducible example,它是独立的、最小的并且完整。 -
@PeterDuniho 在阅读了您的 cmets 之后,我刚刚添加了
Person.xaml文件的<Window...>标记的缺失部分。 -
这行不通。不能有一个带有 XAML 的 Person 类,该类包含一个使用与 Source 相同类的实例的 Binding。 CustomWindowStyle 应在 Person.xaml 中声明。
-
@nam:
ResourceDictionary在哪里被合并/引用到窗口中? -
@nam:是的,没错。
标签: wpf xaml resourcedictionary