【发布时间】:2016-10-19 22:52:19
【问题描述】:
我正在尝试在 Xamarin.Forms 中通过 Entry 实现完美的 MVVM。
我的模型包含类型包括 string、int?、decimal?、bool? 等的属性。每当我绑定到字符串类型时,两种方式的绑定都会起作用,因为 text 属性具有字符串类型(它们匹配)。但是,一旦您尝试绑定回模型并且属性是 int 或 int?,它就不会更新模型的属性值。
在我的研究过程中,在 Xamarin 支持的帮助下,这是一个关于如何处理可为空类型的非常有用的线程:
Nullable type in x:TypeArguments
XAML 代码:
<controls:NullableIntEntry Grid.Column="1" Grid.Row="14" NumericText="{Binding BusinessOwnership, Mode=TwoWay}" x:Name="lblBusinessOwnership"></controls:NullableIntEntry>
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);
}
}
}
}
NullableIntEntry 和 NullableDecimalEntry(指定类型的可绑定条目扩展):
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?>
{
}
}
型号:
private int? _businessOwnership { get; set; }
public int? BusinessOwnership
{
get { return _businessOwnership; }
set
{
if (_businessOwnership != value)
{
_businessOwnership = value;
RaisePropertyChanged();
}
}
}
我实际上能够绑定到整数、小数、浮点数,基本上是任何不是字符串的类型,这是朝着正确方向迈出的一步。但是,要做到这一点,我必须创建上面的 BindableEntry 并指定它是哪种类型。 (将 T 替换为 int?、T 与 decimal? 等。同时指定 e.NewTextValue 的转换方式。
问题:下面的类型更改转换打破了双向绑定。
this.NumericText = (T)Convert.ChangeType(e.NewTextValue, typeof(T));
但是,这给了我一个错误(显然),因为 this.NumericText 在运行时之前是 T 类型。
所以,如果我希望该条目适用于可空整数,我需要将所有类型 T 替换为 int?以及将上面的代码更改为:
Convert.ToInt32(e.NewTextValue)
当我单步执行代码时,只要到达 Convert.ChangeType to T 行,它就会退出框架。没有错误并且页面被显示,但是在那个特定的可绑定条目之后的每个控件都没有值。
After stepping through the ConvertType function
如果我错过了任何信息,请告诉我。帮助将不胜感激!
【问题讨论】:
标签: c# mvvm xamarin xamarin.forms