c# - 如何修复 Xamarin.Forms 条目附加属性 "Bleed Over"?

标签 c# xaml mvvm xamarin.forms inotifydataerrorinfo

我正在尝试使用 INotifyDataErrorInfo 和附加属性 (AP) 为 Xamarin.Forms(XF) 条目设置验证。该代码仅使用一个条目时按预期工作。当我为第二个条目初始化 AP 时出现问题。然后,如果 Entry1 发生错误,它会显示在 Entry2 中。

我在 WPF 中使用了 INotifyDataErrorInfo,它的工作原理非常棒。唯一的问题是我找不到任何关于如何为 WPF 实现“验证”附加属性的文档。我已经查看了关于验证的其他替代方案,但似乎没有将 INotifyDataErrorInfo 与自定义模型包装器一起使用那么可靠。

绑定(bind)扩展(用于获取绑定(bind))

public static class BindingObjectExtensions
{
    public static Binding GetBinding(this BindableObject self, BindableProperty property)
    {
        var methodInfo = typeof(BindableObject).GetTypeInfo().GetDeclaredMethod("GetContext");
        var context = methodInfo?.Invoke(self, new[] { property });

        var propertyInfo = context?.GetType().GetTypeInfo().GetDeclaredField("Binding");
        return propertyInfo?.GetValue(context) as Binding;
    }

    public static object GetBindingExpression(this Binding self)
    {
        var fieldInfo = self?.GetType().GetTypeInfo().GetDeclaredField("_expression");
        return fieldInfo?.GetValue(self);
    }
}

附加属性

using {YourNamespace}.Extensions;
using System;
using System.Collections;
using System.ComponentModel;
using System.Linq;
using Xamarin.Forms;

public static class ValidationBehavior
{
    // Fields
    public static string _propertyName;
    public static BindableObject _view;

    public static readonly BindableProperty IsActiveProperty;
    public static readonly BindableProperty HasErrorProperty;
    public static readonly BindableProperty ErrorsProperty;

    // Constructor
    static ValidationBehavior()
    {
        IsActiveProperty = BindableProperty.CreateAttached("IsActive", typeof(bool), typeof(ValidationBehavior), default(bool), propertyChanged: OnIsActivePropertyChanged);
        ErrorsProperty = BindableProperty.CreateAttached("Errors", typeof(IList), typeof(ValidationBehavior), null);
        HasErrorProperty = BindableProperty.CreateAttached("HasError", typeof(bool), typeof(ValidationBehavior), default(bool));
    }


    // Properties
    #region IsActive Property
    public static bool GetIsActive(BindableObject obj)
    {
        return (bool)obj.GetValue(IsActiveProperty);
    }
    public static void SetIsActive(BindableObject obj, bool value)
    {
        obj.SetValue(IsActiveProperty, value);
    }
    #endregion

    #region Errors Property
    public static IList GetErrors(BindableObject obj)
    {
        return (IList)obj.GetValue(ErrorsProperty);
    }
    public static void SetErrors(BindableObject obj, IList value)
    {
        obj.SetValue(ErrorsProperty, value);
    }
    #endregion

    #region HasError Property
    public static bool GetHasError(BindableObject obj)
    {
        return (bool)obj.GetValue(HasErrorProperty);
    }
    public static void SetHasError(BindableObject obj, bool value)
    {
        obj.SetValue(HasErrorProperty, value);
    }
    #endregion


    // Methodes
    private static void OnIsActivePropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        if ((bool)newValue)
        {
            var binding = bindable.GetBinding(Entry.TextProperty); 
            if (binding != null)
            {
                string bindingPath = binding.Path;
                _propertyName = bindingPath.Split('.').Last();
                bindable.BindingContextChanged += Bindable_BindingContextChanged;
            }
        }
        else
        {
            _propertyName = null;
            bindable.BindingContextChanged -= Bindable_BindingContextChanged;
        }
    }

    private static void Bindable_BindingContextChanged(object sender, EventArgs e)
    {
        var bindable = sender as BindableObject;
        if (bindable == null)
        {
            _view = null;
            return;
        }
        else
        {
            _view = bindable;
        }

        var errorInfo = bindable.BindingContext as INotifyDataErrorInfo;
        if (errorInfo == null)
            return;

        errorInfo.ErrorsChanged += ErrorInfo_ErrorsChanged; // NB! Not sure if this will create memory leak
    }

    private static void ErrorInfo_ErrorsChanged(object sender, DataErrorsChangedEventArgs e)
    {
        if (e.PropertyName != _propertyName)
            return;

        var errorInfo = sender as INotifyDataErrorInfo;
        if (errorInfo == null)
            return;

        if (!errorInfo.HasErrors)
        {
            SetErrors(_view, null);
            SetHasError(_view, false);
        }
        else
        {
            var foundErrors = errorInfo.GetErrors(e.PropertyName);
            if (foundErrors == null)
            {
                SetErrors(_view, null);
                SetHasError(_view, false);
            }
            else
            {
                SetErrors(_view, foundErrors.Cast<string>().ToList());
                SetHasError(_view, true);
            }
        }
    }
}

入口风格

<Style TargetType="Entry" x:Key="EntryInput">
    <Setter Property="b:ChangeValidationBehavior.IsActive" Value="True"/>
    <Style.Triggers>
        <Trigger Property="b:ChangeValidationBehavior.IsChanged" Value="True" TargetType="Entry">
            <Setter Property="BackgroundColor" Value="SteelBlue"/>
        </Trigger>
        <Trigger Property="b:ChangeValidationBehavior.HasError" Value="True" TargetType="Entry">
            <Setter Property="BackgroundColor" Value="LightCoral"/>
        </Trigger>
    </Style.Triggers>
</Style>

View 中的条目

<Entry BindingContext="{Binding Project}" Text="{Binding Name, Mode=TwoWay}"
       Grid.Column="1" Style="{StaticResource EntryInput}"/>

<Entry BindingContext="{Binding Project}" Text="{Binding Description, Mode=TwoWay}"
       Grid.Row="3" Grid.Column="1" Style="{StaticResource EntryInput}"/>

我期望发生的是我应该能够为多个 Entry 控件激活此 ValidationBehavior,然后显示每个控件的错误。

我猜这与 AP 是静态类以及属性也是静态类这一事实有关,但后来我有点迷路了,因为如果这是正确的,那么附加属性有什么意义。 .

有没有人知道如何做到这一点?

最佳答案

我恍然大悟.. _propertyName 和 _view 只是静态字段..

所以现在的解决方案是创建 _propertyName 作为附加属性并将“ErrorInfo_ErrorsChanged”方法编写为 lamda 表达式。

代码如下:

using {YourNamespace}.Extensions;
using System;
using System.Collections;
using System.ComponentModel;
using System.Linq;
using Xamarin.Forms;

public static class ValidationBehavior
{
    // Fields
    public static readonly BindableProperty IsActiveProperty;
    public static readonly BindableProperty HasErrorProperty;
    public static readonly BindableProperty ErrorsProperty;
    public static readonly BindableProperty PropertyNameProperty;

    // Constructor
    static ValidationBehavior()
    {
        IsActiveProperty = BindableProperty.CreateAttached("IsActive", typeof(bool), typeof(ValidationBehavior), default(bool), propertyChanged: OnIsActivePropertyChanged);
        ErrorsProperty = BindableProperty.CreateAttached("Errors", typeof(IList), typeof(ValidationBehavior), null);
        HasErrorProperty = BindableProperty.CreateAttached("HasError", typeof(bool), typeof(ValidationBehavior), default(bool));
        PropertyNameProperty = BindableProperty.CreateAttached("PropertyName", typeof(string), typeof(ChangeValidationBehavior), default(string));
    }


    // Properties
    #region IsActive Property
    public static bool GetIsActive(BindableObject obj)
    {
        return (bool)obj.GetValue(IsActiveProperty);
    }
    public static void SetIsActive(BindableObject obj, bool value)
    {
        obj.SetValue(IsActiveProperty, value);
    }
    #endregion

    #region Errors Property
    public static IList GetErrors(BindableObject obj)
    {
        return (IList)obj.GetValue(ErrorsProperty);
    }
    public static void SetErrors(BindableObject obj, IList value)
    {
        obj.SetValue(ErrorsProperty, value);
    }
    #endregion

    #region HasError Property
    public static bool GetHasError(BindableObject obj)
    {
        return (bool)obj.GetValue(HasErrorProperty);
    }
    public static void SetHasError(BindableObject obj, bool value)
    {
        obj.SetValue(HasErrorProperty, value);
    }
    #endregion

    #region PropertyName Property
    public static string GetPropertyName(BindableObject obj)
    {
        return (string)obj.GetValue(PropertyNameProperty);
    }
    public static void SetPropertyName(BindableObject obj, string value)
    {
        obj.SetValue(PropertyNameProperty, value);
    }
    #endregion


    // Methodes
    private static void OnIsActivePropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        if ((bool)newValue)
        {
            var binding = bindable.GetBinding(Entry.TextProperty); 
            if (binding != null)
            {
                string bindingPath = binding.Path;
                string propertyName = bindingPath.Split('.').Last();
                SetPropertyName(bindable, propertyName);
                bindable.BindingContextChanged += Bindable_BindingContextChanged;
            }
        }
        else
        {
            SetPropertyName(bindable, null);
            bindable.BindingContextChanged -= Bindable_BindingContextChanged;
        }
    }

    private static void Bindable_BindingContextChanged(object sender, EventArgs e)
    {
        var bindable = sender as BindableObject;
        if (bindable == null)
            return;

        var errorInfo = bindable.BindingContext as INotifyDataErrorInfo;
        if (errorInfo == null)
            return;

        // NB! Not sure if this will create memory leak
        errorInfo.ErrorsChanged += (s, ea) =>
        {
            if (ea.PropertyName != GetPropertyName(bindable))
                return;

            var info = s as INotifyDataErrorInfo;
            if (info == null)
                return;

            if (!info.HasErrors)
            {
                SetErrors(bindable, null);
                SetHasError(bindable, false);
            }
            else
            {
                var foundErrors = info.GetErrors(ea.PropertyName);
                if (foundErrors == null)
                {
                    SetErrors(bindable, null);
                    SetHasError(bindable, false);
                }
                else
                {
                    SetErrors(bindable, foundErrors.Cast<string>().ToList());
                    SetHasError(bindable, true);
                }
            }
        };
    }
}

这可能不是“正确的方法”,但它确实有效。 非常感谢对代码的任何见解或改进。

希望对大家有帮助:)

关于c# - 如何修复 Xamarin.Forms 条目附加属性 "Bleed Over"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56946072/

相关文章:

c# - 如何在没有 Windows 窗体引用的情况下获得屏幕分辨率?

c# - Html 敏捷包使用

c# - 如何在 C# 中按名称查找动态创建的 XAML 组件?

c# - 根据内容调整新窗口的大小,并相对于主窗口居中

android - 有没有更好的方法将私有(private) MutableLiveData 公开为 ViewModel 的 LiveData。 [安卓、 Kotlin ]

c# - Linq2Sql 包含

C# Groupby 然后在拆分 CSV 后求和(无标题)

c# - 控制不同的DataContext

c# - 使用 Caliburn.Micro 同时显示两个 WPF 窗口

c# - 在ObservableCollection中为ComboBox创建事件(选中/未选中)