使用ResourceDictionary

问题描述

在我的WPF项目中,我使用WindowChrome自定义我的Mainwindow。尝试绑定如下所示的LastName文件Person.xaml.cs代码隐藏类的属性Person.Xaml时出现以下错误

异常:找不到名为“ prs”的资源。资源名称区分大小写。

备注

  1. 错误似乎是由于如下所示的<local:Person x:Key="prs"/>文件中的ResourceDictionary.xaml引用引起的。如下所示,当“ Person.xaml.cs InitializeComponent(); ResourceDictionary.xaml.cs”文件显示Persona.xaml.cs文件的第class is instantiated in a button click event defined in行时,就会发生错误
  2. 如果我改为在<local:Person x:Key="prs"/>文件中引用App.xaml,则会发生完全相同的错误
  3. 但是,当我不使用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"
                                       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>

解决方法

您应该从资源字典中删除Person资源,因为您已经在click事件处理程序中创建了Person窗口的另一个实例。该资源仍然没有被使用。