C# 如何绑定子视图';s属性转换为Xamarin表单中当前viewmodel的属性
我试图在xamarin表单中构建一个定制的numpadcontrol,我想我在这里完全弄错了,因为我工作的唯一结果是混淆异常 这是我的numpad视图xaml:C# 如何绑定子视图';s属性转换为Xamarin表单中当前viewmodel的属性,c#,binding,xamarin.forms,C#,Binding,Xamarin.forms,我试图在xamarin表单中构建一个定制的numpadcontrol,我想我在这里完全弄错了,因为我工作的唯一结果是混淆异常 这是我的numpad视图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:C
<?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>
我希望能够从任何其他页面或视图加载此控件,并将控件的Value属性绑定到viewmodel的任何属性。因此,我尝试将控件的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);
}
}
<local:Numpad Value="{Binding selectedValue}"></local:Numpad>
并尝试将其绑定到XAML中包含页面的属性,如下所示:
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);
}
}
<local:Numpad Value="{Binding selectedValue}"></local:Numpad>
在我的UWP Localmachine调试中,它抛出一个“System.Reflection.TargetInvocationException”(由调用的目标抛出)??dafuq
或者在iOS模拟器上运行时出现此异常:
我尝试了几个小时,然后我想扭转局面,让父类的“selectedValue”成为一个可绑定的属性,但也不起作用。对我来说,改变它也没有意义,因为我可以绑定到viewmodel的任何属性而不使它们成为可绑定属性,或者我错了吗
我是Xamarin的新手,我来自Angular 2这样的技术领域,在这里,类似的东西都是直截了当的
请帮忙
(目前我正处于项目的第二阶段)
我已将代码上载到我的中,声明默认值可能导致问题,但我看不出有任何区别。请尝试更改以下声明,希望对您有所帮助
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);
除了XAML解析异常之外,我没有任何您提到的异常
所以这里有两件事是有问题的,首先,@Dinesh kumar指出,我没有用与实际属性相同的名称声明绑定属性,我编写了valueProperty而不是valueProperty 第二个错误是在我的numpad的构造函数中设置BindingContext。我删除了它,并在numpad.xaml的顶部设置了x:name=“this”。多亏了
他还发布了一篇关于如何使用xamarin.forms构建自定义用户控件的文章。不,没有帮助。仍然有相同的异常:(@Weissvonnix您能从输出中附加异常的屏幕截图吗?如何将其绑定到父对象的属性?我已从包含我要绑定的selectedValue属性的contentpage的codebehind文件中添加了代码。@Weissvonnix对延迟表示抱歉,“ValueProperty”你得到的异常是因为属性和BindableProperty在声明方面不一样。V->应该是ValueProperty中的caps。现在项目应该运行了。到目前为止,很好,异常消失了,谢谢!不幸的是,我的Value-Property的设置程序现在没有被命中。即使我没有绑定到属性并写入与Value=“2”类似,该值永远不会出现在我的Numpad.Value属性中:(