c# – 支持基本类型和可空类型的双向可绑定条目
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 支持基本类型和可空类型的双向可绑定条目,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3127字,纯文字阅读大概需要5分钟。
内容图文
我试图用入门的Xamarin.Forms实现完美的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替换为十进制?等等.另外指定如何生成e.NewTextValue.
问题:下面的类型更改转换打破了双向绑定.
this.NumericText = (T)Convert.ChangeType(e.NewTextValue, typeof(T));
但是,这给了我一个错误(显然),因为this.NumericText在运行时之前是T类型.
所以,如果我希望该条目可以为可空整数工作,我需要用int替换所有类型的T?以及将上述代码更改为:
Convert.ToInt32(e.NewTextValue)
当我单步执行代码时,每当我将Convert.ChangeType转到T行时,它就会退出该帧.没有错误,页面显示,但在该特定可绑定条目之后的每个控件都没有值.
After stepping through the ConvertType function
如果我错过任何信息,请告诉我.帮助将不胜感激!
解决方法:
我建议使用值转换器,它比创建自定义控件和确认MVVM模式容易得多.
你可以在这里找到代码片段 – https://forums.xamarin.com/discussion/comment/60076/#Comment_60076
内容总结
以上是互联网集市为您收集整理的c# – 支持基本类型和可空类型的双向可绑定条目全部内容,希望文章能够帮你解决c# – 支持基本类型和可空类型的双向可绑定条目所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。