【问题标题】:VS2013: An error occurred while finding the resource dictionary / Type is not part of namespaceVS2013:查找资源字典时出错/类型不是命名空间的一部分
【发布时间】:2014-10-17 17:11:10
【问题描述】:

我正在开发一个自定义控件,这是我目前所拥有的:

主题/Generic.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Themes/ColorPicker.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

控件/ColorPicker.cs

namespace TrigDebugUtil.Controls
{
    public class ColorPicker : Control
    {
        #region Private Fields

        #endregion //Private Fields

        #region Properties
        public BitmapImage ColorWheelImage
        {
            get;
            private set;
        }
        #endregion //Properties

        static ColorPicker()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ColorPicker), new FrameworkPropertyMetadata(typeof(ColorPicker)));
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            WriteableBitmap ColorDrawboard;
        }
    }
}

主题/ColorPicker.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:TrigDebugUtil.Controls">


    <Style TargetType="{x:Type local:ColorPicker}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ColorPicker}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <!-- <Image Source="{TemplateBinding ColorWheelImage}" /> -->
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

App.xaml

<Application x:Class="TrigDebugUtil.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:cont="clr-namespace:TrigDebugUtil.Controls"
             StartupUri="MainWindow.xaml">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Themes/Generic.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <Style TargetType="{x:Type cont:ColorPicker}" BasedOn="{StaticResource ColorPicker}" />
        </ResourceDictionary>
    </Application.Resources>
</Application>

我现在收到十几个错误,都在说:

命名空间“TrigDebugUtil.Controls”中不存在类型或命名空间“ColorPicker”

搜索资源字典“Themes/Generic.xaml”时出错

如果我用

替换有问题的行

pack://application:,,,/Trig.DebugUtil;component/Themes/ColorPicker.xaml

错误被此项目未引用的警告“Trig.DebugUtil”程序集替换(免费翻译)

不管错误如何,项目似乎都可以编译(控制台输出中没有错误消息,只是在错误列表中)

【问题讨论】:

  • App.xamlColorPicker 控件是位于同一个程序集下还是不同程序集下?
  • 两个文件是同一个项目的一部分,因此它应该是同一个程序集。但是我应该注意,解决方案中还有其他项目(但是没有引用它们)并且我在项目的属性和 Assembly.cs 中更改了程序集名称
  • 由于您已经在单独的 xaml 中为 ColorPicker ColorPicker.xaml 声明了模板,因此您需要将其合并到您的应用资源下。尝试合并或将默认模板移动到Generic.xaml 下。
  • 我实际上将模板从 Generic.xaml 移到了一个单独的文件中(之前的问题几乎相同)。在 Generic.xaml 中包含 ColorPicker.xaml,在 app.xaml 中包含 generic.xaml 还不够吗?
  • 试试BasedOn="{StaticResource {x:Type cont:ColorPicker}}",看看能不能解决你的问题。

标签: c# wpf visual-studio xaml visual-studio-2013


【解决方案1】:

您当然应该使用BasedOn="{StaticResource {x:Type cont:ColorPicker}}"。您使用的语法仅适用于 WPF 提供的默认控件,但对于自定义控件,您必须提供确切的类型。

同样对于 TemplateBinding,属性应该是依赖属性而不是 CLR 属性。 ColorWheelImage 您已将其声明为普通 CLR 属性,但应将其声明为 Dependency 属性

语法:

public BitmapImage ColorWheelImage
{
    get { return (BitmapImage)GetValue(ColorWheelImageProperty); }
    set { SetValue(ColorWheelImageProperty, value); }
}

public static readonly DependencyProperty ColorWheelImageProperty =
    DependencyProperty.Register("ColorWheelImage", typeof(BitmapImage), 
                                 typeof(ColorPicker));

【讨论】:

  • 感谢您的回答,虽然大多数错误都消失了,但这是实际问题。实际上,我对此感到非常惊讶,因为我在另一个项目中以“错误”的方式进行了操作,并且它在那里没有问题地工作,尽管这些控件继承自 ContentControl 而不是 Control。您能否进一步解释为什么必须给出完整类型?
  • 我也知道依赖属性,因为其他错误,我只是在编码中间停下来。
  • 这很好,因为你已经明确设置了x:Key - &lt;Style x:Key="TimeSlider" TargetType="{x:Type Controls:TimeSlider}"&gt;。所以BasedOn="{StaticResource TimeSlider}"对应那个x:Key。
  • BasedOn 是必需的,以防您想从其他样式继承一种样式。所以 BasedOn 类似于 C# 中的继承。当您想要从一种样式中获取设置器、触发器等并且不想重复自己时,请使用 BasedOn 从该样式中继承它。
  • 如果你设置了x:Key,那么它就不再是默认样式了。如果您省略为 Generic.xaml 中定义的样式设置 x:Key,它将被视为默认样式,并将应用于您应用中的所有 ColorPicker。因此,使用&lt;Style TargetType="{x:Type cont:ColorPicker}" BasedOn="{StaticResource ColorPicker}" /&gt; 行,您正在创建默认样式。如果您在 Generic.xaml 下声明了默认样式(不带 x:Key),则可以省略此行。
猜你喜欢
  • 1970-01-01
  • 2014-11-01
  • 2015-03-04
  • 2013-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-05
相关资源
最近更新 更多