支持基本类型和可为 null 类型的两种方式可绑定条目

Ton*_*sky 5 c# mvvm xamarin xamarin.forms

我试图在 Xamarin.Forms 中通过 Entry 实现完美的 MVVM。

我的模型包含类型包括 string、int?、decimal?、bool? 等的属性。每当我绑定到字符串类型时,两种方式绑定都会起作用,因为文本属性具有字符串类型(它们匹配)。但是,一旦您尝试绑定回模型并且属性是 int 或 int?,它就不会更新模型的属性值。

在我的研究过程中,在 Xamarin 支持的帮助下,这是一个关于如何处理可为 null 类型的非常有用的线程:

x:TypeArguments 中的可为空类型

XAML 代码:

<controls:NullableIntEntry Grid.Column="1" Grid.Row="14" NumericText="{Binding BusinessOwnership, Mode=TwoWay}" x:Name="lblBusinessOwnership"></controls:NullableIntEntry>
Run Code Online (Sandbox Code Playgroud)

BindableEntry(条目扩展)代码:

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Reflection;
using Xamarin.Forms;

namespace CreditBuilderApp.Controls
{
    public class BindableEntry<T> : Entry
    {
        static bool firstLoad;

        public static readonly BindableProperty NumericTextProperty =
            BindableProperty.Create("NumericText", typeof(T), typeof(BindableEntry<T>),
                null, BindingMode.TwoWay, propertyChanged: OnNumericTextChanged);

        static void OnNumericTextChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var boundEntry = (BindableEntry<T>)bindable;
            if (firstLoad && newValue != null)
            {
                firstLoad = false;
                boundEntry.Text = newValue.ToString();
            }
        }

        public T NumericText
        {
            get { return (T)GetValue(NumericTextProperty); }
            set { SetValue(NumericTextProperty, value); }
        }

        public BindableEntry()
        {
            firstLoad = true;
            this.TextChanged += BindableEntry_TextChanged;
        }

        private void BindableEntry_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!String.IsNullOrEmpty(e.NewTextValue))
            {
                this.NumericText = (T)Convert.ChangeType(e.NewTextValue, typeof(T));
            }
            else
            {
                this.NumericText = default(T);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

NullableIntEntry 和 NullableDecimalEntry(指定类型的 Bindable Entry 扩展):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CreditBuilderApp.Controls
{
    public class NullableIntEntry : BindableEntry<Int32?>
    {

    }

    public class NullableDecimalEntry : BindableEntry<Decimal?>
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

模型:

      private int? _businessOwnership { get; set; }
        public int? BusinessOwnership
        {
            get { return _businessOwnership; }
            set
            {
                if (_businessOwnership != value)
                {
                    _businessOwnership = value;
                    RaisePropertyChanged();
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

我实际上能够绑定到整数、小数、浮点数,基本上任何不是字符串的类型,这是朝着正确方向迈出的一步。但是,为了完成此任务,我必须创建上面的 BindableEntry 并指定它的类型。(将 T 替换为 int?,将 T 替换为小数?等等。同时指定 e.NewTextValue 的转换方式。

问题:下面的类型更改转换破坏了双向绑定。

this.NumericText = (T)Convert.ChangeType(e.NewTextValue, typeof(T));
Run Code Online (Sandbox Code Playgroud)

但是,这给了我一个错误(显然),因为 this.NumericText 在运行时之前是 T 类型。

那么,如果我希望该条目适用于可为 null 的整数,我需要将所有类型 T 替换为 int 吗?以及将上面的代码更改为:

Convert.ToInt32(e.NewTextValue)
Run Code Online (Sandbox Code Playgroud)

当我单步执行代码时,只要到达 Convert.ChangeType to T 行,它就会退出框架。没有错误并且显示页面,但是该特定可绑定条目之后的每个控件都没有值。

单步执行 ConvertType 函数后

如果我遗漏了任何信息,请告诉我。帮助将不胜感激!

Pra*_*dda 1

我建议使用值转换器,它比创建自定义控件要容易得多,并且符合 MVVM 模式。

您可以在此处找到代码片段 - https://forums.xamarin.com/discussion/comment/60076/#Comment_60076