加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 22:20:15 所属栏目:百科 来源:网络整理
导读:我正在研究 WPF类库,而不是应用程序.这是我在c#中制作的Label的一个例子,我想用XAML“设计”它. private void CreateElement(int i) { UIElementOut[i] = new Label(); var uiElement = (Label)UIElementOut[i]; uiElement.HorizontalAlignment = Horizontal
我正在研究 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" />
    <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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读