1

我正在开发一个复合 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); ;
        }
    }
4

1 回答 1

4

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

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

于 2012-05-25T16:39:54.887 回答