4

我想使用外部文件来自定义我的应用程序的样式,但它不起作用。我正在逐步执行此操作,但是当我执行项目时,异常属于:

System.Windows.ni.dll 中出现“System.Windows.Markup.XamlParseException”类型的第一次机会异常

我的 XAML 代码:

应用程序.xaml:

<Application.Resources>
    <local:LocalizedStrings xmlns:local="clr-namespace:App1" x:Key="LocalizedStrings"/>
    <ResourceDictionary x:Key="myDict">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

资源.xaml:

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

    <Style TargetType="TextBox" x:Key="MyTextBox">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0.5"/>
        <Setter Property="BorderBrush" Value="Gray"/>
        <Setter Property="Opacity" Value="0.5"/>
        <Setter Property="Foreground" Value="Red"/>
    </Style>

</ResourceDictionary>
4

1 回答 1

5

尝试将本地资源声明移动ResourceDictionary到您正在创建并分配给Application.Resources属性的内部:

<Application.Resources>
    <ResourceDictionary x:Key="myDict">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources.xaml"/>
        </ResourceDictionary.MergedDictionaries>

        <local:LocalizedStrings xmlns:local="clr-namespace:App1" x:Key="LocalizedStrings"/>
        <!-- other resources in here -->
    </ResourceDictionary>
</Application.Resources>
于 2013-02-12T02:57:32.147 回答