简体   繁体   English

WPF/XAML 如何指定要从中加载资源的程序集?

[英]WPF/XAML How to specify the assembly to load the resource from?

I'm working on a WPF class library, not an application.我正在研究 WPF 类库,而不是应用程序。 Here is an example of a Label I'm making in c# and I'd like to "style" it using XAML.这是我在 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;
    }

For this I have ressourcedictionnary containing my LabelStyle, everything is compiling without problem.为此,我有包含我的 LabelStyle 的 ressourcedictionnary,一切都在编译没有问题。

<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>

but when I use my dll later on, the style is not applied and I have this error message :但是当我稍后使用我的 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.

here is my actual App.xaml with build action setup to page :这是我实际的 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>

How to specify the assembly to load the resource from ?如何指定要从中加载资源的程序集? I'm fairly new to WPF and I'm stuck on this problem, thanks in advance.我对 WPF 还很陌生,我一直在解决这个问题,提前致谢。

EDIT 1 :编辑 1:

I tried as my Assembly Name is WpfApplication1 (see here http://postimg.org/image/ksyj9xi5p/ )我尝试过,因为我的程序集名称是 WpfApplication1(请参阅此处http://postimg.org/image/ksyj9xi5p/

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

instead of代替

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

and get the same error.并得到相同的错误。

Did you try to replace your你有没有尝试更换你的

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

by the suggestion that is indicated in you error, that is using the "Pack" syntax ?根据您错误中指出的建议,即使用“Pack”语法?

pack://application:,,,/assemblyname;component/

Given the information you provided鉴于您提供的信息

Uri uri = new Uri("pack://application:,,,/WpfApplication1;component/Styles/LabelStyle.xaml", UriKind.Relative);

This might help you这可能会帮助你

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

You can then find resources within it in the usual manner, eg, myDictionary["LabelStyle"]然后您可以以通常的方式在其中查找资源,例如myDictionary["LabelStyle"]

You can set the property: System.Windows.Application.ResourceAssembly as suggested in the error message.您可以按照错误消息中的建议设置属性: System.Windows.Application.ResourceAssembly Read carefully the documentation .仔细阅读文档 Note that the property can only be set once.请注意,该属性只能设置一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM