gpt4 book ai didi

c# - Xamarin BindableProperty 仅在我不使用 BindingContext 时才使用绑定(bind)

转载 作者:行者123 更新时间:2023-12-05 05:40:58 25 4
gpt4 key购买 nike

在 Xamarin Forms 中,我创建了一个包含自定义组件的页面,我想像这样提供一个值:

<c:CustomComponent Test="{Binding Test}" />

然而,这不起作用。当我使用原始数字而不是 Binding 时,它会起作用。我发现的问题是,我的自定义组件使用了 ViewModel/BindingContext。因此,当我删除将我的 ViewModel 分配给 BindingContext 的行时,Binding 起作用。

为什么会这样,我如何在我的自定义组件中同时使用 BindingContext 和 BindableProperty?还是我必须在后面的代码中完成所有操作?

引用一些代码示例我是如何创建 BindableProperty

public static readonly BindableProperty TestProperty = BindableProperty.Create(nameof(Test), typeof(int),
typeof(CustomComponent), propertyChanged: (bindable, oldVal, newVal) => {
Debug.WriteLine("TEST " + newVal);
});

public int Test {
get => (int)GetValue(TestProperty);
set => SetValue(TestProperty, value);
}

最佳答案

"my custom component used a ViewModel / BindingContext."

创建可重用的自定义组件会更容易,如果它是“独立的”——没有 BindingContext。

构造函数:

public CustomComponent()
{
InitializeComponent();
}

将组件 View 模型中当前拥有的所有内容移动到 xaml.cs 代码隐藏文件中。

现在在 CustomComponent.xaml 中,为其命名(此处为 theComponent):

<ContentView ...
x:Name="theComponent"
x:Class=...>

当组件的 xaml 想要绑定(bind)到自身的属性时,这会派上用场:

<Label Text="{Binding TestString, Source={x:Reference theComponent}}" />
public string TestString
{
get => _testString;
set {
_testString = value;
OnPropertyChanged();
}
}
private string _testString = "test";

tl;dr: 如果组件在其 xaml 中有一个 x:Name="theComponent",可以使用 {Binding ..., Source={ x:Reference theComponent}},引用它自己的属性。不需要 BindingContext。


如果您希望组件具有关联的 ViewModel,则使用上述技术您不必将 BindingContext 设置为该 VM。这样做:

public class MyViewModel
{
public string TestString
{
get => _testString;
set {
_testString = value;
OnPropertyChanged();
}
}
private string _testString = "test";
}

自定义组件:

public MyViewModel VM { get; private set; }
public CustomComponent()
{
InitializeComponent();
VM = new MyViewModel();
}

xaml 中的用法:

<Label Text="{Binding VM.TestString, Source={x:Reference theComponent}}" />

关于c# - Xamarin BindableProperty 仅在我不使用 BindingContext 时才使用绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72338557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com