xamarin 条目包含0而不是无

cwtwac6a  于 2023-08-01  发布在  其他
关注(0)|答案(2)|浏览(74)

我正在用xamarin写我的第一个应用程序。我加载了一个页面,它初始化了一个列表的新项,因此我将一些整型和双精度变量与页面中的条目绑定在一起。当应用程序加载页面时,条目不包含任何内容,因此我必须在输入数据之前删除条目中的值。如何解决这个问题?
Actual Behaviour
Desired behaviour

jpfvwuh4

jpfvwuh41#

正如Jonathan Willcock和Serge在评论中所说,将int改为int?如果没有值,可以使条目文本为空。
如果你想实现你的问题中的行为,可以使用这样的代码:

if(string.IsNullOrEmpty(entry.text))
{entry.Placeholder="Value"}

字符串
或者在xmal中:

<Entry Text="{Binding date,TargetNullValue='Value'}"/>

yh2wf1be

yh2wf1be2#

你必须创建一个这样的转换器

public class NumericValueEntryConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        try
        {
            if (value == null || (value != null && ((double)value) == 0d)) return "";
        }
        catch (Exception ex) { App.ValidateException(ex); }
        return "" + value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        try
        {
            if (value == null || (value != null && ((string)value).Length == 0)) return 0d;
        }
        catch (Exception ex) { App.ValidateException(ex); }
        return double.Parse("" + value);
    }
}

字符串
就是这样使用的

<Entry Text="{Binding DoubleNumber, Converter={StaticResource NumericValueEntryConverter}}" />

相关问题