簡體   English   中英

WPF:名稱XYZ在XAML命名空間中不存在

[英]WPF: The name XYZ does not exist in XAML namespace

我試圖為圖像按鈕制作可重復使用的棉絮,但發現了這個不錯的建議 ,但我無法使其工作。 你能告訴我,我在做什么錯? 我是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)));

    }
}

我在Themes子文件夾中的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>

這是我的項目屬性 ,這是解決方案資源管理器的 snapchat。

我收到的異常:

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'. 

謝謝!

您在名稱空間聲明中不需要程序集名稱。

代替這個:

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

嘗試這個:

xmlns:my="clr-namespace:ImageBut"

順便說一句,標准的WPF Button控件支持任何種類的內容,包括圖像。 因此,您可以在Button內有一個Image控件,如下所示:

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

編輯

在您的generic.xaml文件中,您已將ControlTemplateTargetType設置為錯誤的值,這會導致編譯時錯誤。 代替:

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

它應該是:

<ControlTemplate TargetType="{x:Type my:ImageButton}">

解決此問題后,項目在我的計算機上成功編譯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM