我有一个WPF项目,正在使用MVVM Light。在项目的页面ConnectionPage
上,我有一个UserControl(ctlSqlConnection
),可以引发Connected
事件。我正在尝试使用MVVM Light来通过EventToCommand
拦截此事件,并在NavigateToDatabasePageCommand
的viewmodel(不是UserControl自己的ViewModel!)上执行一个名为ConnectionPage
的命令,但这似乎不起作用。也就是说,什么都没有发生。
由于正确填充了页面的UI,因此DataContext
的ConnectionPage
设置正确。
我知道该事件正在引发,因为我还连接了一个传统的.NET处理程序,因此受到了打击。
如果有任何影响,我正在使用5.3版本的MVVM Light。
我是MVVM和工具包的新手,所以我希望自己做的事很愚蠢。
更新
从那以后,我在UserControl中查看了事件本身,该事件声明为
public event EventHandler<ConnectionSettingViewModel> Connected;
但是当我放置另一个非通用事件处理程序时,例如:
public event EventHandler ConnectedWithNoArgs;
这使其工作!?
所以,
这是
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/