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

我正在研究
WPF类库,而不是应用程序.这是我在c#中制作的Label的一个例子,我想用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;
    }

为此,我有ressourcedictionnary包含我的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);

并得到相同的错误.

最佳答案 你有没有尝试更换你的

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

通过你的错误中指出的建议,即使用“包”语法?

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

鉴于您提供的信息

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