Xamarin 向XAML中的行为传递对象无效

Xamarin 向XAML中的行为传递对象无效,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我有一个混合类型(int和string、url、ip地址等)的设置列表,允许验证这些设置的参数与对象中的实际值一起存储。此对象从设置列表中选择,并传递到可以修改值的详细信息页面 在详细信息页面上,我有一个条目控件,其中包含需要验证的值。该条目从ViewModel属性SettingItem获取其初始值。这个很好用 我有一个行为,我将使用它来动态验证用户输入的值,但是,当HandleTextChanged方法被激发时,ItemToValidate为null。我不明白为什么 XAML: <?xm

我有一个混合类型(int和string、url、ip地址等)的设置列表,允许验证这些设置的参数与对象中的实际值一起存储。此对象从设置列表中选择,并传递到可以修改值的详细信息页面

在详细信息页面上,我有一个条目控件,其中包含需要验证的值。该条目从ViewModel属性SettingItem获取其初始值。这个很好用

我有一个行为,我将使用它来动态验证用户输入的值,但是,当HandleTextChanged方法被激发时,ItemToValidate为null。我不明白为什么

XAML:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="DwyApp.Views.ItemDetailPage"
         xmlns:local="clr-namespace:DwyApp.Behaviours"
         Title="{Binding Title}">
<StackLayout Spacing="20" Padding="15">
    <Label Text="{Binding SettingItem.ParameterName}" FontSize="Medium" FontAttributes="Bold"/>
    <Entry Text="{Binding SettingItem.Value}" FontSize="Medium">
        <Entry.Behaviors>
            <local:ValidateBehaviour ItemToValidate="{Binding SettingItem}" />
        </Entry.Behaviors>
    </Entry>
    <Label x:Name="Value_Error" Text="{Binding SettingItem.ValidationMessage}" FontSize="Medium" IsVisible="{Binding SettingItem.ValidateValue}" TextColor="Red" />
    <Button Text="Save"
            VerticalOptions="Center"
            HorizontalOptions="Center"/>
    <Label Text="{Binding SettingItem.Description}" FontSize="Medium"/>
</StackLayout>

行为:

using DwyApp.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Xamarin.Forms;

namespace DwyApp.Behaviours
{
public class ValidateBehaviour:Behavior<Entry>

{

    public static readonly BindableProperty ItemToValidateProperty = BindableProperty.Create(nameof(ItemToValidate), 
                                                                                            typeof(SettingItem), 
                                                                                            typeof(ValidateBehaviour), 
                                                                                            defaultValue: null);

    public SettingItem ItemToValidate
    {
        get {
            return (SettingItem)GetValue(ItemToValidateProperty);
        }
        set {
            SetValue(ItemToValidateProperty, value);
        }
    }


    protected override void OnAttachedTo(Entry bindable)
    {
        bindable.TextChanged += HandleTextChanged;
        base.OnAttachedTo(bindable);
    }

    void HandleTextChanged(object sender, TextChangedEventArgs e)
    {
        bool IsValid = false;
    }

    protected override void OnDetachingFrom(Entry bindable)
    {
        bindable.TextChanged -= HandleTextChanged;
        base.OnDetachingFrom(bindable);
    }

}
使用DwyApp.Models;
使用制度;
使用System.Collections.Generic;
使用系统诊断;
使用系统文本;
使用Xamarin.Forms;
名称空间DwyApp.behaviors
{
公共类验证行为:行为
{
public static readonly BindableProperty ItemToValidateProperty=BindableProperty.Create(nameof(ItemToValidate),
类型(设置项),
类型(验证行为),
默认值:空);
公共设置项ItemToValidate
{
得到{
返回(SettingItem)GetValue(ItemToValidateProperty);
}
设置{
SetValue(ItemToValidateProperty,value);
}
}
受保护的覆盖无效数据到(条目可绑定)
{
bindable.TextChanged+=HandleTextChanged;
碱。可粘合的DTO(可粘合);
}
void HandleTextChanged(对象发送者,textchangedventargs e)
{
bool IsValid=false;
}
受保护的覆盖无效OnDetachingFrom(条目可绑定)
{
bindable.TextChanged-=HandleTextChanged;
基础。从(可装订)开始连接;
}
}
}


HandleTextChanged处的断点始终命中,但ItemToValidate的值始终为null。有什么想法吗?

将您的绑定更改为以下内容:

<Entry x:Name="EntryValue" Text="{Binding SettingItem.Value}" FontSize="Medium">
    <Entry.Behaviors>
        <local:ValidateBehaviour                            
                ItemToValidate="{Binding BindingContext.SettingItem, Source={x:Reference EntryValue}}" />
    </Entry.Behaviors>
</Entry>

如您所见,我在条目上添加了一个
x:Name
,并使用该值告诉
行为上的绑定它的
源代码将是什么


希望这有帮助。-

将您的绑定更改为以下内容:

<Entry x:Name="EntryValue" Text="{Binding SettingItem.Value}" FontSize="Medium">
    <Entry.Behaviors>
        <local:ValidateBehaviour                            
                ItemToValidate="{Binding BindingContext.SettingItem, Source={x:Reference EntryValue}}" />
    </Entry.Behaviors>
</Entry>

如您所见,我在条目上添加了一个
x:Name
,并使用该值告诉
行为上的绑定它的
源代码将是什么


希望这有帮助。-

您是否在ItemToValidate的set方法上设置了一个断点,并查看是否正在调用它,如果是,是否将其设置为null?jazzmasterkc。是的,我试过了。它没有被命中。您是否在ItemToValidate的set方法上设置了一个断点,并查看它是否被调用,如果被调用,它是否被设置为null?jazzmasterkc。是的,我试过了。它没有被击中。