我正在使用Caliburn.Micro和ModernUI进行WPF应用程序。在我看来,我试图用Caliburn键手势和经典MVVM方式捕获键绑定,但是它们都没有捕获键绑定。
这是我的View.xaml:
<UserControl x:Class="IKA.Views.CameraView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:cal="http://www.caliburnproject.org"
xmlns:model="clr-namespace:IKA.ViewModels"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ika="clr-namespace:IKA"
d:DesignHeight="300" d:DesignWidth="300"
cal:Message.Attach="[Gesture Key: Escape] = [Action Cancel()]">
<UserControl.InputBindings>
<KeyBinding Modifiers="Control" Key="Escape" Command="{Binding Cancel}" />
</UserControl.InputBindings>
<Grid x:Name="grid">
<Grid.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/DirectionControl.xaml" />
<ResourceDictionary Source="/Resources/MediaPlayer.xaml" />
</ResourceDictionary.MergedDictionaries>
<ControlTemplate x:Key="PlayStyle" TargetType="Button">
<Button Style="{StaticResource PlayButtonStyle}" Width="100" Height="100" HorizontalAlignment="Center" />
</ControlTemplate>
<ControlTemplate x:Key="StopStyle" TargetType="Button">
<Button Style="{StaticResource StopButtonStyle}" Width="100" Height="100" HorizontalAlignment="Center" />
</ControlTemplate>
</ResourceDictionary>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Rectangle x:Name="BeforeVideo" Fill="Black" Grid.Row="0" Grid.RowSpan="6" />
<!--<wpf:VlcControl Grid.Row="0" x:Name="myControl"/>-->
<MediaElement x:Name="myControl" Grid.Row="0" Grid.RowSpan="6" Stretch="Fill" LoadedBehavior="Manual" UnloadedBehavior="Stop" />
<Button x:Name="PlayButton" Grid.Row="0" Grid.RowSpan="6" Click="OnPlayButtonClick" Template="{StaticResource PlayStyle}">Play</Button>
<Button x:Name="StopButton" Grid.Row="0" Grid.RowSpan="6" Click="OnStopButtonClick" Template="{StaticResource StopStyle}">Stop</Button>
<Canvas Grid.Column="1" Grid.Row="4" Grid.RowSpan="2" Height="100" Width="100">
<Ellipse Stretch="Fill" Fill="Aqua" Width="100" Height="100" />
<Line Name="CurrentLine" X1="50" Y1="50" X2="50" Y2="0" Stroke="Black" StrokeThickness="1" RenderTransformOrigin="1,1">
<Line.RenderTransform>
<RotateTransform x:Name="LineRotateTransform" Angle="0" ></RotateTransform>
</Line.RenderTransform>
<Line.Triggers>
<EventTrigger RoutedEvent="Line.MouseMove">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Line.RenderTransform).(RotateTransform.Angle)" To="160" Duration="0:0:2" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Line.Triggers>
</Line>
</Canvas>
</Grid>
我的View.xaml.cs:
public partial class CameraView : UserControl
{
public CameraView()
{
InitializeComponent();
this.DataContext = ViewModelsContainer.CameraViewModel;
myControl.Visibility = Visibility.Visible;
StopButton.Visibility = Visibility.Hidden;
PlayButton.Visibility = Visibility.Visible;
string source = "http://192.168.1.159:8080/";
myControl.Source = new Uri(source);
}
private void OnPlayButtonClick(object sender, RoutedEventArgs e)
{
BeforeVideo.Visibility = Visibility.Hidden;
PlayButton.Visibility = Visibility.Hidden;
myControl.Visibility = Visibility.Visible;
PlayVideo();
StopButton.Visibility = Visibility.Visible;
}
private void OnStopButtonClick(object sender, RoutedEventArgs e)
{
myControl.Pause();
StopButton.Visibility = Visibility.Hidden;
PlayButton.Visibility = Visibility.Visible;
}
private async void PlayVideo()
{
await PlayAsync();
}
private Task PlayAsync()
{
Task task = Task.Run(() => this.Dispatcher.Invoke(new Action(() => { myControl.Play(); })));
return task;
}
}
我的ViewModel:
public class CameraViewModel:Screen
{
public CameraViewModel()
{
}
public void Cancel()
{
MessageBox.Show("Do you want to cancel?");
}
}
当我按下Esc键(或其他键)时,没有任何反应。我怎么解决这个问题?我想这可能是关于ModernUI的。
编辑:dkozl评论后在ViewModel中进行编辑。
public CameraViewModel()
{
CreateCancelCommand();
}
private void CreateCancelCommand()
{
Cancel = new RelayCommand(CancelExecute);
}
public void CancelExecute(object obj)
{
MessageBox.Show("Do you want to cancel?");
}
public ICommand Cancel{get; internal set; }
}
但它并没有改变任何事情。