【问题标题】:WPF class library, loading XAML with absolute URI or relative URI both not workingWPF 类库,使用绝对 URI 或相对 URI 加载 XAML 都不起作用
【发布时间】:2015-09-25 11:00:59
【问题描述】:

我正在编写一个主要基于 C# 代码的 WPF 类库,我目前正在尝试仅加载 XAML 文件以设置 UI 元素的样式。

这是带有“BuildAction : Content”的 XAML“样式”代码:

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

这是我的标签的代码:

    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("pack://application:,,,/WpfApplication1;component/WpfApplication1/Styles/LabelStyle.xaml", UriKind.Relative);

        StreamResourceInfo info = Application.GetContentStream(uri);
        System.Windows.Markup.XamlReader reader = new System.Windows.Markup.XamlReader();
        ResourceDictionary myResourceDictionary = Application.LoadComponent(uri) as ResourceDictionary;
        Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
        Style myLabelStyle = myResourceDictionary["LabelStyle"] as Style;
        uiElement.Style = myLabelStyle;
    }

如果 UriKind 设置为“相对”,我会收到以下错误消息:

A relative URI cannot be created because the 'uriString' parameter represents an absolute URI.

但如果 Urikind 设置为“Absolute”,那么我会得到这个:

Cannot use absolute URI.

所以无论如何,XAML 文件都没有加载,样式也没有应用。

编辑:

我试过这个 URI:

pack://application:,,,/WpfApplication1;component/Styles/LabelStyle.xaml

得到同样的错误。

【问题讨论】:

    标签: c# wpf xaml uri


    【解决方案1】:

    正确引用位于本地程序集项目文件夹子文件夹中的资源文件的格式

    pack://application:,,,/Subfolder/ResourceFile.xaml
    

    在单独引用的程序集中引用资源文件的正确格式是

    pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml
    

    把你的 uri 改成

    pack://application:,,,/WpfApplication1;component/Styles/LabelStyle.xaml
    

    应该解决问题

    【讨论】:

    • 我已经尝试过了,因为我找到了一个给出类似答案的帖子,但我仍然遇到同样的错误。
    • 您的 ResourceDictionary 是在同一个项目中还是在您在此处引用的另一个项目中?
    • 包含 LabelStyle.xaml 的样式文件夹就在我构建的 .dll 旁边,现在位于:Desktop\WpfApplication1\WpfApplication1\bin\x86\Debug
    • 根据您在评论中所说的,您的 Style1 文件夹位于您的工作目录中,并且您没有使用/引用任何外部程序集,使用项目中的添加引用选项,所以 pack://application: ,,,/Styles/LabelStyle.xaml 应该可以工作,并使用 UriKind.RelativeOrAbsolute。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多