Last active
February 25, 2018 14:51
-
-
Save codebeaulieu/7a57f0d5cf9bcc29b1127929ffa87e6e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1 | |
public abstract class ContentPageBase<TViewModel> : ReactiveContentPage<TViewModel> where TViewModel : ViewModels.ViewModelBase<TViewModel> | |
{ | |
// 2 | |
protected Lazy<CompositeDisposable> ControlBindings = new Lazy<CompositeDisposable>(() => new CompositeDisposable()); | |
// 3 | |
bool _controlsBound = false; | |
// 4 | |
protected abstract void SetupUserInterface(); | |
protected abstract void BindControls(); | |
// 5 | |
protected ContentPageBase() : base() | |
{ | |
SetupUserInterface(); | |
} | |
//8 | |
const string RendererPropertyName = "Renderer"; | |
protected override void OnPropertyChanged(string propertyName = null) | |
{ | |
base.OnPropertyChanged(propertyName); | |
if (propertyName.Equals(RendererPropertyName, StringComparison.OrdinalIgnoreCase)) | |
{ | |
var rendererResolver = DependencyService.Get<Interfaces.IRendererResolver>(); | |
if (rendererResolver == null) | |
throw new NullReferenceException("The renderer resolver was not initialized properly"); | |
if (rendererResolver.HasRenderer(this)) | |
RegisterBindings(); | |
else | |
UnregisterBindings(); | |
} | |
} | |
//6 | |
protected void RegisterBindings() | |
{ | |
if (_controlsBound) | |
return; | |
ViewModel?.RegisterBindings(); | |
BindControls(); | |
_controlsBound = true; | |
} | |
//7 | |
protected void UnregisterBindings() | |
{ | |
_controlsBound = false; | |
ViewModel?.UnregisterBindings(); | |
if (!ControlBindings.IsValueCreated) return; | |
ControlBindings?.Value?.Clear(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment