1

I've been interested in WPF bindings in c# and tried to bind a control to the mouse position, but after getting a frustrating "The name "[control]" does not exist in the namespace "clr-namespace:[namespace]" every time I pasted the xaml code into the editor I decided it wasn't worth the time to investigate the quirk.

Now I am attempting to simply implement a binded Arc drawing example from Stack Overflow and am getting the same eror again. (All the code for a short runnable example can be found there)

So I've combed through all the Stack Overflow solution to this issue (it actually seems rather widespread) with what appear to be sporadic and inexpiable workarounds and fixes.


  1. This question says that

If nothing else is possible, comment the lines which use the namespace, rebuild, and then build the full project again.

I also tried rebuilding the project, reopening Visual Studio. Nothing helped. I finally commented xaml, rebuilt the project, uncommented xaml and it finally worked! Strange issue.

  1. This one said to set the project to release mode and another answer-er on the same question said to define the assembly: (xmlns:Local="clr-namespace:MusicPlayer.Controls;assembly=MusicPlayer") which did not work for me either.
  2. This person recommended changing the build target platform (x86 - x64)

I've tried pretty much all of these solution to no avail. ReSharper seems to know that Arc class exists in the namespace assigned to Local, but Visual Studio does not.

<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;
        }
    }
}
slugster
  • 49,403
  • 14
  • 95
  • 145
Bennett Yeo
  • 819
  • 2
  • 14
  • 28
  • When I try and to drag and drop the Arc class from the Visual Studio toolbox is comes up with a dialog box "The following reference has been added to project "[Namespace]. Version 1.0.0.0 Culture=neutral, PublicKeyToken=null" In order to use types from the new reference, press OK to restart the XAML designer. After the Designer restarts, re-create the control on the artboard" **before doing nothing** – Bennett Yeo May 25 '16 at 02:21
  • 2
    Arc class code isn't wrapped in a namespace. Could that be your problem? – RobCroll May 25 '16 at 02:25
  • @RobCroll I didn't post the code with my namespace, it is wrapped within a namespace. – Bennett Yeo May 25 '16 at 17:10
  • Check to make sure your project does not have conflicting namespaces and names. Although rare, sometimes this happens and it can give this error too. – Jai May 27 '16 at 04:02

2 Answers2

0

Define Arc inside a namespace as suggested by RobCroll. Check below code, I have included it in the Arc_Learning namespace which is referred in XAML as local.

    <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;
            }
        }
    }
}
Ayyappan Subramanian
  • 5,348
  • 1
  • 22
  • 44
  • I have the same code as you, except my namespace if different. When I hover over local:Arc, I see "The name "Arc" does not exist in the namespace "clr-namespace:CustomControlTest". Where CustomControlTest is my namespace. If I build and run, the Arc doesn't even show up. – Bennett Yeo May 25 '16 at 04:26
0

try to add local reference to your window as follow:

in the opening tag of your window add this reference

xmlns:local="clr-namespace:CustomControlTest"

in code behind C# Code you missing the namespace definition

add the missing namespace line before the class definition so your code should be like this:

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

add this namespace CustomControlTest and then it contains the class

JihadiOS
  • 381
  • 2
  • 13