使用 App.Resources 中的字符串作为 ResourceDictionary 的 Source 属性的值 {WPF}

问题描述 投票:0回答:1

我想在我的应用程序中实现多种语言而不使用资源文件 (

*.resx
),我想为此使用一个单独的 ResourceDictionary,
Text/en-US.xaml
。因为我希望语言一次更新,所以我制作了每个
Window
Page
etc. 将它们的
Resources
属性设置为
ResourceDictionary
,其源是语言文件的路径。

首先,我尝试将容器的

Resources
属性设置为
ResourceDictionary
,并且该资源字典的
Source
属性使用花括号资源机制,如下所示:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Window.Resources>
        <ResourceDictionary Source="{DynamicResource AppLangPath}"/>
    </Window.Resources>
</Window>

然而,在该代码的第 4 行,

Source
属性带有错误下划线,并且名称
AppLangPath
甚至没有出现在 IntelliSense 中。但是,我希望没有下划线,并且名称会出现在 IntelliSense 中,并且位于
en-US.xaml
中的字符串也会显示在 IntelliSense 中。

c# wpf path resourcedictionary
1个回答
0
投票

在单独的 ResourceDictionary 文件“AppResources.xaml”中,您可以定义“AppLangPath”属性,如下所示:

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <sys:String x:Key="AppLangPath">/Text/en-US.xaml</sys:String>
    </ResourceDictionary>

然后,在您的窗口或页面 XAML 中,您可以使用大括号语法引用“AppLangPath”属性:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Window.Resources>
        <ResourceDictionary Source="{DynamicResource AppLangPath}" />
    </Window.Resources>
</Window>
© www.soinside.com 2019 - 2024. All rights reserved.