diff options
author | Alan McGovern <alanmcgovern@users.noreply.github.com> | 2016-11-08 08:14:44 +0000 |
---|---|---|
committer | Stephane Delcroix <stephane@delcroix.org> | 2016-11-08 09:14:44 +0100 |
commit | d43b8a2da6681518b6b765f748a79b5fcf5927ac (patch) | |
tree | 781683cfdef129d485def36d3a980c353e2fe525 /Xamarin.Forms.Platform.Android | |
parent | 84995a921221811d2e7047d54dcefde0c8063397 (diff) | |
download | xamarin-forms-d43b8a2da6681518b6b765f748a79b5fcf5927ac.tar.gz xamarin-forms-d43b8a2da6681518b6b765f748a79b5fcf5927ac.tar.bz2 xamarin-forms-d43b8a2da6681518b6b765f748a79b5fcf5927ac.zip |
[Android] Always set a non-null Device.Info (#504)
When we run under the context of layoutlib the `Context` object
that is created will not implement IDeviceInfoProvider. All this
means is that we will not get change notifications when the
orientation changes, but that's ok! This won't happen anyway.
If we instantiate the Device.Info object then everywhere else in
the code will be able to get access to the screen properties.
Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=44893
Diffstat (limited to 'Xamarin.Forms.Platform.Android')
-rw-r--r-- | Xamarin.Forms.Platform.Android/Forms.cs | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/Xamarin.Forms.Platform.Android/Forms.cs b/Xamarin.Forms.Platform.Android/Forms.cs index fd742875..3d2a735c 100644 --- a/Xamarin.Forms.Platform.Android/Forms.cs +++ b/Xamarin.Forms.Platform.Android/Forms.cs @@ -130,10 +130,7 @@ namespace Xamarin.Forms Device.info = null; } - // probably could be done in a better way - var deviceInfoProvider = activity as IDeviceInfoProvider; - if (deviceInfoProvider != null) - Device.Info = new AndroidDeviceInfo(deviceInfoProvider); + Device.Info = new AndroidDeviceInfo(activity); var ticker = Ticker.Default as AndroidTicker; if (ticker != null) @@ -190,17 +187,21 @@ namespace Xamarin.Forms class AndroidDeviceInfo : DeviceInfo { - readonly IDeviceInfoProvider _formsActivity; + bool disposed; + readonly Context _formsActivity; readonly Size _pixelScreenSize; readonly double _scalingFactor; Orientation _previousOrientation = Orientation.Undefined; - public AndroidDeviceInfo(IDeviceInfoProvider formsActivity) + public AndroidDeviceInfo(Context formsActivity) { _formsActivity = formsActivity; CheckOrientationChanged(_formsActivity.Resources.Configuration.Orientation); - formsActivity.ConfigurationChanged += ConfigurationChanged; + // This will not be an implementation of IDeviceInfoProvider when running inside the context + // of layoutlib, which is what the Android Designer does. + if (_formsActivity is IDeviceInfoProvider) + ((IDeviceInfoProvider) _formsActivity).ConfigurationChanged += ConfigurationChanged; using (DisplayMetrics display = formsActivity.Resources.DisplayMetrics) { @@ -224,7 +225,11 @@ namespace Xamarin.Forms protected override void Dispose(bool disposing) { - _formsActivity.ConfigurationChanged -= ConfigurationChanged; + if (disposing && !disposed) { + disposed = true; + if (_formsActivity is IDeviceInfoProvider) + ((IDeviceInfoProvider) _formsActivity).ConfigurationChanged -= ConfigurationChanged; + } base.Dispose(disposing); } |