[英]System.FormatException in MVVMCross
The following code updates properties on-the-fly via mvvmcross messaging protocol. 以下代码通过mvvmcross消息传递协议即时更新属性。
The problem that I am facing now, when user clears either age
or category
textviews, then I am getting systemformat exception, I guess when textview
gets blank, it does not parse it to a number? 我现在面临的问题是,当用户清除
age
或category
文本视图时,然后出现系统格式异常,我textview
空时,它不会将其解析为数字吗?
System.FormatException: Input string was not in a correct format.
System.FormatException:输入字符串的格式不正确。
MainViewModel 主视图模型
private readonly IMvxMessenger _messenger;
private readonly MvxSubscriptionToken _token;
public MainViewModel(IMvxMessenger messenger) {
_messenger = messenger;
_token = messenger.Subscribe<SelectedItemMessage>(OnMessageReceived);;
}
private void OnMessageReceived(SelectedItemMessage obj)
{
Age = obj.Age;
Category= obj.Category;
}
DetailViewModel DetailViewModel
private readonly IMvxMessenger _messenger;
public DetailViewModel(IMvxMessenger messenger) {
_messenger = messenger;
}
public void UpdateMethod() {
var message = new SelectedItemMessage(this, age, category);
_messenger.Publish(message, typeof(SelectedItemMessage));
}
SelectedItemMessage SelectedItemMessage
public SelectedItemMessage(object sender, double age, int category) : base(sender)
{
Age = age;
Category = category;
}
public double Age { get; set; }
public int Category{ get; set; }
}
You need a value converter which converts empty string to null and you need to make Age
property nullable. 您需要一个将空字符串转换为null的值转换器,并且需要使
Age
属性为可空。 After that you will need to specify the converted in the binding: 之后,您将需要在绑定中指定转换后的内容:
public class NullableValueConverter : MvxValueConverter
{
public override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
public override object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null || string.IsNullOrEmpty(value.ToString()))
{
return null;
}
return value;
}
}
and inside your view: 在您的视图内:
local:MvxBind="Text Age,Converter=Nullable;"
You can read more about value converters at Value Converters Wiki 您可以在Value Converters Wiki上阅读有关价值转换器的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.