c# - 在其他程序集中加载控件时,TextBox 的样式导致异常

标签 c# wpf resourcedictionary mergeddictionaries

我已经为可重用控件创建了一个程序集(除其他外)。在此程序集中,有一个用于自定义控件样式的 Themes/Generic.xaml 文件。 我想在单独的文件中为不同的控件实现不同的样式,所以我认为使用合并字典是个好主意。

我的 Generic/Themes.xaml 看起来像这样:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="../Resources/MyTextBoxStyle.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

文件 MyTextBoxStyle.xaml 如下所示:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:general="clr-namespace:com.testsoft.General">

    <Style TargetType="{x:Type general:MyTextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
        <Setter Property="IsReadOnly" Value="True" />
        <Setter Property="Foreground" Value="Black" />
        <Style.Triggers>
            <Trigger Property="IsReadOnly" Value="True">
                <Setter Property="Foreground" Value="Gray" />
            </Trigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>

我添加了一个静态构造函数,因此将应用样式:

static MyTextBox()
{
    DefaultStyleKeyProperty.OverrideMetadata(typeof(MyTextBox), new FrameworkPropertyMetadata(typeof(MyTextBox)));
}

在另一个程序集中使用此自定义 TextBox 时,应用程序崩溃并出现以下异常:

IOException: Die Ressource "resources/mytextboxstyle.xaml"kann nicht gefunden werden. (找不到资源)

但是,如果我不使用 MergedDictionary 方法并将所有样式 XAML 直接添加到 Generic/Themes.xaml 文件中,则一切正常。

我尝试将 MyTextBoxStyle.xaml 文件的生成操作更改为资源、嵌入式资源,但这没有帮助。 如何在使用 MergedDictionaries 的同时仍然能够在其他程序集中使用样式化控件?

最佳答案

我做的和你想做的完全一样。创建一个 FrameworkLibrary 并在每个 customerSolution 上使用它。如果我定义一个 DefaultStyleKey,我会使用未注释的过程。我不知道你是否在静态构造函数上获得更好的性能。在您的 CustomerApplication (Testbase) 中,您只需将 Themes.xaml 链接到 App.xaml 中。

控件库 (WpfControlLibrary1)

using System.Windows;
using System.Windows.Controls;

namespace WpfControlLibrary1
{
    public class MyTextBox : TextBox
    {
        static MyTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MyTextBox), new FrameworkPropertyMetadata(typeof(MyTextBox)));
        }

        //public MyTextBox()
        //{
        //    DefaultStyleKey = typeof(MyTextBox);
        //}
    }
}

测试应用程序(测试库)

App.xaml

<Application x:Class="Testbase.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Testbase"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         <ResourceDictionary>
             <ResourceDictionary.MergedDictionaries>
                 <ResourceDictionary Source="pack://application:,,,/WpfControlLibrary1;component/generic/Themes.xaml" />
             </ResourceDictionary.MergedDictionaries>
         </ResourceDictionary>
    </Application.Resources>
</Application>

enter image description here

关于c# - 在其他程序集中加载控件时,TextBox 的样式导致异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58515546/

相关文章:

c# - WPF - 折线/路径周围的 HitTest 半径

c# - 在 WPF 中嵌入 Three.js 查看器

c# - 如何在代码隐藏中将样式添加到 ResourceDictionary

WPF 控件作为资源字典中的静态资源,用于多个 WPF 窗口?

WPF 在多层/项目应用解决方案中集中 xaml/image 资源

c# - 使用 RabbitMQ 的 MassTransit - RecieveFrom 的地址

c# - 使用 IPrincipal 的文件和目录安全性

c# - Windows.UI.Xaml.dll 中的 UWP 应用程序在 Release模式下崩溃

c# - 模拟时使用 OpenFileDialog 访问映射的驱动器

c# - 从表达式函数获取属性