summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/Renderers/ToolbarImageButton.cs
blob: 6d18db546b5a4b4b49d32b4301d6e843a4b7d8b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.ComponentModel;
using Android.Content;
using Android.Graphics;
using Android.Widget;

namespace Xamarin.Forms.Platform.Android
{
	internal sealed class ToolbarImageButton : ImageButton, IToolbarButton
	{
		public ToolbarImageButton(Context context, ToolbarItem item) : base(context)
		{
			if (item == null)
				throw new ArgumentNullException("item", "you should specify a ToolbarItem");
			Item = item;
			Enabled = item.IsEnabled;
			Bitmap bitmap;
			bitmap = Context.Resources.GetBitmap(Item.Icon);
			SetImageBitmap(bitmap);
			SetBackgroundColor(new Color(0, 0, 0, 0).ToAndroid());
			Click += (sender, e) => item.Activate();
			bitmap.Dispose();
			Item.PropertyChanged += HandlePropertyChanged;
		}

		public ToolbarItem Item { get; set; }

		protected override void Dispose(bool disposing)
		{
			if (disposing)
				Item.PropertyChanged -= HandlePropertyChanged;
			base.Dispose(disposing);
		}

		void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == MenuItem.IsEnabledProperty.PropertyName)
				Enabled = Item.IsEnabled;
		}
	}
}