WPF/XAML如何指定从哪个程序集加载资源?

5
我正在开发一个WPF类库,而不是应用程序。这是我使用C#制作标签的示例,我想使用XAML进行“样式”设置。
   private void CreateElement(int i)
    {
        UIElementOut[i] = new Label();
        var uiElement = (Label)UIElementOut[i];
        uiElement.HorizontalAlignment = HorizontalAlignment.Center;
        uiElement.VerticalAlignment = VerticalAlignment.Center;
        uiElement.FontFamily = new FontFamily(FFontInput[i]);
        uiElement.FontSize = Convert.ToDouble(FontSizeIn[i]);
        uiElement.Content = TextIn[i];
        Brush BgBrushColor = new SolidColorBrush(RGBAToMediaColor(FBgCol[i]));
        Brush FgBrushColor = new SolidColorBrush(RGBAToMediaColor(FFgCol[i]));
        uiElement.Background = BgBrushColor;
        uiElement.Foreground = FgBrushColor;
        Uri uri = new Uri("Styles/LabelStyle.xaml", UriKind.Relative);
        StreamResourceInfo info = Application.GetContentStream(uri);
        System.Windows.Markup.XamlReader reader = new System.Windows.Markup.XamlReader();
        ResourceDictionary myResourceDictionary = (ResourceDictionary)reader.LoadAsync(info.Stream);
        Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
        Style myLabelStyle = myResourceDictionary["LabelStyle"] as Style;
        uiElement.Style = myLabelStyle;
    }

为此,我有一个资源字典包含我的LabelStyle,一切都可以编译而没有问题。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:WpfApplication1">
<Style x:Key="LabelStyle" TargetType="{x:Type Label}">
    <Setter Property="Height" Value="53" />
    <Setter Property="Width" Value="130" />
    <Setter Property="HorizontalAlignment" Value="Left" />
    <Setter Property="Margin" Value="99,71,0,0" />
    <Setter Property="VerticalAlignment" Value= "Top" />
    <Setter Property="Foreground" Value="#FFE75959" />
    <Setter Property="FontFamily" Value="Calibri" />
    <Setter Property="FontSize" Value="40" />
</Style>

但是当我稍后使用我的dll时,样式没有应用并且出现了这个错误消息:
ERR : Assembly.GetEntryAssembly() returns null. Set the Application.ResourceAssembly property or use the pack://application:,,,/assemblyname;component/ syntax to specify the assembly to load the resource from.

这是我的实际App.xaml文件,构建操作设置为页面:

<Application x:Class="WpfApplication1.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:WpfApplication1"
         StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>

如何指定程序集以从中加载资源? 我对WPF相当新,我卡在这个问题上了,提前感谢。

编辑1:

我尝试过,因为我的程序集名称是WpfApplication1(请参见此处http://postimg.org/image/ksyj9xi5p/

ResourceDictionary myResourceDictionary = Application.LoadComponent(new Uri("/WpfApplication1;component/Styles/LabelStyle.xaml", UriKind.RelativeOrAbsolute)) as ResourceDictionary;

替代

ResourceDictionary myResourceDictionary = (ResourceDictionary)reader.LoadAsync(info.Stream);

并且得到相同的错误。


可能是在单独的程序集中使用ResourceDictionary的重复问题。 - Colin Grealy
@Clemens 是的,我试过了,并且得到了相同的错误。我的解决方案中的程序集名称是:AssemblyInfo.cs。 - lecloneur
这不是程序集名称,而只是AssemblyInfo文件。在“应用程序”选项卡下查看项目属性。在那里你会找到实际的程序集名称。 - Clemens
@Clemens 我确实检查过了,名字是错误的,我编辑了我的第一篇帖子,实际的程序集名称是WpfApplication1,但错误仍然存在。 - lecloneur
@Clemens,我认为是这样的,请看这个截图:http://postimg.org/image/ksyj9xi5p/ - lecloneur
显示剩余3条评论
3个回答

2

Did you try to replace your

Uri uri = new Uri("Styles/LabelStyle.xaml", UriKind.Relative);

根据您的错误提示,建议使用“Pack”语法?
pack://application:,,,/assemblyname;component/

根据您提供的信息。
Uri uri = new Uri("pack://application:,,,/WpfApplication1;component/Styles/LabelStyle.xaml", UriKind.Relative);

1
我想我已经接近了答案,但是现在出现了这个错误信息:"由于“uriString”参数代表绝对 URI,因此无法创建相对 URI"。但是如果我改成绝对 URI,它会说“无法使用绝对 URI”,你们有什么提示吗?谢谢。 - lecloneur

0
您可以按照错误信息中的建议设置属性:System.Windows.Application.ResourceAssembly。仔细阅读文档。请注意,该属性只能设置一次。

0

这可能会对你有所帮助

ResourceDictionary myResourceDictionary = Application.LoadComponent(new Uri("/assemblyname;component/Styles/LabelStyle.xaml", UriKind.RelativeOrAbsolute)) as ResourceDictionary;

然后您可以像平常一样在其中查找资源,例如:myDictionary["LabelStyle"]


谢谢,但我已经添加了这些行:ResourceDictionary myResourceDictionary = Application.LoadComponent(new Uri("/assemblyname;component/Styles/LabelStyle.xaml", UriKind.RelativeOrAbsolute)) as ResourceDictionary;Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary); Style myLabelStyle = myResourceDictionary["LabelStyle"] as Style; uiElement.Style = myLabelStyle; 但我仍然遇到错误。 - lecloneur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接