UserControl事件未被MVVM Light EventToCommand拦截的问题

问题描述 投票:1回答:1

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

DataContextConnectionPage正在设置正确,因为页面的UI正在正确填充。

我知道事件正在被提出,因为我还连接了一个传统的.NET处理程序,而这正受到影响。

我正在使用MVVM Light的5.3版,以防它有任何影响吗?

我是MVVM和工具包的新手,所以我希望我做的事情很傻。

更新

我已经在UserControl中查看了事件本身,它被声明为

public event EventHandler<ConnectionSettingViewModel> Connected;

但是当我把另一个非通用事件处理程序改为:

        public event EventHandler ConnectedWithNoArgs;

这使它工作!?

所以,

  1. 这是否意味着交互部分不能处理通用事件声明,或者我不做正确的事情?
  2. 如何将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; }

}
c# wpf mvvm mvvm-light eventtocommand
1个回答
1
投票

好吧,我想我已经弄清楚了,以防万一这有助于其他人。

Connected事件是public event EventHandler<ConnectionSettingViewModelEventArgs> Connected;

我将PassEventArgsToCommand="True"添加到EventToCommand xaml。我将RelayCommand更改为RelayCommand<T>,其中T:EventArgs(这是重要的位!)并使用ConnectionSettingViewModelEventArgs作为T.

© www.soinside.com 2019 - 2024. All rights reserved.