我有一个WPF项目,我正在使用MVVM Light。在项目的页面上,ConnectionPage
,我有一个可以引发ctlSqlConnection
事件的UserControl(Connected
)。我试图使用MVVM Light用EventToCommand
拦截这个事件,并在NavigateToDatabasePageCommand
的viewmodel上执行一个名为ConnectionPage
的命令(不是UserControl自己的ViewModel!),但这似乎没有用。也就是说,什么也没发生。
DataContext
的ConnectionPage
正在设置正确,因为页面的UI正在正确填充。
我知道事件正在被提出,因为我还连接了一个传统的.NET处理程序,而这正受到影响。
我正在使用MVVM Light的5.3版,以防它有任何影响吗?
我是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"
添加到EventToCommand
xaml。我将RelayCommand
更改为RelayCommand<T>
,其中T:EventArgs(这是重要的位!)并使用ConnectionSettingViewModelEventArgs
作为T.