WPF:XAML名称空间中不存在名称XYZ

时间:2015-01-19 11:14:48

标签: c# wpf xaml button

我正在尝试为图片按钮制作可重复使用的tamplete,并发现这个很棒advice,但我无法使其正常工作。你能告诉我,我做错了什么?我是WPF的新手,所以它可能是非常基本的东西,但我看不到它。

我的ImageButton.cs课程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ImageBut
{
    public class ImageButton : Button
    {
        static ImageButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton),
                new FrameworkPropertyMetadata(typeof(ImageButton)));
        }


        public ImageSource Image
        {
            get { return (ImageSource)GetValue(ImageProperty); }
            set { SetValue(ImageProperty, value); }
        }

        public static readonly DependencyProperty ImageProperty =
            DependencyProperty.Register("Image", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(default(ImageSource)));

    }
}

我在主题子文件夹中的generic.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:my="clr-namespace:ImageBut;assembly=ImageBut"
                    >

    <Style x:Key="{x:Type my:ImageButton}" TargetType="{x:Type my:ImageButton}">
        <Setter Property="Width" Value="32"></Setter>
        <Setter Property="Height" Value="32"></Setter>
        <Setter Property="BorderThickness" Value="0"></Setter>
        <Setter Property="Margin" Value="4,0,0,0"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Image Source="{TemplateBinding Image}" Stretch="Fill"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <!--  Some triggers ( IsFocused, IsMouseOver, etc.) -->
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>    
</ResourceDictionary>

我的MainWindow.xaml

<Window x:Class="ImageBut.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:ImageBut;assembly=ImageBut"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <my:ImageButton Image="C:\Users\Martin\Source\Workspaces\Digiterm\ImageBut\ImageBut\1_1.bmp"></my:ImageButton>

    </Grid>

</Window>

以下是我的Project properties,这里是Solution Explorer的结尾。

我得到的例外:

1>C:\Users\Martin\Source\Workspaces\Digiterm\ImageBut\ImageBut\MainWindow.xaml(7,10): error MC3074: The tag 'ImageButton' does not exist in XML namespace 'clr-namespace:ImageBut;assembly=ImageBut'. 

谢谢!

1 个答案:

答案 0 :(得分:0)

您不应该在命名空间声明中使用程序集名称。

而不是:

xmlns:my="clr-namespace:ImageBut;assembly=ImageBut"

试试这个:

xmlns:my="clr-namespace:ImageBut"

另外,标准WPF Button控件支持任何类型的内容,包括图像。因此,您可以在Image内设置Button控件,如下所示:

<Button>
    <Image Source="C:\Users\Martin\Source\Workspaces\Digiterm\ImageBut\ImageBut\1_1.bmp" />
</Button>

修改

generic.xaml文件中,您已将TargetType的{​​{1}}设置为错误,这会导致编译时错误。而不是:

ControlTemplate

它应该是:

<ControlTemplate TargetType="{x:Type Button}">

修复此问题后,项目在我的计算机上成功编译。