【问题标题】:what i am getting the error each entry must have an associated key? (WPF)我得到的错误是每个条目必须有一个关联的键? (WPF)
【发布时间】:2020-05-26 20:43:59
【问题描述】:

我已经阅读了很多关于此的问题,但所有这些问题都是关于 VM 模型的,而我没有 VM 模型,我正在尝试使用 ResourceDictionary 更改我的应用程序的语言,如本文所述.

How to change language in WPF/XAML

我也看到了很多没有指定任何键的例子https://docs.microsoft.com/es-es/dotnet/framework/wpf/advanced/how-to-use-a-resourcedictionary-to-manage-localizable-string-resources

我不明白我做错了什么,这是 xaml 资源

<Window.Resources>
        <Style x:Key="titulo" TargetType="TextBlock" BasedOn="{StaticResource Encabezado}">
            <Setter Property="Margin" Value="20"/>
            <Setter Property="Foreground" Value="White"/>
        </Style>

        <Style x:Key="tipoBase" TargetType="TextBlock">
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Margin" Value="10"/>
        </Style>

        <Style x:Key="imagenTitulo" TargetType="Image">
            <Setter Property="Height" Value="200"/>
            <Setter Property="Source" Value="/images/asistente.jpg" />
        </Style>

        <Style x:Key="narracion" TargetType="TextBlock" BasedOn="{StaticResource tipoBase}">
            <Setter Property="TextWrapping" Value="Wrap"/>
            <Setter Property="FontSize" Value="18"/>
            <Setter Property="TextAlignment" Value="Center"/>
        </Style>

        <Style x:Key="botonSalirInicio" TargetType="Button" BasedOn="{StaticResource botonSalir}">
            <Setter Property="Height" Value="50"/>
            <Setter Property="Width" Value="50"/>
            <Setter Property="Margin" Value="0 0 10 0"/>
        </Style>

        <ResourceDictionary>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Idiomas/IdiomasLogin.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </ResourceDictionary>

    </Window.Resources>

资源文件

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:ElEscribaDelDJ"
                    xmlns:system="clr-namespace:System;assembly=System.Runtime">


    <system:String x:Key="Titulo">Bienvenido a "el escriba del DJ"</system:String>
    <system:String x:Key="Narración">a</system:String>
    <system:String x:Key="User">Usuario:</system:String>
    <system:String x:Key="UserText">Introduzca el nombre de usuario:</system:String>
    <system:String x:Key="CheckUser">Recordar Usuario:</system:String>
    <system:String x:Key="CheckLogin">Recordar Login:</system:String>
    <system:String x:Key="License">Este producto esta bajo la licencia</system:String>
    <system:String x:Key="Credits">Creditos</system:String>
</ResourceDictionary>

其他奇怪的是我不能使用 clr-namespace:System;assembly=mscorlib,当我尝试使用它时,总是出错

资源字典的属性 嵌入资源 自定义工具:XamlIntelliSenseFileGenerator

最后是文本块

<TextBlock Style="{DynamicResource titulo}" x:Name="titulo" Text="{DynamicResource Titulo}"/>

【问题讨论】:

    标签: c# wpf xaml resourcedictionary


    【解决方案1】:

    您应该将所有样式放入一个 ResourceDictionary。目前你有太多的&lt;ResourceDictionary&gt;声明,最外面的被视为资源但没有x:Key,因此错误。

    这样改写(&lt;ResourceDictionary.MergedDictionaries&gt;可以先写)

    <Window.Resources>
        <ResourceDictionary>
            <!--<ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Idiomas/IdiomasLogin.xaml"/>
            </ResourceDictionary.MergedDictionaries>-->
    
            <Style x:Key="titulo" TargetType="TextBlock" BasedOn="{StaticResource Encabezado}">
                <Setter Property="Margin" Value="20"/>
                <Setter Property="Foreground" Value="White"/>
            </Style>
    
            <Style x:Key="tipoBase" TargetType="TextBlock">
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="Margin" Value="10"/>
            </Style>
    
            <Style x:Key="imagenTitulo" TargetType="Image">
                <Setter Property="Height" Value="200"/>
                <Setter Property="Source" Value="/images/asistente.jpg" />
            </Style>
    
            <Style x:Key="narracion" TargetType="TextBlock" BasedOn="{StaticResource tipoBase}">
                <Setter Property="TextWrapping" Value="Wrap"/>
                <Setter Property="FontSize" Value="18"/>
                <Setter Property="TextAlignment" Value="Center"/>
            </Style>
    
            <Style x:Key="botonSalirInicio" TargetType="Button" BasedOn="{StaticResource botonSalir}">
                <Setter Property="Height" Value="50"/>
                <Setter Property="Width" Value="50"/>
                <Setter Property="Margin" Value="0 0 10 0"/>
            </Style>
    
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Idiomas/IdiomasLogin.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    

    【讨论】:

    • 感谢您解决了问题,我现在的问题是,我应该将所有样式移动到资源文件“IdiomasLogin.xaml”还是在不同文件中有 2 个资源然后合并?例如,我应该添加一些东西。 .MergedDictionaries>
    • @DavidPinedoSolano,单独文件中的资源字典允许在不同的地方加载和重用资源(通过合并该字典)。如果您在一个地方使用 smth - 它不必在单独的文件中。如果您总是一起使用两种资源 - 将它们放在同一个字典中。这是一个组织问题——类似于我们如何使用命名空间、.NET 组件、Nuget 包
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 2014-09-11
    相关资源
    最近更新 更多