复合命令不起作用

发布于 2024-08-22 17:17:52 字数 2136 浏览 8 评论 0原文

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

尽管 CanRun 返回 true,但按钮默认被禁用! !!我已遵循综合指南,但 OnLoadMenu 未触发!

我一直在兜圈子(事件聚合器、委托命令、复合命令)它只是不起作用。任何人都可以看看这个并告诉我我缺少什么吗?

//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); ;
        }
    }

I am working on a Composite MVVM application and trying to get Global Binding events happening - Except it is NOT!..

the buttons are disabled by default although the CanRun returns true!! !! I have followed the Composite Guide and the OnLoadMenu is not firing!!!

I have been going around in circles (Event Aggregators, DelegateCommands, Composite Commands) It just isnt working. Can any please look at this and tell me what I am Missing??

//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); ;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弃爱 2024-08-29 17:17:52

使用 PRISM 时,如果您要创建将 monitorCommandActivity 设置为 true 的 CompositeCommand,您还需要了解并设置 DelegateCommand.IsActive 状态。

在这种情况下,CompositeCommand 将不会考虑不活动的 DelegateCommand,因此您的按钮可能会保持禁用状态(例如,当没有其他活动且可执行的 DelegateCommand 时)位于 CompositeCommands 的命令链中)。

When using PRISM if you are creating a CompositeCommand with monitorCommandActivity set to true, you also need to be aware of and set DelegateCommand.IsActive state.

In that case CompositeCommand will not consider inactive DelegateCommands and as a result your button might stay disabled (for example when no other active and executable DelegateCommand is in the CompositeCommands's command chain).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文