Abstract
Mvvmcross includes a convert which translates a boolean into an MvxVisibility enumerable, and a target binding on each platform which translates an MvxVisibility to either a View.Visibility on Android, or an inverted boolean for UIView.Hidden on iOS.
Its usage requires to use a magic string ("Visibility") 2 times in the binding description. And it supports only boolean values in the viewmodel.
And last, it is distributed as a Mvvmcross plugin, which slows down the app's startup time, and crashes the app when the "link all" build option is activated", unless you add special code to prevent it. If you forget to add the plugin, it won't crash, it won't log, and you may search for your error a long time before remembering to add the plugin.
Introducing Visibility2
The name is derived from the mvvmcross one, but it differs in many ways:
- needs only one target binding, no more converter. The target binding will do the conversion for you.
- supports any type as source. bool? is translated to Gone/Visible/Invisible. bool is translated to Gone/Visible. other objects are compared to null. Also supports MvxVisibility in case you still used the visibility converter.
- no more magic string: use the name property on the class instead (see example below)
- just works in many professional projects !
Usage
Viewmodel:
var set = this.CreateBindingSet<StreamFlightCell, StreamFlightCellViewModel>();
set.Bind(MenuButton).For(Visibility2TargetBinding.Name).To(vm => vm.IsMenuButtonVisible);
set.Apply();
View:
public abstract class StreamBaseCellViewModel : BaseViewModel
{
/// <summary>
/// True if current user is owner of this flight
/// </summary>
public bool IsMenuButtonVisible { get; set; }
protected StreamBaseCellViewModel() : base(false)
{
}
}