Ejemplo n.º 1
0
        //IDisposable commandSubscription;



        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();
                var appb = GetApplicationBar(Page);
                if (appb.Buttons != null)
                {
                    Buttons = appb.Buttons.OfType <IApplicationBarIconButton>().ToList();
                }

                if (appb.MenuItems != null)
                {
                    MenuItems = appb.MenuItems.OfType <IApplicationBarMenuItem>().ToList();
                }

                RefreshApplicationBar(this);

                eventSubscribe = EventRouting.EventRouter.Instance
                                 .GetEventObject <EventCommandEventArgs>()
                                 .ObserveOn(System.Reactive.Concurrency.DispatcherScheduler.Current)
                                 .Where(_ =>
                                        IsBindingActive)
                                 .Where(x =>
                                        x.EventName == EventFilterName)
                                 .Where(
                    x =>
                    (x.Sender is IApplicationBarIconButton && this.TargetType == BindToApplicationBarTarget.IconButton) || (!(x.Sender is IApplicationBarIconButton) && this.TargetType == BindToApplicationBarTarget.MenuItem))
                                 .Where(ev =>
                {
                    if (ev.EventArgs != null)
                    {
                        switch (TargetType)
                        {
                        case BindToApplicationBarTarget.IconButton:
                            return(Page.ApplicationBar.Buttons.IndexOf(ev.Sender) == IndexBindTo);

                        case BindToApplicationBarTarget.MenuItem:
                            return(Page.ApplicationBar.MenuItems.IndexOf(ev.Sender) == IndexBindTo);

                        default:
                            return(false);
                        }
                    }
                    return(false);
                })

                                 .Subscribe(
                    ev =>
                {
                    var args = ev.EventArgs;
                    var cb   = new CommandBinding()
                    {
                        EventName   = args.EventName,
                        EventSource = Page,
                        Parameter   = args.Parameter,
                        Command     = AssociatedObject.Command,
                    };

                    cb.ExecuteFromEvent(args.ViewSender, args.EventArgs, args.EventName, args.EventHandlerType);

                    //if (AssociatedObject.Command != null)
                    //{
                    //    if (AssociatedObject.Command.CanExecute(AssociatedObject.CommandParameter))
                    //    {
                    //        AssociatedObject.Command.Execute(AssociatedObject.CommandParameter);
                    //    }
                    //}
                });


                if (!GetHasEventsWired(Page))
                {
                    PageSetup();
                    SetHasEventsWired(Page, true);
                }


                return;
            };
        }