【问题标题】:Mvvcross WithFallback on xamarin iosXamarin ios 上的 Mvvcross WithFallback
【发布时间】:2018-10-16 16:22:48
【问题描述】:

我在 xamarin iOS 上使用 MvvmCross。我在 ViewModel 和 json 上使用 fluent 进行绑定。我想尝试 WithFallback() 函数,但是当我的 ViewModel 上的属性(在本例中为字符串)为 null 或为空时,它什么也不做。我试过这个:

//This works
this.BindLanguage(Header1, "Title");

/*  This works when vm.Message is not null or empty, 
/*  else print nothing, but don't call the WithFallback function 
*/
set.Bind(myLbl).For(view => view.Text).To(vm => vm.Message).WithFallback("Something");
set.Apply();

另一个问题是我如何将回退与 viewmodel 或 json 的属性绑定。非常感谢!

【问题讨论】:

    标签: xamarin.ios mvvmcross xamarin.ios-binding


    【解决方案1】:

    Fallback 仅在绑定失败时使用,而不是在属性存在且为 null 或其他情况下使用。

    您可以在official documentation 中阅读更多相关信息。

    在你的情况下,我建议你使用 ValueConverter,这样就可以了:

    public class MyValueConverter : MvxValueConverter<string, string>
    {
        protected override string Convert(string value, Type targetType, object parameter, CultureInfo culture)
        {
            return !string.IsNullOrEmpty(value) ? value : "Something";
        }
    
        protected override string ConvertBack(string value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    然后是你的绑定:

    set.Bind(myLbl).For(view => view.Text).To(vm => vm.Message).WithConversion<MyValueConverter>();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-17
      • 2013-06-26
      • 1970-01-01
      • 2015-12-04
      • 2020-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多