【问题标题】:Custom control does not exist in the namespace?命名空间中不存在自定义控件?
【发布时间】:2016-09-22 10:41:24
【问题描述】:

我一直对 c# 中的 WPF 绑定感兴趣,并尝试将控件绑定到鼠标位置,但在得到令人沮丧的“名称“[control]”不存在于命名空间“clr-namespace:[namespace ]" 每次我将 xaml 代码粘贴到编辑器中时,我都认为不值得花时间研究这个怪癖。

现在我尝试简单地实现 binded Arc drawing example from Stack Overflow 并再次遇到相同的错误。 (可在此处找到所有可运行的简短示例的代码)

因此,我已经梳理了针对此问题的所有 Stack Overflow 解决方案(实际上似乎相当普遍),其中包含看似零星无法弥补的变通方法和修复。


  1. This question 这么说

如果没有其他可能,请注释使用命名空间的行,重新构建,然后再次构建完整的项目。

我还尝试重建项目,重新打开 Visual Studio。没有 帮助。我终于评论了xaml,重建项目,取消评论 xml,它终于奏效了!奇怪的问题。

  1. This one 说将项目设置为发布模式,另一个回答者说要定义程序集:(xmlns:Local="clr-namespace:MusicPlayer.Controls;assembly=MusicPlayer"),这对我也不起作用。
  2. This person 建议更改构建目标平台(x86 - x64)

我尝试了几乎所有这些解决方案都无济于事。 ReSharper 似乎知道 Arc 类存在于分配给 Local 的命名空间中,但 Visual Studio 不知道。

<Local:Arc Center="{Binding Path=PreviousMousePositionPixels}" 
         Stroke="White" 
         StrokeDashArray="4 4"
         SnapsToDevicePixels="True"
         StartAngle="0" 
         EndAngle="{Binding Path=DeltaAngle}" 
         SmallAngle="True"
         Radius="40"/>

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;

public sealed class Arc : Shape
{
    public Point Center
    {
        get { return (Point)GetValue(CenterProperty); }
        set { SetValue(CenterProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Center.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CenterProperty =
        DependencyProperty.Register("Center", typeof(Point), typeof(Arc)
        , new FrameworkPropertyMetadata(new Point(0, 0), FrameworkPropertyMetadataOptions.AffectsRender));


    public double StartAngle
    {
        get { return (double)GetValue(StartAngleProperty); }
        set { SetValue(StartAngleProperty, value); }
    }

    // Using a DependencyProperty as the backing store for StartAngle.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty StartAngleProperty =
        DependencyProperty.Register("StartAngle", typeof(double), typeof(Arc)
        , new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));

    public double EndAngle
    {
        get { return (double)GetValue(EndAngleProperty); }
        set { SetValue(EndAngleProperty, value); }
    }

    // Using a DependencyProperty as the backing store for EndAngle.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty EndAngleProperty =
        DependencyProperty.Register("EndAngle", typeof(double), typeof(Arc)
        , new FrameworkPropertyMetadata(Math.PI/2.0, FrameworkPropertyMetadataOptions.AffectsRender));

    public double Radius
    {
        get { return (double)GetValue(RadiusProperty); }
        set { SetValue(RadiusProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Radius.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty RadiusProperty =
        DependencyProperty.Register("Radius", typeof(double), typeof(Arc)
        , new FrameworkPropertyMetadata(10.0, FrameworkPropertyMetadataOptions.AffectsRender));



    public bool SmallAngle
    {
        get { return (bool)GetValue(SmallAngleProperty); }
        set { SetValue(SmallAngleProperty, value); }
    }

    // Using a DependencyProperty as the backing store for SmallAngle.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SmallAngleProperty =
        DependencyProperty.Register("SmallAngle", typeof(bool), typeof(Arc)
        , new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));


    static Arc()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(Arc), new FrameworkPropertyMetadata(typeof(Arc)));
    }

    protected override Geometry DefiningGeometry
    {
        get
        {

            var a0 = StartAngle < 0 ? StartAngle + 2 * Math.PI : StartAngle;
            var a1 = EndAngle < 0 ? EndAngle + 2 * Math.PI : EndAngle;

            if (a1<a0)
            {
                a1 += Math.PI * 2;
            }

            SweepDirection d = SweepDirection.Counterclockwise;
            bool large;

            if (SmallAngle)
            {
                large = false;
                double t = a1;
                if ((a1-a0)>Math.PI)
                {
                    d = SweepDirection.Counterclockwise;
                }
                else
                {
                    d = SweepDirection.Clockwise;
                }


            }else{
                large = (Math.Abs(a1 - a0) < Math.PI);
            }

            Point p0 = Center + new Vector(Math.Cos(a0), Math.Sin(a0)) * Radius;
            Point p1 = Center + new Vector(Math.Cos(a1), Math.Sin(a1)) * Radius;


            List<PathSegment> segments = new List<PathSegment>(1);
            segments.Add(new ArcSegment(p1, new Size(Radius, Radius), 0.0, large, d, true));

            List<PathFigure> figures = new List<PathFigure>(1);
            PathFigure pf = new PathFigure(p0, segments, true);
            pf.IsClosed = false;
            figures.Add(pf);

            Geometry g = new PathGeometry(figures, FillRule.EvenOdd, null);
            return g;
        }
    }
}

【问题讨论】:

  • 当我尝试从 Visual Studio 工具箱中拖放 Arc 类时,会出现一个对话框“以下引用已添加到项目中”[命名空间]。版本 1.0.0.0 Culture=neutral, PublicKeyToken=null" 为了使用新引用中的类型,请按 OK 重新启动 XAML 设计器。设计器重新启动后,在画板上重新创建控件" 然后什么都不做
  • Arc 类代码未包装在命名空间中。这可能是你的问题吗?
  • @RobCroll 我没有用我的命名空间发布代码,它被包装在一个命名空间中。
  • 检查以确保您的项目没有冲突的命名空间和名称。虽然很少见,但有时会发生这种情况,并且也会出现此错误。

标签: c# .net wpf xaml namespaces


【解决方案1】:

按照 RobCroll 的建议在命名空间内定义 Arc。检查下面的代码,我已将其包含在 XAML 中称为本地的 Arc_Learning 命名空间中。

    <Window x:Class="Arc_Learning.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Arc_Learning"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <local:Arc
             Stroke="White" 
         StrokeDashArray="4 4"
         SnapsToDevicePixels="True"
         StartAngle="0" 
         EndAngle="{Binding Path=DeltaAngle}" 
         SmallAngle="True"
         Radius="40"/>
    </Grid>
</Window>

    using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Arc_Learning
{
    public sealed class Arc : Shape
    {
        public Point Center
        {
            get { return (Point)GetValue(CenterProperty); }
            set { SetValue(CenterProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Center.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CenterProperty =
            DependencyProperty.Register("Center", typeof(Point), typeof(Arc)
            , new FrameworkPropertyMetadata(new Point(0, 0), FrameworkPropertyMetadataOptions.AffectsRender));


        public double StartAngle
        {
            get { return (double)GetValue(StartAngleProperty); }
            set { SetValue(StartAngleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for StartAngle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StartAngleProperty =
            DependencyProperty.Register("StartAngle", typeof(double), typeof(Arc)
            , new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));

        public double EndAngle
        {
            get { return (double)GetValue(EndAngleProperty); }
            set { SetValue(EndAngleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for EndAngle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty EndAngleProperty =
            DependencyProperty.Register("EndAngle", typeof(double), typeof(Arc)
            , new FrameworkPropertyMetadata(Math.PI / 2.0, FrameworkPropertyMetadataOptions.AffectsRender));

        public double Radius
        {
            get { return (double)GetValue(RadiusProperty); }
            set { SetValue(RadiusProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Radius.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RadiusProperty =
            DependencyProperty.Register("Radius", typeof(double), typeof(Arc)
            , new FrameworkPropertyMetadata(10.0, FrameworkPropertyMetadataOptions.AffectsRender));



        public bool SmallAngle
        {
            get { return (bool)GetValue(SmallAngleProperty); }
            set { SetValue(SmallAngleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SmallAngle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SmallAngleProperty =
            DependencyProperty.Register("SmallAngle", typeof(bool), typeof(Arc)
            , new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));


        static Arc()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(Arc), new FrameworkPropertyMetadata(typeof(Arc)));
        }

        protected override Geometry DefiningGeometry
        {
            get
            {

                var a0 = StartAngle < 0 ? StartAngle + 2 * Math.PI : StartAngle;
                var a1 = EndAngle < 0 ? EndAngle + 2 * Math.PI : EndAngle;

                if (a1 < a0)
                {
                    a1 += Math.PI * 2;
                }

                SweepDirection d = SweepDirection.Counterclockwise;
                bool large;

                if (SmallAngle)
                {
                    large = false;
                    double t = a1;
                    if ((a1 - a0) > Math.PI)
                    {
                        d = SweepDirection.Counterclockwise;
                    }
                    else
                    {
                        d = SweepDirection.Clockwise;
                    }


                }
                else {
                    large = (Math.Abs(a1 - a0) < Math.PI);
                }

                Point p0 = Center + new Vector(Math.Cos(a0), Math.Sin(a0)) * Radius;
                Point p1 = Center + new Vector(Math.Cos(a1), Math.Sin(a1)) * Radius;


                List<PathSegment> segments = new List<PathSegment>(1);
                segments.Add(new ArcSegment(p1, new Size(Radius, Radius), 0.0, large, d, true));

                List<PathFigure> figures = new List<PathFigure>(1);
                PathFigure pf = new PathFigure(p0, segments, true);
                pf.IsClosed = false;
                figures.Add(pf);

                Geometry g = new PathGeometry(figures, FillRule.EvenOdd, null);
                return g;
            }
        }
    }
}

【讨论】:

  • 我的代码和你一样,除了我的命名空间不同。当我将鼠标悬停在 local:Arc 上时,我看到“名称“Arc”在名称空间“clr-namespace:CustomControlTest”中不存在。CustomControlTest 是我的名称空间。如果我构建并运行,Arc 甚至不会出现.
【解决方案2】:

尝试将本地引用添加到您的窗口,如下所示:

在窗口的开始标记中添加此引用

xmlns:local="clr-namespace:CustomControlTest"

C# 后面的代码中你缺少命名空间定义

在类定义之前添加缺少的命名空间行,这样你的代码应该是这样的:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;

namespace CustomControlTest
{
    public sealed class Arc : Shape
    {
        public Point Center
        {
    get { return (Point)GetValue(CenterProperty); }
    set { SetValue(CenterProperty, value); }
}

// Using a DependencyProperty as the backing store for Center.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty CenterProperty =
    DependencyProperty.Register("Center", typeof(Point), typeof(Arc)
    , new FrameworkPropertyMetadata(new Point(0, 0), FrameworkPropertyMetadataOptions.AffectsRender));


public double StartAngle
{
    get { return (double)GetValue(StartAngleProperty); }
    set { SetValue(StartAngleProperty, value); }
}

// Using a DependencyProperty as the backing store for StartAngle.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty StartAngleProperty =
    DependencyProperty.Register("StartAngle", typeof(double), typeof(Arc)
    , new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));

public double EndAngle
{
    get { return (double)GetValue(EndAngleProperty); }
    set { SetValue(EndAngleProperty, value); }
}

// Using a DependencyProperty as the backing store for EndAngle.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty EndAngleProperty =
    DependencyProperty.Register("EndAngle", typeof(double), typeof(Arc)
    , new FrameworkPropertyMetadata(Math.PI/2.0, FrameworkPropertyMetadataOptions.AffectsRender));

public double Radius
{
    get { return (double)GetValue(RadiusProperty); }
    set { SetValue(RadiusProperty, value); }
}

// Using a DependencyProperty as the backing store for Radius.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty RadiusProperty =
    DependencyProperty.Register("Radius", typeof(double), typeof(Arc)
    , new FrameworkPropertyMetadata(10.0, FrameworkPropertyMetadataOptions.AffectsRender));



public bool SmallAngle
{
    get { return (bool)GetValue(SmallAngleProperty); }
    set { SetValue(SmallAngleProperty, value); }
}

// Using a DependencyProperty as the backing store for SmallAngle.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty SmallAngleProperty =
    DependencyProperty.Register("SmallAngle", typeof(bool), typeof(Arc)
    , new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));


static Arc()
{
    DefaultStyleKeyProperty.OverrideMetadata(typeof(Arc), new FrameworkPropertyMetadata(typeof(Arc)));
}

protected override Geometry DefiningGeometry
{
    get
    {

        var a0 = StartAngle < 0 ? StartAngle + 2 * Math.PI : StartAngle;
        var a1 = EndAngle < 0 ? EndAngle + 2 * Math.PI : EndAngle;

        if (a1<a0)
        {
            a1 += Math.PI * 2;
        }

        SweepDirection d = SweepDirection.Counterclockwise;
        bool large;

        if (SmallAngle)
        {
            large = false;
            double t = a1;
            if ((a1-a0)>Math.PI)
            {
                d = SweepDirection.Counterclockwise;
            }
            else
            {
                d = SweepDirection.Clockwise;
            }


        }else{
            large = (Math.Abs(a1 - a0) < Math.PI);
        }

        Point p0 = Center + new Vector(Math.Cos(a0), Math.Sin(a0)) * Radius;
        Point p1 = Center + new Vector(Math.Cos(a1), Math.Sin(a1)) * Radius;


        List<PathSegment> segments = new List<PathSegment>(1);
        segments.Add(new ArcSegment(p1, new Size(Radius, Radius), 0.0, large, d, true));

        List<PathFigure> figures = new List<PathFigure>(1);
        PathFigure pf = new PathFigure(p0, segments, true);
        pf.IsClosed = false;
        figures.Add(pf);

        Geometry g = new PathGeometry(figures, FillRule.EvenOdd, null);
        return g;
    }
}
}
}  

添加这个namespace CustomControlTest 然后它包含类

【讨论】:

  • 我已经声明了命名空间,我没有将它包含在我上面发布的代码中。
  • 然后将此添加到您的窗口标签:&lt;window ..... ..... xmlns:local="clr-namespace:CustomControlTest" ...... ......&gt;@KiroYakuza
  • 能否请您发布您的&lt;window&gt; 开始标签以及您在C# 代码后面的代码,从第一次使用到类定义,并确保您添加了引用System.Drawing
猜你喜欢
  • 1970-01-01
  • 2016-08-01
  • 2012-04-02
  • 2012-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-28
相关资源
最近更新 更多