简体   繁体   中英

VS2013: An error occurred while finding the resource dictionary / Type is not part of namespace

I'm working on a custom control, and here's what I have so far:

Themes/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>

Controls/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;
        }
    }
}

Themes/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>

I now get a dozen of errors, all saying this:

Type or namespace "ColorPicker" does not exist in namespace "TrigDebugUtil.Controls"

An error occured while searching for resource dictionary "Themes/Generic.xaml"

if I replace the line in question with

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

the error is replaced by a warning "Trig.DebugUtil" assembly not referenced by this project (free translation)

regardless of the error, the project seems to compile (no error messages in the console output, just in the error list)

You should use BasedOn="{StaticResource {x:Type cont:ColorPicker}}" for sure. The syntax you used only works for default controls provided by WPF but for custom controls you have to provide the exact type.

Also for TemplateBinding, property should be dependency property and not CLR property. ColorWheelImage you declared it as a normal CLR property but it should be declared as Dependency property .

Syntax:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM