【问题标题】:Using UserControl.Resources使用 UserControl.Resources
【发布时间】:2012-01-25 03:27:39
【问题描述】:

我在使用 UserControl.Resources 时遇到了一些问题。我一直遇到错误消息“文件 'Resources\Resource.xaml' 不是项目的一部分,或者它的 'Build Action' 属性未设置为 'Resource'。

我在论坛中进行了搜索,但看起来我已经完全按照示例中的说明进行了操作。不知何故,错误仍然存​​在,并且资源中嵌入的样式未能应用到我的控件中。以下是我的代码:

<UserControl x:Class="Client.NavigationControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:Client"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             HorizontalAlignment="Left"
             VerticalAlignment="Top"
             d:DesignHeight="59"
             d:DesignWidth="349"
             mc:Ignorable="d">

<UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources\Resource.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
 </UserControl.Resources>

<Grid Name="grid1"
          Width="341"
          Height="54"
          HorizontalAlignment="Stretch"
          VerticalAlignment="Stretch">


 <Button Name="button2"
                Width="97"
                Height="35"
                Margin="106,10,0,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Command="{Binding ButtonResponseSection}"
                CommandParameter="ResponseSection"
                Content="ResponseSection"
                Style="{DynamicResource GlassButton}" />


    </Grid>
</UserControl>

请帮忙:-(..

【问题讨论】:

  • 抱歉,我不太明白您关于“您的项目文件看起来像”的问题吗? ..

标签: wpf xaml user-controls


【解决方案1】:

确保将 Resource.xaml 的构建操作设置为 Page。

进一步查看,如果您的相对 URI“Resources\Resource.xaml”是正确的。

作为 URI 语法的示例,假设您的 UserControl NavigationControl 位于名为 Client 的命名空间中,并且命名空间 Client 包含在名为 App 的命名空间中。如果 App 将包含命名空间 Resources 并且您的 Resource.xaml 将驻留在此命名空间中,那么正确的 URI 将是 ../Resources/Resource.xaml。

【讨论】:

  • 感谢您的回答!我已将资源引用放在 App.xaml 中,现在可以使用了!非常感谢!
猜你喜欢
  • 1970-01-01
  • 2015-02-25
  • 2023-03-27
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-28
  • 1970-01-01
相关资源
最近更新 更多