English 中文(简体)
使用 " 依赖性财产 " 。 使用 " 依赖性财产 " 方法而不是 " 依赖性对象 " 上的简单的 " POCO " 属性获取者/订立者 " 的原因何在?
原标题:What is the reason for using DependencyProperty.AddOwner method instead of simple POCO property getters/setters on a DependencyObject?
  • 时间:2012-05-25 11:02:05
  •  标签:
  • c#
  • wpf

有人能解释一下,在WPF(完全属人财产)中使用“拥有者方法”有什么好处吗?我有些误解。

对于斯坦斯,看看下面的代码。

public class TestVisual: Shape
{
    private LineGeometry line = new LineGeometry();

    public static readonly DependencyProperty XY1Property =
        LineGeometry.StartPointProperty.AddOwner(
            typeof(TestVisual),
            new FrameworkPropertyMetadata(new Point(0,0),
                FrameworkPropertyMetadataOptions.AffectsMeasure));

    public static readonly DependencyProperty XY2Property =
        LineGeometry.EndPointProperty.AddOwner(
            typeof(TestVisual),
            new FrameworkPropertyMetadata(new Point(0, 0),
                FrameworkPropertyMetadataOptions.AffectsMeasure));


    public Point XY1
    {
        get { return (Point)GetValue(XY1Property);}
        set { SetValue(XY1Property,value); }
    }

    public Point XY2
    {
        get { return (Point)GetValue(XY2Property); }
        set { SetValue(XY2Property, value); }
    }


    protected override Geometry DefiningGeometry
    {
        get
        {
            line.StartPoint = XY1 ;
            line.EndPoint = XY2;                         
            return line;
        }
    }
}

从上面的代码中,您可以看到测试视觉类使用 AdPowerer 方法对抚养财产的使用。 好的, 但同样的结果我们可以更容易一些( 我的意思是我们可以得到一个可以调整线线目视觉元素的分类, 设置 XY1 和 XY2 点 ) :

    public class TestVisual: Shape
{
    private LineGeometry line = new LineGeometry();

    public Point XY1
    {
        get;
        set;
    }

    public Point XY2
    {
        get;
        set;
    }


    protected override Geometry DefiningGeometry
    {
        get
        {
            line.StartPoint = XY1 ;
            line.EndPoint = XY2;                         
            return line;
        }
    }
}

要点是什么?

最佳回答

要点是 XY1 XY2 在第一个版本中是依赖属性,第二个版本是简单的.NET属性。见为什么是依赖属性?

一般而言,AddPowerr 被用于提供一种依赖性财产,其类型最终并非来自拥有该依赖性财产的类型。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签