4

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

<UserControl.Resources>

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

</UserControl.Resources>

oldName简单地引用类型的资源Image,定义在App.xaml.

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

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

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

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

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

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

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

在 App.xaml 中(在 下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):

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

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

4

2 回答 2

2

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

应用程序.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>

用户控件1.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图像添加到我的项目中,它显示得很好。

于 2009-07-17T15:35:59.523 回答
1

我尝试使用与 Kent 相同的设置并收到以下错误:

Property 'Resources' does not support values of type 'System.Windows.Controls.Image'.

我还尝试了其他一些类型并得到了相同的结果(只是使用了不同的完全限定类型名称)。

我只能通过以下方式重新键入图像<UserControl.Resources>

<UserControl.Resources>
    <Image x:Key="myimg"
           Source="{Binding Source={StaticResource appimg}, Path=Source}"
           Stretch="{Binding Source={StaticResource appimg}, Path=Stretch}"/>
</UserControl.Resources>

我不禁要问:为什么?资源不是为重新键入而设计的。我怀疑你最终想要完成的事情可以通过其他方式做得更好、更容易。可能与风格的二传手。

于 2009-07-29T17:48:50.463 回答