1
我工作的一个综合应用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); ;
}
}
你在哪里构造AdminModuleViewModel?它如何设置为一个DataContext? 另外,你是否真的需要该命令的“全局性”,或者你只是试图将这一个Button objects命令连接到ViewModel中的处理程序?如果是的话,我可以发布一个更简单的方法来完成 – 2010-02-25 18:29:27
数据上下文是通过棱镜设置解决方案...在这个例子中,我希望命令从这个模块运行。例如它是复合的,并且shell不知道AdminModule,因此我不能将它放在主应用程序中,因此Globl Commands Class。 (全局命令类是在应用程序基础结构中,并且不能“知道”应用程序模块。我试图从admin模块获得成果,如果命令从应用程序中的任何位置触发,菜单将显示。 – Traci 2010-02-26 04:56:28