0

我在 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 的属性绑定。非常感谢!

4

1 回答 1

1

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

您可以在官方文档中阅读更多相关信息。

在你的情况下,我建议你使用 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>();
于 2018-10-20T14:57:42.027 回答