protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            base.OnNavigatingFrom(e);
            _timer.Stop();

            _tempHum?.Dispose();
            _tempHum = null;

            _barometer?.Dispose();
            _barometer = null;

            _luxMeter?.Dispose();
            _luxMeter = null;

            _magnetometer?.Dispose();
            _magnetometer = null;
        }
        private async void InitializeAsync()
        {
            try
            {
                _tempHum = new Dsth01(27); // GPIO27 connected to Dsth01 CS-pin
                _barometer = new Bmp180();
                _magnetometer = new Hmc5883L(22); // GPIO22 connected to Hmc5883L DRDY-pin
                _luxMeter = new Bh1750Fvi();
            }
            catch
            {
                Debug.WriteLine("Sensor initialization failed.");
                return;
            }

            await Task.WhenAll(_magnetometer.ConnectAsync(), _barometer.ConnectAsync(), _tempHum.ConnectAsync(), _luxMeter.ConnectAsync());

            // Set magnetometer gain an averaging
            if (_magnetometer.Connected)
            {
                await
                    Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                        () => { Magnetometer.Visibility = Visibility.Visible; });
                _magnetometer.WriteRegister(Hmc5883L.Register.ConfA, (byte) Hmc5883L.ConfigA.Average8);
                _magnetometer.WriteRegister(Hmc5883L.Register.ConfB, (byte) Hmc5883L.ConfigB.Gain1370);
            }

            // The BH175FVI sensor supports a continuous measurement mode
            if (_luxMeter.Connected)
            {
                await
                    Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                        () => { Ambient.Visibility = Visibility.Visible; });
                _luxMeter.Mode = Bh1750Fvi.Resolution.VeryHigh;
                _luxMeter.ReadingChanged += _AmbientLuxChanged;
                _luxMeter.ContinuousPeriod = 2000; // every 2 seconds
                _luxMeter.ContinuousMeasurement = true;
            }

            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                if (_tempHum.Connected)
                    Humidity.Visibility = Visibility.Visible;

                if (_barometer.Connected)
                    Barometer.Visibility = Visibility.Visible;
            });

            // The rest of the sensors are polled periodically
            _timer = new DispatcherTimer {Interval = TimeSpan.FromMilliseconds(1000)};
            _timer.Tick += _timer_Tick;
            _timer.Start();
        }
        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            base.OnNavigatingFrom(e);
            _timer.Stop();

            _tempHum?.Dispose();
            _tempHum = null;

            _barometer?.Dispose();
            _barometer = null;

            _luxMeter?.Dispose();
            _luxMeter = null;

            _magnetometer?.Dispose();
            _magnetometer = null;
        }
        private async void InitializeAsync()
        {
            try
            {
                _tempHum      = new Dsth01(27);   // GPIO27 connected to Dsth01 CS-pin
                _barometer    = new Bmp180();
                _magnetometer = new Hmc5883L(22); // GPIO22 connected to Hmc5883L DRDY-pin
                _luxMeter     = new Bh1750Fvi();
            }
            catch
            {
                Debug.WriteLine("Sensor initialization failed.");
                return;
            }

            await Task.WhenAll(_magnetometer.ConnectAsync(), _barometer.ConnectAsync(), _tempHum.ConnectAsync(), _luxMeter.ConnectAsync());

            // Set magnetometer gain an averaging
            if (_magnetometer.Connected)
            {
                await
                Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                                    () => { Magnetometer.Visibility = Visibility.Visible; });

                _magnetometer.WriteRegister(Hmc5883L.Register.ConfA, (byte)Hmc5883L.ConfigA.Average8);
                _magnetometer.WriteRegister(Hmc5883L.Register.ConfB, (byte)Hmc5883L.ConfigB.Gain1370);
            }

            // The BH175FVI sensor supports a continuous measurement mode
            if (_luxMeter.Connected)
            {
                await
                Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                                    () => { Ambient.Visibility = Visibility.Visible; });

                _luxMeter.Mode                  = Bh1750Fvi.Resolution.VeryHigh;
                _luxMeter.ReadingChanged       += _AmbientLuxChanged;
                _luxMeter.ContinuousPeriod      = 2000; // every 2 seconds
                _luxMeter.ContinuousMeasurement = true;
            }

            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                if (_tempHum.Connected)
                {
                    Humidity.Visibility = Visibility.Visible;
                }

                if (_barometer.Connected)
                {
                    Barometer.Visibility = Visibility.Visible;
                }
            });

            // The rest of the sensors are polled periodically
            _timer = new DispatcherTimer {
                Interval = TimeSpan.FromMilliseconds(1000)
            };
            _timer.Tick += _timer_Tick;
            _timer.Start();
        }