【问题标题】:Entry binding to int? in Xamarin Forms条目绑定到 int?在 Xamarin 形式中
【发布时间】:2017-07-30 16:38:45
【问题描述】:

我在 Xamarin Forms 中有一个简单的条目:

<Entry Text="{Binding Number, Mode=TwoWay}" Placeholder="Number" Keyboard="Numeric" />

在 ViewModel 中有属性:

    private int? _number;
    public int? Number
    {
        get { return _number; }
        set
        {
            if (SetProperty(ref _number, value))
            {
                OnPropertyChange(nameof(Number));
            }
        }
    }

我在 Entry 中输入数字并按下按钮,但在按钮单击过程中 - 数字仍然为空。我做错了什么?

【问题讨论】:

  • Binding系统包含一些明显的类型转换,但是String到Int32呢?不是其中之一

标签: xamarin binding xamarin.forms


【解决方案1】:

您可以将 int 绑定到 Entry 您不能绑定可为空的 int。您可以添加另一个将数字转换为字符串的属性,或者您可以轻松地创建一个像这样的值转换器...

class NullableIntConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var nullable = value as int?;
        var result = string.Empty;

        if (nullable.HasValue)
        {
            result = nullable.Value.ToString();
        }

        return result;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var stringValue = value as string;
        int intValue;
        int? result = null;

        if (int.TryParse(stringValue, out intValue))
        {
            result = new Nullable<int>(intValue);
        }

        return result;
    }

...并像这样在您的页面中使用它...

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:IntBinding"
             x:Class="IntBinding.DemoPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <local:NullableIntConverter x:Key="NullableIntConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <StackLayout>
        <Entry Text="{Binding Number, Mode=TwoWay, Converter={StaticResource NullableIntConverter}}" Placeholder="Number" Keyboard="Numeric" />
        <Label Text="{Binding Number, Converter={StaticResource NullableIntConverter}}" />
    </StackLayout>
</ContentPage>

【讨论】:

  • 您使用可为空的 int 构造函数而不是执行 result = intValue 是否有特殊原因?
  • 您无法转换为负数,因为对话会在每个字符输入时发生,并且减号会转换为 null。
【解决方案2】:

条目接受一个字符串。如果你想绑定一个 int 属性,你应该使用 IValueConverter 但我认为最好的解决方案是使用 String 属性而不是将值从 String 转换为 Int

public string StrNumber{
   get {
        if (Number == null) 
            return "";
        else
            return Number.ToString();
   }

   set {
       try {
           Number = int.Parse(value);
       }
       catch{
           Number = null;
       }
   }
}

【讨论】:

    【解决方案3】:
    public string MP { get; set; }
    public float MainPrice => (float.TryParse(MP, out float mp)) ? mp : 0;
    

    【讨论】:

      猜你喜欢
      • 2021-10-16
      • 1970-01-01
      • 2020-11-03
      • 2019-05-24
      • 2017-01-11
      • 1970-01-01
      • 1970-01-01
      • 2020-12-21
      • 1970-01-01
      相关资源
      最近更新 更多