Prism - 命令未触发

发布于 2024-08-03 17:34:21 字数 2253 浏览 1 评论 0原文

我有一个视图

<UserControl x:Class="Modules.NavigationMenu.Views.NavigationMenuView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<StackPanel>
    <Button Command="{Binding InspectionCommand}">Inspection</Button>
    <Button Command="{Binding HandheldCommand}">Handheld</Button>
</StackPanel>
</UserControl>

和一个简单的视图模型 - 但命令似乎不会触发 - 谁能指出我正确的方向吗?

public class NavigationMenuViewModel : INavigationMenuViewModel
{
    public INavigationMenuView View { get; set; }

    public NavigationMenuViewModel(IEventAggregator eventAggregator, INavigationMenuView view)
    {
        View = view;
        HandheldCommand = new DelegateCommand<object>(LaunchHandheld);
        InspectionCommand = new DelegateCommand<object>(LaunchInspection);
    }

    private void LaunchInspection(object obj)
    {
        MessageBox.Show("Inspection Clicked");
    }


    private void LaunchHandheld(object obj)
    {
        MessageBox.Show("Handheld Clicked");
    }

    public DelegateCommand<object> HandheldCommand { get; set; }
    public DelegateCommand<object> InspectionCommand { get; set; }

}

我的模块看起来像......

public class NavigationMenuModule : IModule
{
    private readonly IUnityContainer _container;
    private readonly IRegionManager _regionManager;

    public NavigationMenuModule(IUnityContainer container, IRegionManager regionManager)
    {
        _container = container;
        _regionManager = regionManager;
    }

    #region Implementation of IModule

    public void Initialize()
    {
        RegisterViewsAndServices();
        var viewModel = _container.Resolve<INavigationMenuViewModel>();
        _regionManager.Regions[RegionNames.MainMenu].Add(viewModel.View);

    }

    #endregion

    #region Protected Methods

    protected void RegisterViewsAndServices()
    {
        _container.RegisterType<INavigationMenuView, NavigationMenuView>();
        _container.RegisterType<INavigationMenuViewModel, NavigationMenuViewModel>();
    }

    #endregion
}

I've got a view

<UserControl x:Class="Modules.NavigationMenu.Views.NavigationMenuView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<StackPanel>
    <Button Command="{Binding InspectionCommand}">Inspection</Button>
    <Button Command="{Binding HandheldCommand}">Handheld</Button>
</StackPanel>
</UserControl>

and a simple view model - but the commands won't seem to fire - can anyone point me in the right direction please?

public class NavigationMenuViewModel : INavigationMenuViewModel
{
    public INavigationMenuView View { get; set; }

    public NavigationMenuViewModel(IEventAggregator eventAggregator, INavigationMenuView view)
    {
        View = view;
        HandheldCommand = new DelegateCommand<object>(LaunchHandheld);
        InspectionCommand = new DelegateCommand<object>(LaunchInspection);
    }

    private void LaunchInspection(object obj)
    {
        MessageBox.Show("Inspection Clicked");
    }


    private void LaunchHandheld(object obj)
    {
        MessageBox.Show("Handheld Clicked");
    }

    public DelegateCommand<object> HandheldCommand { get; set; }
    public DelegateCommand<object> InspectionCommand { get; set; }

}

My Module just looks like ...

public class NavigationMenuModule : IModule
{
    private readonly IUnityContainer _container;
    private readonly IRegionManager _regionManager;

    public NavigationMenuModule(IUnityContainer container, IRegionManager regionManager)
    {
        _container = container;
        _regionManager = regionManager;
    }

    #region Implementation of IModule

    public void Initialize()
    {
        RegisterViewsAndServices();
        var viewModel = _container.Resolve<INavigationMenuViewModel>();
        _regionManager.Regions[RegionNames.MainMenu].Add(viewModel.View);

    }

    #endregion

    #region Protected Methods

    protected void RegisterViewsAndServices()
    {
        _container.RegisterType<INavigationMenuView, NavigationMenuView>();
        _container.RegisterType<INavigationMenuViewModel, NavigationMenuViewModel>();
    }

    #endregion
}

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

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

发布评论

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

评论(1

无悔心 2024-08-10 17:34:21

您确定命令绑定有效吗?如果您运行应用程序并查看调试输出面板,您是否看到任何绑定警告?也许您的 UserControl 的 DataContext 未设置为您的 NavigationMenuViewModel。

Are you sure that the command binding is working? If you run the app and look in the debug output panel, do you see any binding warnings? Perhaps the DataContext of your UserControl isn't set to your NavigationMenuViewModel.

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