简体   繁体   English

Xamarin BindableProperty在自定义控件中

[英]Xamarin BindableProperty in custom control

I am using a bindable property in a custom control in order to set a property from the xaml code. 我在自定义控件中使用可绑定属性,以便从xaml代码设置属性。 However, it seems like my property always will get the default value that I've specified for the bindable property. 但是,似乎我的属性始终会获得为可绑定属性指定的默认值。

My xaml code: 我的XAML程式码:

<controls:MyView ID="4" />

My code behind: 我后面的代码:

public partial class MyView : ContentView
{
    public static readonly BindableProperty IDProperty = BindableProperty.Create(
                                    nameof(ID),
                                    typeof(int),
                                    typeof(MyView),
                                    15);
    public int ID
    {
        get
        {
             return (int)GetValue(IDProperty);
        }
        set
        {
            SetValue(IDProperty, value);
        }
    }

    private MyViewViewModel viewModel;

    public MyView()
    {
        InitializeComponent();
        viewModel = new MyViewViewModel() {};
        BindingContext = viewModel; 
    }
}

I expect that my property should get value 4 in this example, but it always get the default value 15. Should the property be set in the constructor or later? 我希望在此示例中,我的属性应获得值4,但始终获得默认值15。该属性应在构造函数中还是在更高版本中设置?

What am I doing wrong? 我究竟做错了什么?

Why do you embed a ViewModel inside your custom control? 为什么要在自定义控件中嵌入ViewModel

It is weird and even wrong. 这很奇怪,甚至是错误的。 The idea behind a custom control is that you could reuse and bind it to the parent's ViewModel . 自定义控件背后的想法是,您可以重用并将其绑定到父级的ViewModel Think of a simple Button control, it is reusable by simple placing it on the screen and setting the BindableProperties like Text , Command and etc. It is working because it's BindingContext by default is the same as it's parent. 想想一个简单的Button控件,只需将其放在屏幕上并设置TextCommandBindableProperties重用。它之所以起作用是因为默认情况下,它的BindingContext与它的父级相同。

In your case you sort of isolate your control from any modifications, since you set the BindingContext to a private custom ViewModel class. 在这种情况下,由于将BindingContext设置为私有自定义ViewModel类,因此可以将控件与任何修改区ViewModel You have to rethink your solution. 您必须重新考虑您的解决方案。

It should be as simple as: 它应该很简单:

public partial class MyView : ContentView
{
    public static readonly BindableProperty IDProperty = BindableProperty.Create(
                                    nameof(ID),
                                    typeof(int),
                                    typeof(MyView),
                                    15);
    public int ID
    {
        get => (int)GetValue(IDProperty);
        set => SetValue(IDProperty, value);
    }

    public MyView()
    {
        InitializeComponent();
    }
}

You don't need Bindable property if your are not binding , Just Create a Normal Property of type int With ID as property name.And then you can assign the ID from XAML.(Intellisense will also show the ID property) 如果您没有绑定,则不需要Bindable属性,只需创建一个类型为int的Normal属性,并将ID作为属性名称即可,然后可以从XAML分配ID(Intellisense还将显示ID属性)

public int ID
{
    get;set;
}

In you xaml , do 在你里面,做

<controls:MyView ID="{Binding Id}" />

And then in ViewModel, Create a porperty called Id 然后在ViewModel中,创建一个名为Id的属性

public int Id {get; set;} = 4;

I see it's too late, but i have been suffering for a while now, that - for a reason that i don't know - a ContentView(Custom view element) won't bind when you set it's BindingContext property in any way other than this: 我认为为时已晚,但是我已经受了一段时间了,由于我不知道的原因,当您以其他方式设置ContentView(Custom view element)的BindingContext属性时,它不会绑定这个:

<ContentView x:Class="mynamespace.CustomViews.MyView"
....
 x:Name="this">

then on the main container element (in my case a frame) set the BindingContext 然后在主容器元素(在我的情况下为框架)上设置BindingContext

<Frame BindingContext="{x:Reference this}"
....>

Setting the BindingContext in the constructor - in MyView.xaml.cs - does not work, while this way - and other ways - work in binding a View to another class (a ViewModel mainly), it does not - i repeat - work in binding ContentView to it's codeBehind.cs file. 构造函数中设置BindingContext (在MyView.xaml.cs中)不起作用,而这种方式(以及其他方式)可以将View绑定到另一个类(主要是ViewModel),但不能(我重复)在绑定中起作用ContentView到它的codeBehind.cs文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM