可以为派生类设置Visual.VisualEdgeMode属性,但不能将其设置为类成员

时间:2014-09-03 11:11:59

标签: c# wpf

在下面的代码中,我的LineAliased和LineAntiAliased类派生自DrawingVisual,我可以使用" this"用于设置" VisualEdgeMode"的关键字每个班级的财产。这工作正常,我可以按预期绘制到画布的行,一行别名,一行AntiAliased。

但是,在我的Lines类中,我不是从DrawingVisual派生而是为该类创建DrawingVisual成员。

问题在于,当我尝试设置" VisualEdgeMode"这些成员的属性我收到错误" System.Windows.Media.Visual.VisualEdgeMode由于其保护级别而无法访问"。

我很新,并且能够在其他地方找到一个合适的解释,说明我做错了什么以及为什么这样做不起作用。

MSDN上的Visual.VisualEdgeMode属性
http://msdn.microsoft.com/en-us/library/system.windows.media.visual.visualedgemode(v=vs.110).aspx

MSDN上的DrawingVisual类
http://msdn.microsoft.com/en-us/library/system.windows.media.drawingvisual(v=vs.110).aspx

谢谢!

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

    }
}

1 个答案:

答案 0 :(得分:0)

VisualEdgeMode属性为protected,因此只能从派生类(或定义类本身)的方法访问。

然而,您可以调用静态RenderOptions.SetEdgeMode

RenderOptions.SetEdgeMode(objDrawingVisualAliased, EdgeMode.Aliased);