简体   繁体   English

如何在Xamarin Forms中将子视图的属性绑定到当前视图模型的属性

[英]How to Bind a childview's property to a property of the current viewmodel in Xamarin Forms

I'm trying to build a custom numpadcontrol in xamarin forms and I think I am getting something totally wrong here because the only result of my work are confusing exceptions. 我正在尝试以xamarin形式构建自定义numpadcontrol,并且我觉得这里出现了完全错误的地方,因为我的工作的唯一结果就是使异常混乱。

This is my numpad-views xaml: 这是我的numpad-views xaml:

    <?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="XFormsControls.Numpad">
  <ContentView.Content>
        <Grid>
            <Label BackgroundColor="Silver" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" Text="{Binding Value}" HorizontalTextAlignment="End" />
            <Button Grid.Row="1" Grid.RowSpan="2" Grid.Column="0" Text="+1" />
            <Button Grid.Row="3" Grid.RowSpan="2" Grid.Column="0" Text="-1" />

            <Button Grid.Row="1" Grid.Column="1" Text="1" />
            <Button Grid.Row="1" Grid.Column="2" Text="2" />
            <Button Grid.Row="1" Grid.Column="3" Text="3" />

            <Button Grid.Row="2" Grid.Column="1" Text="4" />
            <Button Grid.Row="2" Grid.Column="2" Text="5" />
            <Button Grid.Row="2" Grid.Column="3" Text="6" />

            <Button Grid.Row="3" Grid.Column="1" Text="7" />
            <Button Grid.Row="3" Grid.Column="2" Text="8" />
            <Button Grid.Row="3" Grid.Column="3" Text="9" />

            <Button Grid.Row="4" Grid.Column="1" Text="Trash" />
            <Button Grid.Row="4" Grid.Column="2" Text="0" />
            <Button Grid.Row="4" Grid.Column="3" Text="Ok" />
        </Grid>
  </ContentView.Content>
</ContentView>

and my numpad.xaml.cs: 和我的numpad.xaml.cs:

public partial class Numpad : ContentView
{
    private double? _value { get; set; }
    public double? Value
    {
        get
        {
            return _value;
        }
        set
        {
            if (_value != value)
            {
                _value = value;
                OnPropertyChanged();
            }
        }
    }

    public Numpad()
    {

        InitializeComponent();
        BindingContext = this;
    }
}

I want to be able to load this control from any other page or view and just bind the control's Value property to any property of a viewmodel. 我希望能够从任何其他页面或视图加载此控件,并将该控件的Value属性绑定到viewmodel的任何属性。 So I tried to make the Value property of my control a Bindable Property like this: 因此,我尝试将控件的Value属性设置为可绑定属性,如下所示:

        public static readonly BindableProperty valueProperty = BindableProperty.Create(
                                                        propertyName: "Value",
                                                        returnType: typeof(double),
                                                        declaringType: typeof(Numpad),
                                                        defaultValue: 0,
                                                        defaultBindingMode: BindingMode.TwoWay);

    public double Value
    {
        get { return (double)GetValue(valueProperty); }
        set
        {
            SetValue(valueProperty, value);
        }
    }

and tried to bind it to a property of the containing Page in XAML like this: 并尝试将其绑定到XAML中包含页面的属性,如下所示:

<local:Numpad Value="{Binding selectedValue}"></local:Numpad>

the selectedValue property of my ContentPage is defined like that (in codebehind): ContentPage的selectedValue属性的定义如下(在代码后面):

private double _selectedValue = 25;
public double selectedValue
{
    get
    {
        return _selectedValue;
    }
    set
    {
        _selectedValue = value;
        OnPropertyChanged();
    }
}

On my UWP-Localmachine debugging it throws a "System.Reflection.TargetInvocationException" (thrown by the the target of an invocation) ??dafuq? 在我的UWP-Localmachine调试中,它抛出“ System.Reflection.TargetInvocationException”(由调用的目标抛出)?

Or this exception when I run on iOS Simulator: 或在iOS模拟器上运行时出现以下异常: 在此处输入图片说明

I tried for hours now and then i thought to turn things around and make my "selectedValue" of the parent class a bindable property, that didn't work either. 我试了好几个小时,然后才想好转一下,然后将父类的“ selectedValue”设置为可绑定的属性,但这也不起作用。 It also makes no sense to me to turn it around because I can bind to any property of a viewmodel without making them bindable properties, or am I wrong here? 对我来说,也无济于事,因为我可以绑定到视图模型的任何属性而无需使其具有可绑定的属性,或者我在这里错了吗?

I am new to Xamarin.Forms and I am coming from technologies like Angular 2, where stuff like that is just straight forward. 我是Xamarin.Forms的新手,我来自Angular 2之类的技术,而其中的东西很简单。

Please Help 请帮忙

(at the moment I am at phase two of the development process ) (目前我处于开发过程的第二阶段)

I have uploaded the Code to my Git-Repo 我已将代码上传到我的Git-Repo

Declaring Default value could've caused the problem other than that i don't see any difference. 声明默认值可能会导致问题,除了我没有看到任何区别。 Just try to change the declaration as below, hope that helps! 只需尝试如下更改声明,希望对您有所帮助!

private static double defaultValue = 0;

        public static readonly BindableProperty valueProperty = BindableProperty.Create(
            propertyName: "Value",
            returnType: typeof(double),
            declaringType: typeof(Numpad),
            defaultValue: defaultValue,
            defaultBindingMode: BindingMode.TwoWay);

I don't get any exceptions like you mentioned except XAML parse exception 除了XAML解析例外,我没有像您提到的任何例外 样本图片

So two things were problematic here, first, what @Dinesh kumar pointed out, I did not declare the binding property with the same name as the actual property, I wrote valueProperty instead of ValueProperty. 所以这里有两件事是有问题的,首先,@ Dinesh kumar指出,我没有声明与实际属性同名的绑定属性,而是编写了valueProperty而不是ValueProperty。

The second mistake was to set the BindingContext inside of the constructor of my numpad. 第二个错误是在numpad的构造函数中设置BindingContext。 I removed it and set x:name="this" on the top of my numpad.xaml. 我删除了它,并在numpad.xaml的顶部设置了x:name =“ this”。 Thanks to @AdamMeaney in the Xamarin Forum 感谢Xamarin论坛中的@AdamMeaney

He also posted a very usefull link to a example of how to build custom user controls with xamarin.forms. 他还发布了一个非常有用的链接,该链接指向示例如何使用xamarin.forms构建自定义用户控件的链接。

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

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