【问题标题】:Xamarin custom BindableProperty with ConverterXamarin 自定义 BindableProperty 与转换器
【发布时间】:2017-12-28 13:57:45
【问题描述】:

我有一个带有自定义 BindableProperty 的自定义视图:

public string Valore {
            get { return (string)GetValue(ValoreProperty); }
            set { SetValue(ValoreProperty, value); }
        }

    public static readonly BindableProperty ValoreProperty =
        BindableProperty.Create(
            propertyName: nameof(Valore),
            returnType: typeof(string),
            declaringType: typeof(VolosTimePickerView),
            defaultBindingMode: BindingMode.TwoWay,
            propertyChanged: ValorePropertyChanged,
            propertyChanging: ValorePropertyChanging
        );

在我的 XAML 中,由于 Valore 的值是可以为空的 Int,我必须使用这样的转换器:

<view:VolosTimePickerView Grid.Row="0" Grid.Column="1" 
     Valore="{Binding OrariSelezionati.ViaggioInizioMattina, Converter={StaticResource NullableConverter}}" />

如何在 XAML 端不使用 Converter={StaticResource NullableConverter 而是直接在属性后面的代码中做同样的事情?

谢谢!

【问题讨论】:

  • 为什么需要它成为BindableProperty ?为什么不能是实现INotifyPropertyChanged 的常规属性?
  • 为什么需要在代码隐藏中设置转换器?您是否考虑过公开 Nullable-Int BindableProperty,然后在 ValorePropertyChanged 事件处理程序中更新常规字符串属性的值?在这种情况下,您不需要转换器,并且逻辑在 ContentView 中实现。相反,如果您真的想从代码隐藏中设置转换器,请尝试查看forums.xamarin.com/discussion/18849/…

标签: c# xaml xamarin bindable


【解决方案1】:

您的Valorestring。尝试将其转换为Nullable&lt;int&gt; Valore。看看会发生什么。

并尝试将 Valore 的值也绑定到您的 ViewModel 中。所以你会得到

Nullable<int> _Valore;
public Nullable<int> Valore {get; set;}

【讨论】:

    猜你喜欢
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多