2

我有两个项目。一个是库,另一个是 WPF 应用程序。在我的库项目中,我创建了一个资源文件 (Resources.resx) 并在其中添加了一些字符串,并将其作为公共访问。

现在在我的 WPF 项目中,我添加了库项目的引用。现在我正在尝试在我的 XAML 控件中使用库资源文件。

这是我的代码。这里 MyApplication 是 wpf 项目, MyLibrary 是 dll 项目。

<UserControl x:Class="MyApplication.LauncherView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:p="clr-namespace:MyLibrary"
    <StackPanel>
        <Button Content="{x:Static p:Resources.TextFromResource}" Command="{Binding LaunchCommand}" />
    </StackPanel>
</UserControl>

但最后我收到这些错误。

错误 1 ​​名称“资源”在命名空间“clr-namespace:MyLibrary”中不存在。错误 2 找不到类型“资源”。请注意,类型名称区分大小写。

4

2 回答 2

2

要使用该类,而不是在您的程序集中,您需要定义程序集

xmlns:p="clr-namespace:MyNamespace;assembly=MyLibrary"

但是对于包含资源,你不需要包含命名空间,因为它不是一个类,也没有命名空间......你需要包含资源文件......这是正确的方法

<ResourceDictionary Source="pack://application:,,,/MyLibrary;component/Subfolder/MyResourceFile.xaml"/>

之后您可以使用

Content="{x:StaticResource TextFromResourceKey}"
于 2013-10-09T07:55:55.570 回答
-1

你有使用合并字典。

<ResourceDictionary.MergedResources> <ResourceDictionary Source="pack://application:,,,/MyLibrary;component/component/Subfolder/MyResourceFile.xaml"/ > </ResourceDictionary.MergedResources>

之后你就可以使用它了。

于 2013-10-09T07:59:55.947 回答