Exemplo n.º 1
0
        void UpdateToolbarItems()
        {
            if (_page.ApplicationBar == null)
            {
                _page.ApplicationBar = new ApplicationBar();
            }

            ToolbarItem[]    items        = _tracker.ToolbarItems.ToArray();
            MasterDetailPage masterDetail = _tracker.Target.Descendants().Prepend(_tracker.Target).OfType <MasterDetailPage>().FirstOrDefault();

            TaggedAppBarButton oldMasterDetailButton = _page.ApplicationBar.Buttons.OfType <TaggedAppBarButton>().FirstOrDefault(b => b.Tag is MasterDetailPage && b.Tag != masterDetail);

            if (oldMasterDetailButton != null)
            {
                _page.ApplicationBar.Buttons.Remove(oldMasterDetailButton);
            }

            if (masterDetail != null)
            {
                if (masterDetail.ShouldShowToolbarButton())
                {
                    if (_page.ApplicationBar.Buttons.OfType <TaggedAppBarButton>().All(b => b.Tag != masterDetail))
                    {
                        var button = new TaggedAppBarButton
                        {
                            IconUri   = new Uri(masterDetail.Master.Icon ?? "ApplicationIcon.jpg", UriKind.Relative),
                            Text      = masterDetail.Master.Title,
                            IsEnabled = true,
                            Tag       = masterDetail
                        };
                        button.Click += (sender, args) =>
                        {
                            var masterDetailRenderer = GetRenderer(masterDetail) as MasterDetailRenderer;

                            if (masterDetailRenderer != null)
                            {
                                masterDetailRenderer.Toggle();
                            }
                        };
                        _page.ApplicationBar.Buttons.Add(button);
                    }
                }
            }

            var buttonsToAdd = new List <TaggedAppBarButton>();

            foreach (ToolbarItem item in items.Where(i => i.Order != ToolbarItemOrder.Secondary))
            {
                IMenuItemController controller = item;
                if (_page.ApplicationBar.Buttons.OfType <TaggedAppBarButton>().Any(b => b.Tag == item))
                {
                    continue;
                }

                var button = new TaggedAppBarButton
                {
                    IconUri   = new Uri(item.Icon ?? "ApplicationIcon.jpg", UriKind.Relative),
                    Text      = !string.IsNullOrWhiteSpace(item.Text) ? item.Text : (string)item.Icon ?? "ApplicationIcon.jpg",
                    IsEnabled = controller.IsEnabled,
                    Tag       = item
                };
                button.Click += (sender, args) => controller.Activate();
                buttonsToAdd.Add(button);
            }

            var menuItemsToAdd = new List <TaggedAppBarMenuItem>();

            foreach (ToolbarItem item in items.Where(i => i.Order == ToolbarItemOrder.Secondary))
            {
                if (_page.ApplicationBar.MenuItems.OfType <TaggedAppBarMenuItem>().Any(b => b.Tag == item))
                {
                    continue;
                }

                var button = new TaggedAppBarMenuItem {
                    Text = !string.IsNullOrWhiteSpace(item.Text) ? item.Text : (string)item.Icon ?? "MenuItem", IsEnabled = true, Tag = item
                };
                button.Click += (sender, args) => ((IMenuItemController)item).Activate();
                menuItemsToAdd.Add(button);
            }

            TaggedAppBarButton[] deadButtons = _page.ApplicationBar.Buttons.OfType <TaggedAppBarButton>().Where(b => b.Tag is ToolbarItem && !items.Contains(b.Tag)).ToArray();

            TaggedAppBarMenuItem[] deadMenuItems = _page.ApplicationBar.MenuItems.OfType <TaggedAppBarMenuItem>().Where(b => b.Tag is ToolbarItem && !items.Contains(b.Tag)).ToArray();

            // we must remove the dead buttons before adding the new ones so we don't accidentally go over the limit during the transition
            foreach (TaggedAppBarButton deadButton in deadButtons)
            {
                deadButton.Dispose();
                _page.ApplicationBar.Buttons.Remove(deadButton);
            }

            foreach (TaggedAppBarMenuItem deadMenuItem in deadMenuItems)
            {
                _page.ApplicationBar.MenuItems.Remove(deadMenuItem);
            }

            // fixme, insert in order
            foreach (TaggedAppBarButton newButton in buttonsToAdd)
            {
                _page.ApplicationBar.Buttons.Add(newButton);
            }

            foreach (TaggedAppBarMenuItem newMenuItem in menuItemsToAdd)
            {
                _page.ApplicationBar.MenuItems.Add(newMenuItem);
            }

            _page.ApplicationBar.IsVisible = _page.ApplicationBar.Buttons.Count > 0 || _page.ApplicationBar.MenuItems.Count > 0;
        }
Exemplo n.º 2
0
		void UpdateToolbarItems()
		{
			if (_page.ApplicationBar == null)
				_page.ApplicationBar = new ApplicationBar();

			ToolbarItem[] items = _tracker.ToolbarItems.ToArray();
			MasterDetailPage masterDetail = _tracker.Target.Descendants().Prepend(_tracker.Target).OfType<MasterDetailPage>().FirstOrDefault();

			TaggedAppBarButton oldMasterDetailButton = _page.ApplicationBar.Buttons.OfType<TaggedAppBarButton>().FirstOrDefault(b => b.Tag is MasterDetailPage && b.Tag != masterDetail);

			if (oldMasterDetailButton != null)
				_page.ApplicationBar.Buttons.Remove(oldMasterDetailButton);

			if (masterDetail != null)
			{
				if (masterDetail.ShouldShowToolbarButton())
				{
					if (_page.ApplicationBar.Buttons.OfType<TaggedAppBarButton>().All(b => b.Tag != masterDetail))
					{
						var button = new TaggedAppBarButton
						{
							IconUri = new Uri(masterDetail.Master.Icon ?? "ApplicationIcon.jpg", UriKind.Relative),
							Text = masterDetail.Master.Title,
							IsEnabled = true,
							Tag = masterDetail
						};
						button.Click += (sender, args) =>
						{
							var masterDetailRenderer = GetRenderer(masterDetail) as MasterDetailRenderer;

							if (masterDetailRenderer != null)
								masterDetailRenderer.Toggle();
						};
						_page.ApplicationBar.Buttons.Add(button);
					}
				}
			}

			var buttonsToAdd = new List<TaggedAppBarButton>();
			foreach (ToolbarItem item in items.Where(i => i.Order != ToolbarItemOrder.Secondary))
			{
				IMenuItemController controller = item;
				if (_page.ApplicationBar.Buttons.OfType<TaggedAppBarButton>().Any(b => b.Tag == item))
					continue;

				var button = new TaggedAppBarButton
				{
					IconUri = new Uri(item.Icon ?? "ApplicationIcon.jpg", UriKind.Relative),
					Text = !string.IsNullOrWhiteSpace(item.Text) ? item.Text : (string)item.Icon ?? "ApplicationIcon.jpg",
					IsEnabled = controller.IsEnabled,
					Tag = item
				};
				button.Click += (sender, args) => controller.Activate();
				buttonsToAdd.Add(button);
			}

			var menuItemsToAdd = new List<TaggedAppBarMenuItem>();
			foreach (ToolbarItem item in items.Where(i => i.Order == ToolbarItemOrder.Secondary))
			{
				if (_page.ApplicationBar.MenuItems.OfType<TaggedAppBarMenuItem>().Any(b => b.Tag == item))
					continue;

				var button = new TaggedAppBarMenuItem { Text = !string.IsNullOrWhiteSpace(item.Text) ? item.Text : (string)item.Icon ?? "MenuItem", IsEnabled = true, Tag = item };
				button.Click += (sender, args) => ((IMenuItemController)item).Activate();
				menuItemsToAdd.Add(button);
			}

			TaggedAppBarButton[] deadButtons = _page.ApplicationBar.Buttons.OfType<TaggedAppBarButton>().Where(b => b.Tag is ToolbarItem && !items.Contains(b.Tag)).ToArray();

			TaggedAppBarMenuItem[] deadMenuItems = _page.ApplicationBar.MenuItems.OfType<TaggedAppBarMenuItem>().Where(b => b.Tag is ToolbarItem && !items.Contains(b.Tag)).ToArray();

			// we must remove the dead buttons before adding the new ones so we don't accidentally go over the limit during the transition
			foreach (TaggedAppBarButton deadButton in deadButtons)
			{
				deadButton.Dispose();
				_page.ApplicationBar.Buttons.Remove(deadButton);
			}

			foreach (TaggedAppBarMenuItem deadMenuItem in deadMenuItems)
				_page.ApplicationBar.MenuItems.Remove(deadMenuItem);

			// fixme, insert in order
			foreach (TaggedAppBarButton newButton in buttonsToAdd)
				_page.ApplicationBar.Buttons.Add(newButton);

			foreach (TaggedAppBarMenuItem newMenuItem in menuItemsToAdd)
				_page.ApplicationBar.MenuItems.Add(newMenuItem);

			_page.ApplicationBar.IsVisible = _page.ApplicationBar.Buttons.Count > 0 || _page.ApplicationBar.MenuItems.Count > 0;
		}