VS2013:查找资源字典/类型不是命名空间的一部分时发生错误

时间:2014-08-24 10:44:42

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

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

主题/ 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>

我现在收到了十几个错误,都是这样说的:

类型或命名空间&#34; ColorPicker&#34;在名称空间&#34; TrigDebugUtil.Controls&#34;

中不存在

搜索资源字典时发生错误&#34; Themes / Generic.xaml&#34;

如果我用

替换有问题的行

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

错误被警告&#34; Trig.DebugUtil&#34;取代。此项目未引用的程序集(免费翻译)

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

1 个答案:

答案 0 :(得分:1)

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

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

语法:

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

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