private static void RefreshApplicationBar(BindToAppliactionBarBehavior behavior) { switch (behavior.TargetType) { case BindToApplicationBarTarget.IconButton: { var lst = behavior.Buttons; if (lst != null) { if (behavior.IndexBindTo < lst.Count && behavior.IndexBindTo >= 0) { var item = lst[behavior.IndexBindTo]; if (item.IconUri != behavior.IconUri) { if (behavior.IconUri != null) { item.IconUri = behavior.IconUri; } } ; if (item.IsEnabled != behavior.IsEnabled) { item.IsEnabled = behavior.IsEnabled; } ; if (item.Text != behavior.Text && behavior.Text != null) { item.Text = behavior.Text; } ; } } } break; case BindToApplicationBarTarget.MenuItem: { var lst = behavior.MenuItems; if (lst != null) { if (behavior.IndexBindTo < lst.Count && behavior.IndexBindTo >= 0) { var item = lst[behavior.IndexBindTo]; if (item.IsEnabled != behavior.AssociatedObject.IsEnabled) { item.IsEnabled = behavior.AssociatedObject.IsEnabled; } ; if (item.Text != behavior.Text) { item.Text = behavior.Text; } ; } } } break; default: break; } }
protected override void OnAttached() { base.OnAttached(); // TryLocatePageFromButton(); var binding = new Binding(); binding.Source = AssociatedObject; binding.Path = new PropertyPath("IsEnabled"); BindingOperations.SetBinding(this, IsEnabledProperty, binding); AssociatedObject.Loaded += (s, e) => { this.IsBindingActive = true; TryLocatePageFromButton(); eventSubscribe = EventRouting.EventRouter.Instance .GetEventObject <IApplicationBarMenuItem>() .ObserveOn(System.Reactive.Concurrency.DispatcherScheduler.Current) .Where(_ => IsBindingActive) .Where(x => x.EventName == EventFilterName) .Where(x => BindToAppliactionBarBehavior.GetHasEventsWired(Page)) .Select(ev => { int idx = -1; if (ev.EventArgs != null) { InstanceToIndexDic.TryGetValue(ev.EventArgs, out idx); } return(new { innerev = ev, idx }); } ) .Where(ev => ev.idx == IndexBindTo) .Subscribe( ev => { if (AssociatedObject.Command != null) { if (AssociatedObject.Command.CanExecute(AssociatedObject.CommandParameter)) { AssociatedObject.Command.Execute(AssociatedObject.CommandParameter); } } }); var appb = GetApplicationBar(Page); if (appb == null) { return; } if (appb.Buttons != null) { Buttons = appb.Buttons.OfType <IApplicationBarIconButton>().ToList(); var i = 0; foreach (IApplicationBarIconButton btn in appb.Buttons) { WireEventToItem(btn); InstanceToIndexDic[btn] = i; i++; } } if (appb.MenuItems != null) { MenuItems = appb.MenuItems.OfType <IApplicationBarMenuItem>().ToList(); var i = 0; foreach (IApplicationBarMenuItem itm in appb.MenuItems) { WireEventToItem(itm); InstanceToIndexDic[itm] = i; i++; } } RefreshApplicationBar(this); BindToAppliactionBarBehavior.SetHasEventsWired(Page, true); }; }