Properties 如何在Windows Phone中将属性添加到用户控件

Properties 如何在Windows Phone中将属性添加到用户控件,properties,windows-phone-8,controls,windows-phone,Properties,Windows Phone 8,Controls,Windows Phone,所以我正在创建这个应用程序,在这里我创建了我自己的用户控件,每个控件都有一些东西可以用来自web的信息进行更新,但我不知道如何向它添加属性。就像那个特定控件的名称一样。例如,高度设置存储在控件中的什么位置?在代码中?像这样 mycontrol: String nameofthiscontrol = "control"; 和主要代码: mycontrol mycontrol = new mycontrol(); mycontrol.nameofthiscontrol = "control1";

所以我正在创建这个应用程序,在这里我创建了我自己的用户控件,每个控件都有一些东西可以用来自web的信息进行更新,但我不知道如何向它添加属性。就像那个特定控件的名称一样。例如,高度设置存储在控件中的什么位置?在代码中?像这样

mycontrol:

String nameofthiscontrol = "control";
和主要代码:

mycontrol mycontrol = new mycontrol();
mycontrol.nameofthiscontrol = "control1";

这就是它的工作原理吗?我真的需要一些关于这一点的指导,请帮助!提前谢谢

如果您谈论的是UserControl,那么它将具有所有控件所共有的一些基本属性(如宽度、高度、背景等)。您可以像添加其他任何地方一样添加属性—在您的UserControl中

public partial class MyControl : UserControl
{
    public MyControl()
    {
        InitializeComponent();
    }

    //simple property
    public DesiredType PropertyName { get; set; }

    //dependancy property
    public DesiredType MyProperty
    {
        get { return (DesiredType)GetValue(MyPropertyProperty); }
        set { SetValue(MyPropertyProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.Register("MyProperty", typeof(DesiredType), typeof(ownerclass), new PropertyMetadata(0));
}

两者都很有用(并且必须是公共的),但对于MVVM中的绑定来说更好。

如果您谈论的是UserControl,那么它将具有一些所有控件通用的基本属性(如宽度、高度、背景等)。您可以像添加其他任何地方一样添加属性—在您的UserControl中

public partial class MyControl : UserControl
{
    public MyControl()
    {
        InitializeComponent();
    }

    //simple property
    public DesiredType PropertyName { get; set; }

    //dependancy property
    public DesiredType MyProperty
    {
        get { return (DesiredType)GetValue(MyPropertyProperty); }
        set { SetValue(MyPropertyProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.Register("MyProperty", typeof(DesiredType), typeof(ownerclass), new PropertyMetadata(0));
}

这两种方法都很有用(并且必须是公共的),但对于MVVM中的绑定来说更好。

我不太理解代码,请您进一步解释一下好吗?属性基本上是get和set方法的包装器(有时是我第一个示例中的变量)。您编写此属性并将其作为任何其他现有属性调用。若您需要绑定它,并且当值更改时会发生一些事情,那个么请使用DependencyProperty,在这里您可以定义回调方法。下面是关于的更多信息。我不太理解代码,您能更深入地解释一下吗?属性基本上是get和set方法的包装器(有时是我第一个示例中的变量)。您编写此属性并将其作为任何其他现有属性调用。若您需要绑定它,并且当值更改时会发生一些事情,那个么请使用DependencyProperty,在这里您可以定义回调方法。这里有更多关于这个问题的信息。