xamarin.iosmvvmcrossxamarin.ios-binding

Mvvcross WithFallback on xamarin ios


i'm using MvvmCross on xamarin iOS. I'm using fluent for the bindings on the ViewModel and json. I wanted to try the WithFallback() function, but when the property on my ViewModel (string in this case), comes null or empty, it doesn't do anything. I tried this:

//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();

And another question is how i can bind that fallback with a property of the viewmodel or json. Thanks a lot!


Solution

  • Fallback will only be used if the binding fails, not if the property exists and is null or whatever.

    You can read more about this in the official documentation.

    In your case, I would suggest you use a ValueConverter, something like this will work:

    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();
        }
    }
    

    And then your binding:

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