代码之家  ›  专栏  ›  技术社区  ›  Traci

复合命令不起作用

  •  1
  • Traci  · 技术社区  · 15 年前

    我正在开发一个复合mvvm应用程序,并试图让全局绑定事件发生-除非它不是!…

    虽然canrun返回true,但默认情况下按钮是禁用的!!!!!我已经按照复合指南操作,onloadmenu没有启动!!!!

    我一直在绕圈子(事件聚合器、delegatecommands、复合命令)它只是不起作用。有人能看看这个告诉我我缺了什么吗??

    //xmlns:local="clr-namespace:Commands;assembly=MyApp"
     <Button HorizontalAlignment="Center" Margin="1,1,1,1" 
                                   Grid.Row="2" 
                Command="{x:Static local:AdminGlobalCommands.LoadAdminMenu}"/>
    
    
     public static class AdminGlobalCommands // In Common Code Library
        {
            //List All Global Commands Here
            public static CompositeCommand LoadAdminMenu = new CompositeCommand();
        }
    
    
     public class AdminModuleViewModel : ViewModelBase, IAdminModuleViewModel // In AdminModule
        {
            protected IRegionManager _regionManager;
            private IUnityContainer _container;
    
            public AdminModuleViewModel(
    
                IEventAggregator eventAggregator,
                IBusyService busyService,
                IUnityContainer container,
                IRegionManager regionManager
              )
                : base(eventAggregator, busyService, container)
            {
                // show the progress indicator
                busyService.ShowBusy();
                this._regionManager = regionManager;
                this._container = container;
    
                //set up the command receivers
                this.AdminShowMenuCommand =  new DelegateCommand<object>(this.OnLoadAdminMenu, this.CanShowAdminMenu);
    
                //Listen To Events
                AdminGlobalCommands.LoadAdminMenu.RegisterCommand(AdminShowMenuCommand);
    
                busyService.HideBusy();
            }
            public DelegateCommand<object> AdminShowMenuCommand { get; private set; }
    
            private bool CanShowAdminMenu(object  obj) 
            { //Rules to Handle the Truth
                return true; 
            }
    
            public void OnLoadAdminMenu(object obj)
            {
                UIElement viewToOpen = (UIElement)_container.Resolve(typeof(AdminMenuControl)) ;
                _regionManager.AddToRegion("MainRegion", viewToOpen);
                _regionManager.Regions["MainRegion"].Activate(viewToOpen); ;
            }
        }
    
    1 回复  |  直到 15 年前
        1
  •  3
  •   John    13 年前

    如果要创建 CompositeCommand 具有 monitorCommandActivity 设置为true,还需要注意并设置 DelegateCommand.IsActive 状态。

    在那种情况下 复合命令 不会认为不活动 DelegateCommand 因此,您的按钮可能保持禁用状态(例如,当没有其他活动的和可执行的 委派命令 是在 CompositeCommands 的命令链)。