在下面的代碼中,我的LineAliased和LineAntiAliased類派生自DrawingVisual,我可以使用「this」關鍵字來設置「VisualEdgeMode」屬性爲每個班級。這工作正常,我可以按預期的方式將畫線繪製到畫布上,其中一行是別名,一行是AntiAliased。Visual.VisualEdgeMode屬性可以爲派生類設置,但不能作爲類成員設置
但是,在我的Lines類中,我不是從DrawingVisual派生的,而是爲該類創建DrawingVisual成員。
問題是,當我嘗試爲這些成員設置「VisualEdgeMode」屬性時,出現「System.Windows.Media.Visual.VisualEdgeMode由於其保護級別而無法訪問」的錯誤。
我很新,一直沒有找到一個體面的解釋我在做什麼錯了,爲什麼這是行不通的。
Visual.VisualEdgeMode財產上的MSDN
http://msdn.microsoft.com/en-us/library/system.windows.media.visual.visualedgemode(v=vs.110).aspx
DrawingVisual類MSDN上
http://msdn.microsoft.com/en-us/library/system.windows.media.drawingvisual(v=vs.110).aspx
THANK YOU!
using System;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows;
namespace CanvasTest
{
public class LineAliased : DrawingVisual
{
public LineAliased()
{
this.VisualEdgeMode = EdgeMode.Aliased;
DrawingContext dc = this.RenderOpen();
dc.DrawLine(new Pen(new SolidColorBrush(Colors.Red), 1), new Point(100, 105), new Point(300, 105));
dc.Close();
}
}
public class LineAntiAliased : DrawingVisual
{
public LineAntiAliased()
{
this.VisualEdgeMode = EdgeMode.Unspecified;
DrawingContext dc = this.RenderOpen();
dc.DrawLine(new Pen(new SolidColorBrush(Colors.Red), 1), new Point(100, 100), new Point(300, 100));
dc.Close();
}
}
public class Lines
{
public DrawingVisual objDrawingVisualAliased = new DrawingVisual();
public DrawingVisual objDrawingVisualAntiAliased = new DrawingVisual();
public Lines()
{
// Next line gives following error.
// System.Windows.Media.Visual.VisualEdgeMode is inaccessible due to its protection level.
objDrawingVisualAliased.VisualEdgeMode = EdgeMode.Aliased;
DrawingContext dc1 = objDrawingVisualAliased.RenderOpen();
dc1.DrawLine(new Pen(new SolidColorBrush(Colors.Red), 1), new Point(100, 105), new Point(300, 105));
dc1.Close();
// Next line gives following error.
// System.Windows.Media.Visual.VisualEdgeMode is inaccessible due to its protection level.
objDrawingVisualAntiAliased.VisualEdgeMode = EdgeMode.Unspecified;
DrawingContext dc2 = objDrawingVisualAntiAliased.RenderOpen();
dc2.DrawLine(new Pen(new SolidColorBrush(Colors.Red), 1), new Point(100, 105), new Point(300, 105));
dc2.Close();
}
}
public class DrawingCanvas : Canvas
{
// This object is used to work with the Visuals belonging to the Canvas.
private VisualCollection graphicsList;
public DrawingCanvas()
{
graphicsList = new VisualCollection(this);
graphicsList.Add(new LineAliased());
graphicsList.Add(new LineAntiAliased());
Lines lines = new Lines();
graphicsList.Add(lines.objDrawingVisualAliased);
graphicsList.Add(lines.objDrawingVisualAntiAliased);
}
// http://msdn.microsoft.com/en-us/library/ms742254.aspx#creating_overrides
// Provide required overrides
protected override int VisualChildrenCount { get { return graphicsList.Count; } }
protected override Visual GetVisualChild(int index)
{
if (index < 0 || index >= graphicsList.Count) { throw new ArgumentOutOfRangeException(); }
return graphicsList[index];
}
}
}
A [保護](http://msdn.microsoft.com/en-us/library/bcd5672a.aspx)成員只在可訪問其類和派生類實例。 – DmitryG 2014-09-03 11:24:04