别名资源 (WPF) [英] Aliasing Resources (WPF)

查看:44
本文介绍了别名资源 (WPF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 XAML 中为资源设置别名,如下所示:

I am trying to alias a resource in XAML, as follows:

<UserControl.Resources>

    <StaticResourceExtension x:Key="newName" ResourceKey="oldName"/>

</UserControl.Resources>

oldName 只是指Image 类型的资源,在App.xaml 中定义.

oldName simply refers to a resource of type Image, defined in App.xaml.

据我所知,这是执行此操作的正确方法,并且应该可以正常工作.但是,XAML 代码给了我一个非常无用的错误:

As far as I understand, this is the correct way to do this, and should work fine. However, the XAML code gives me the superbly unhelpful error:

"The application XAML file failed to load. Fix errors in the application XAML before opening other XAML files."

当我将鼠标悬停在代码中的 StaticResourceExtension 行(带有波浪形下划线)时,就会出现这种情况.实际错误列表中还生成了其他几个错误,但似乎相当不相关且无意义(例如当前上下文中不存在名称‘InitializeComponent’的消息"),因为当删除该行时它们都消失了.

This appears when I hover over the StaticResourceExtension line in the code (which has a squiggly underline). Several other errors are generated in the actual Error List, but seem to be fairly irrelevant and nonsenical (such messages as "The name 'InitializeComponent' does not exist in the current context"), as they all disappear when the line is removed.

我完全被这里难住了.为什么 WPF 会抱怨这段代码?请对解决方案有任何想法吗?

I'm completely stumped here. Why is WPF complaining about this code? Any ideas as to a resolution please?

注意:我在 .NET 3.5 SP1 中使用 WPF.

Note: I'm using WPF in .NET 3.5 SP1.

更新 1:
我应该澄清我确实收到编译器错误(错误列表中的上述消息),所以这不仅仅是设计者的问题.

Update 1:
I should clairfy that I do receive compiler errors (the aforementioned messages in the Error List), so it's not just a designer problem.

更新 2:
这是完整的相关代码...

Update 2:
Here's the relevant code in full...

在 App.xaml 中(在 Application.Resource 下):

In App.xaml (under Application.Resource):

<Image x:Key="bulletArrowUp" Source="Images/Icons/bullet_arrow_up.png" Stretch="None"/>
<Image x:Key="bulletArrowDown" Source="Images/Icons/bullet_arrow_down.png" Stretch="None"/>

在 MyUserControl.xaml 中(在 UserControl.Resources 下):

And in MyUserControl.xaml (under UserControl.Resources):

<StaticResourceExtension x:Key="columnHeaderSortUpImage" ResourceKey="bulletArrowUp"/>
<StaticResourceExtension x:Key="columnHeaderSortDownImage" ResourceKey="bulletArrowDown"/>

当然,这些是产生错误的行.

These are the lines that generate the errors, of course.

推荐答案

尽我所能,我无法重现您的问题.我认为比您发布的代码更多.这对我来说很好用:

Try as I might, I cannot reproduce your problem. I think there is more at play than the code you have posted. This works fine for me:

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"
    StartupUri="Window1.xaml">
    <Application.Resources>
        <Image x:Key="bulletArrowUp" Source="Leaves.jpg" Stretch="None"/>
        <Image x:Key="bulletArrowDown" Source="Leaves.jpg" Stretch="None"/>
    </Application.Resources>
</Application>

Window1.xaml:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="300">
    <local:UserControl1/>
</Window>

UserControl1.xaml:

<UserControl x:Class="WpfApplication1.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <UserControl.Resources>
        <StaticResourceExtension x:Key="columnHeaderSortUpImage" ResourceKey="bulletArrowUp"/>
        <StaticResourceExtension x:Key="columnHeaderSortDownImage" ResourceKey="bulletArrowDown"/>
    </UserControl.Resources>
    <ContentControl Content="{StaticResource columnHeaderSortUpImage}"/>
</UserControl>

我将 Leaves.jpg 图像添加到我的项目中,它显示得很好.

I added the Leaves.jpg image to my project and it displayed just fine.

这篇关于别名资源 (WPF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆