【发布时间】:2016-09-22 10:41:24
【问题描述】:
我一直对 c# 中的 WPF 绑定感兴趣,并尝试将控件绑定到鼠标位置,但在得到令人沮丧的“名称“[control]”不存在于命名空间“clr-namespace:[namespace ]" 每次我将 xaml 代码粘贴到编辑器中时,我都认为不值得花时间研究这个怪癖。
现在我尝试简单地实现 binded Arc drawing example from Stack Overflow 并再次遇到相同的错误。 (可在此处找到所有可运行的简短示例的代码)
因此,我已经梳理了针对此问题的所有 Stack Overflow 解决方案(实际上似乎相当普遍),其中包含看似零星和无法弥补的变通方法和修复。
- This question 这么说
如果没有其他可能,请注释使用命名空间的行,重新构建,然后再次构建完整的项目。
我还尝试重建项目,重新打开 Visual Studio。没有 帮助。我终于评论了xaml,重建项目,取消评论 xml,它终于奏效了!奇怪的问题。
-
This one 说将项目设置为发布模式,另一个回答者说要定义程序集:(
xmlns:Local="clr-namespace:MusicPlayer.Controls;assembly=MusicPlayer"),这对我也不起作用。 - 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