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

本文关键字:一部 分时 错误 命名空间 资源 查找 字典 类型 VS2013 | 更新日期: 2023-09-27 18:19:12

我正在做一个自定义控件,下面是我目前所做的:

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

我现在得到了一打错误,都是这样说的:

类型或命名空间"ColorPicker"在命名空间"TrigDebugUtil"中不存在。控制"

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

如果我用

替换有问题的行

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

错误被一个警告"Trig。DebugUtil"此项目未引用的程序集(自由翻译)

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

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

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

同样对于TemplateBinding, property应该是依赖属性而不是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));