c# - MVVM Light EventToCommand无法拦截UserControl事件的问题

标签 c# wpf mvvm mvvm-light eventtocommand

我有一个WPF项目,正在使用MVVM Light。在项目的页面ConnectionPage上,我有一个UserControl(ctlSqlConnection),可以引发Connected事件。我正在尝试使用MVVM Light来通过EventToCommand拦截此事件,并在NavigateToDatabasePageCommand的viewmodel(不是UserControl自己的ViewModel!)上执行一个名为ConnectionPage的命令,但这似乎不起作用。也就是说,什么都没有发生。

由于正确填充了页面的UI,因此DataContextConnectionPage设置正确。

我知道该事件正在引发,因为我还连接了一个传统的.NET处理程序,因此受到了打击。

如果有任何影响,我正在使用5.3版本的MVVM Light。

我是MVVM和工具包的新手,所以我希望自己做的事很愚蠢。

更新

从那以后,我在UserControl中查看了事件本身,该事件声明为

public event EventHandler<ConnectionSettingViewModel> Connected;

但是当我放置另一个非通用事件处理程序时,例如:
        public event EventHandler ConnectedWithNoArgs;

这使其工作!?

所以,
  • 这是否表示“交互”部分不能处理通用事件声明,或者我做错了什么吗?
  • 我如何将EventArgs从事件传递到Nav()方法

  • 这是ConnectionPage的XAML:
    <Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib" 
    x:Class="Views.ConnectionPage"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:data="clr-namespace:Utilities.Data;assembly=Utilities"
    xmlns:util="clr-namespace:Utilities.Data;assembly=Utilities"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
    
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="300"
    Title="ConnectionPage"
    DataContext="{Binding Source={StaticResource Locator}, Path=ConnectionPageViewModel}" x:Name="connPage" >
    
    <Grid x:Name="rootGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
    
        <Label Grid.Row="0" Content="Create / Select Sql Server connection" Style="{StaticResource HeaderStyle}"/>
    
        <util:SqlServerConnectionControl Grid.Row="1" x:Name="ctlSqlConnection" DataContext="{Binding SqlServerConnectionControlViewModel}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Connected">
                    <command:EventToCommand Command="{Binding ElementName=connPage, Path=DataContext.NavigateToDatabasePageCommand}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </util:SqlServerConnectionControl>
    </Grid>
    

    这是ConnectionPageViewModel:
        public class ConnectionPageViewModel : ViewModelBase
    {
        SqlServerConnectionControlViewModel _serverCtrlVM;
        Avalon _avalon;
        IFrameNavigationService _navService;
    
        public ConnectionPageViewModel(IFrameNavigationService navigationService, Avalon avalon)
        {
            _avalon = avalon;
            _navService = navigationService;
            _serverCtrlVM = new SqlServerConnectionControlViewModel(avalon.ConnectionStringManager);
    
            NavigateToDatabasePageCommand = new RelayCommand(Nav);
        }
        private void Nav()
        {
           // NOT GETTING HERE!!!
            _navService.NavigateTo("DatabasePage");
        }
    
        public SqlServerConnectionControlViewModel SqlServerConnectionControlViewModel
        {
            get { return _serverCtrlVM; }
        }
    
        public RelayCommand NavigateToDatabasePageCommand { get; private set; }
    
    }
    

    最佳答案

    好的,我想我已经弄清楚了,以防万一这对其他人有帮助。
    Connected事件为public event EventHandler<ConnectionSettingViewModelEventArgs> Connected;
    我在PassEventArgsToCommand="True" xaml中添加了EventToCommand
    我将RelayCommand更改为RelayCommand<T>,其中T:EventArgs(这很重要!),并将ConnectionSettingViewModelEventArgs用作T。

    关于c# - MVVM Light EventToCommand无法拦截UserControl事件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41960157/

    相关文章:

    c# - 在 C# 中捕获 Plink 输出

    c# - MVC 5 C#将错误报告回数据库

    c# - 将列表中类的属性从 ObservableCollection 绑定(bind)到数据网格

    c# - 如何在 Azure 中搜索包含引号(转义单引号)的文本?

    wpf - 使用 WPF MVVM 运行动画

    c# - 使用不带 ViewSortHintAttribute 的通用 View 模型进行 Prism 区域排序

    c# - 不同窗口中两个文本框之间的数据绑定(bind)

    c# - Josh Smith MVVM 演示应用程序

    wpf - MVVM 限制代码背后?

    c# - C#中的goto语句