gpt4 book ai didi

Xamarin.Forms: Bindable Property in CustomView not hit(Xamarin.Forms:CustomView中的Bindable属性未命中)

转载 作者:bug小助手 更新时间:2023-10-25 19:03:01 25 4
gpt4 key购买 nike



I define a CustomView with one Bindable Property. To make things clearer, I removed all code unnessesary to understand my problem.
This is my XAML:

我定义了一个具有一个可绑定属性的CustomView。为了让事情更清楚,我删除了理解我的问题所需的所有代码。这是我的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"
xmlns:vm="clr-namespace:Packer.ViewModels"
x:DataType="vm:ContainerItemViewModel"
x:Class="Packer.CustomViews.ContainerView">
<ContentView.BindingContext>
<vm:ContainerItemViewModel/>
</ContentView.BindingContext>
<StackLayout Orientation="Vertical">
<Label Text="Hello World!" />
</StackLayout>
</ContentView>

...and this is my code behind:

...这是我后面的代码:


using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Packer.CustomViews
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ContainerView : ContentView
{
public static readonly BindableProperty ItemIdProperty =
BindableProperty.Create(nameof(ItemId), typeof(int), typeof(ContainerView), -1);

public ContainerView()
{
InitializeComponent();
}

public int ItemId
{
get => (int)GetValue(ItemIdProperty);
set
{
SetValue(ItemIdProperty, value);
}
}
}
}

With xmlns:custom="clr-namespace:Packer.CustomViews" This CustomView is called as follows:

使用xmlns:custom=“clr-namespace:Packer.CustomViews”时,此自定义视图的调用方式如下:


<StackLayout Orientation="Horizontal">
<custom:ContainerView HorizontalOptions="FillAndExpand" ItemId="{Binding ItemId}" />
<Label Text="{Binding ItemId}" />
</StackLayout>

Debugging this app as UWP, I can't see property ItemId being hit. However, the label following the Custom View is displayed properly, as well as the "Hello World" Label defined in the Custom View.

将此应用程序调试为UWP,我看不到属性ItemID被命中。但是,可以正确显示自定义视图后面的标签以及在自定义视图中定义的“Hello World”标签。


This is my configuration:

这是我的配置:


enter image description here


更多回答

May I ask what's the BindingContext for the page which consumes the ContainerView?

我可以问一下使用ContainerView的页面的BindingContext是什么吗?

ContainerView seems to be fine, I have a feeling this has to do with your Page's binding context or the binding itself

ContainerView似乎很好,我有一种感觉,这与页面的绑定上下文或绑定本身有关

Hi, I made some updates to explain my answer. Please consider accepting the answer if you think it is helpful! Thanks in advance!

嗨,我做了一些更新来解释我的答案。如果您认为答案有帮助,请考虑接受!提前谢谢!

优秀答案推荐

Updates:

最新情况:


When you set this in xaml:

在XAML中设置此项时:


<ContentView.BindingContext>
<vm:ContainerItemViewModel/>
</ContentView.BindingContext>

It just like you set this in page:

就像你在第页中设置的一样:


<custom:ContainerView BindingContext=ContainerViewModel HorizontalOptions="FillAndExpand" ItemId="{Binding ItemId}" />

Of course we could not get the ItemId property change because we maually set the BindingContext to ContainerViewModel (We know that ItemId is not in it).

当然,我们无法更改Itemid属性,因为我们主要将BindingContext设置为ContainerViewModel(我们知道Itemid不在其中)。


But when I set

但当我把


Content.BindingContext = new ContainerItemViewModel();

It means we just set the BindingContext for the ContainerView's Content. We could also receive the changes of ItemId.

这意味着我们只为ContainerView的内容设置了BindingContext。我们也可以收到伊特米德的变化。




If you want to Detect property changes for BindableProperty, you could use a callback method instead of adding some logic in Setter.

如果要检测BindableProperty的属性更改,可以使用回调方法,而不是在Setter中添加一些逻辑。


public static readonly BindableProperty ItemIdProperty =
BindableProperty.Create(nameof(ItemId), typeof(int), typeof(ContainerView), -1,propertyChanged:OnItemIdPropertyChanged);

private static void OnItemIdPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var a = (int)newValue;
}

I don't know why you use a ContainerItemViewModel for ContainerView. Because the BindingContext can inherit from the parent page. If you still want to do so, you could delete:

我不知道您为什么对ContainerView使用ContainerItemViewModel。因为BindingContext可以从父页继承。如果您仍想这样做,您可以删除:


<!--<ContentView.BindingContext>
<vm:ContainerItemViewModel/>
</ContentView.BindingContext>-->

And set it in code-behind like this:

并在代码隐藏中设置它,如下所示:


public ContainerView()
{
InitializeComponent();
// Notice here, please set Content.BindingContext here
Content.BindingContext = new ContainerItemViewModel();
}

Now if you set a breakpoint in OnItemIdPropertyChanged method, and it will hit the code. If you still have any question, feel free to ask.

现在,如果您在OnItemIdPropertyChanged方法中设置断点,它将命中代码。如果你还有任何问题,请随时提问。


Hope it helps!

希望它能帮上忙!



Solution:

解决方案:


Liqun Shen-MSFT's answer solves my problem.

沈立群的回答解决了我的问题。


However, the breakpoint in the ItemId setter is not hit but event OnItemIdChanged:

但是,未命中Itemid setter中的断点,而是事件OnItemIdChanged:


public static readonly BindableProperty ItemIdProperty =
BindableProperty.Create(nameof(ItemId), typeof(int), typeof(ContainerView), propertyChanged:OnItemIdChanged);

private static void OnItemIdChanged(BindableObject bindable, object oldValue, object newValue)
{
}

This is a strange behaviour that I did not expect.

这是一种我没想到的奇怪行为。


更多回答

In my answer, you could see I have set Content.BindingContext = new ContainerItemViewModel();, this helps set the BindingContext for the ContainerView. I don't understand why you just comment this line.

在我的回答中,您可以看到我已经设置了Content.BindingContext=new ContainerItemViewModel();,这有助于设置ContainerView的BindingContext。我不明白你为什么只评论这一行。

Hi, I think I haven't explained my answer clearly. So I add some explanations to my answer. Please have a look.

嗨,我想我还没有解释清楚我的答案。因此,我在我的回答中补充了一些解释。请看一看。

Ok, you are right. Because of a typo your suggestion seemed not to work for me at first. See my edits above.

好的,你是对的。因为一个打字错误,你的建议起初似乎对我不起作用。请看我上面的编辑。

I think this behaviour is by design. You could see some threads also talk about this: Setter of bindable property never fires, Setter of BindableProperty of Integer never called from xaml (Xamarin.forms)

我认为这种行为是故意的。您可以看到一些线程也在谈论这个问题:bindable属性的setter从不触发,Integer的BindableProperty的setter从不从XAML(Xamarin.Forms)调用

This worked, however, for all content pages I defined before. So I was confused.

然而,这对我之前定义的所有内容页面都有效。所以我很困惑。

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