Advertisement
Shimmy

Untitled

Mar 19th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Metronome.Shared;
  8. using Xamarin.Forms;
  9.  
  10. namespace MyProject.Views
  11. {
  12.   public partial class MainPage : ContentPage
  13.   {
  14.     public MainPage()
  15.     {
  16.       InitializeComponent();
  17.     }
  18.  
  19.     protected override void OnPropertyChanging([CallerMemberName] string propertyName = null)
  20.     {
  21.       base.OnPropertyChanging(propertyName);
  22.       if (propertyName == nameof(BindingContext) && BindingContext is IFlashlight flashlight)
  23.         SetEventHandler(false);
  24.     }
  25.  
  26.     protected override void OnBindingContextChanged()
  27.     {
  28.       base.OnBindingContextChanged();
  29.       SetEventHandler(true);
  30.     }
  31.  
  32.     protected override void OnAppearing()
  33.     {
  34.       base.OnAppearing();
  35.       SetEventHandler(true);
  36.     }
  37.  
  38.     protected override void OnDisappearing()
  39.     {
  40.       SetEventHandler(false);
  41.       base.OnDisappearing();
  42.     }
  43.  
  44.     bool isAttached;
  45.     void SetEventHandler(bool attach)
  46.     {
  47.       if (isAttached == attach) return;
  48.  
  49.       if (BindingContext is IFlashlight flashlight)
  50.       {
  51.         if (attach)
  52.           flashlight.Flash += OnFlash;
  53.         else
  54.           flashlight.Flash -= OnFlash;
  55.       }
  56.  
  57.       isAttached = attach;
  58.     }
  59.  
  60.     private void OnFlash(bool obj)
  61.     {
  62.  
  63.     }
  64.  
  65.  
  66.   }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement