我在尝试解决以下问题时遇到了麻烦。
我有 2 个带视图的模块(A 和 B)。
在模块 A 上,我有一个包含项目 1-4 的列表框。我有一个按键事件,每次按“Enter”键打开模块 B 时都会触发,该事件位于包含列表框的网格上。
在模块 B 上,我有一个关闭模块 B 并打开模块 A 的按钮。我在此控件上设置的唯一属性是 IsDefault = true。
当我按下“Enter”时,模块 B 会关闭,但模块 A 现在也捕获 Key Up 事件,这会导致按下“Enter”键的无限循环。
我已经为此苦苦挣扎了 2 天,因此将不胜感激。
下面的代码示例:
模块 A - .xaml
<UserControl x:Class="Module1.UxModule1"
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" d:DesignHeight="700" d:DesignWidth="800">
<Grid Focusable="False">
<Grid Name="MainGrid" KeyUp="MainGrid_KeyUp" Loaded="UserControl_Loaded">
<ListBox Grid.Column="0" Width="Auto" Foreground="White" Background="Black" Height="520" BorderThickness="0" Name="lstMenuLeft" IsTabStop="True" IsSynchronizedWithCurrentItem="False" IsTextSearchEnabled="False"></ListBox>
</Grid>
</Grid>
</UserControl>
模块 A - .cs
private void MainGrid_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
if (lstMenuLeft.SelectedItems.Count > 0)
{
MessageBox.Show(lstMenuLeft.SelectedItem.ToString());
OpenModuleB();
}
}
}
//standard prism code to inject new view.
private void OpenModuleB()
{
var regionManager = _container.Resolve<IRegionManager>();
var view = regionManager.Regions["Main"].GetView("uxMod2");
if (view == null)
{
var m = _container.Resolve<IModuleManager>();
m.LoadModule("Mod2");
}
else
{
regionManager.Regions["Main"].Activate(view);
}
}
//make sure i have focus on the listbox to allow my keyboard to move up and down.
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
lstMenuLeft.Focus();
}
模块 B - .xaml
<UserControl
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"
d:DesignHeight="800" d:DesignWidth="600">
<Grid Height="805" Width="Auto" Name="mainGrid" KeyUp="Grid_KeyUp_1" Background="#FFF5EFEF" Loaded="mainGrid_Loaded">
<Label Content="Module 2 View" FontSize="24" Foreground="#FF9C03FC" Height="47" HorizontalAlignment="Left" Margin="174,12,0,0" Name="label1" VerticalAlignment="Top" Width="174" />
<Label Content="Module 2 View" FontSize="20" Foreground="#FFFF940A" Height="41" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label2" VerticalAlignment="Top" Width="146" FontStyle="Italic" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="496,28,0,0" Name="textBox1" VerticalAlignment="Top" Width="92" />
<Grid Focusable="True" Margin="0,84,0,457">
<Button Content="Switch Module" Height="23" IsDefault="True" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="119" IsDefault="True" Click="button1_Click" />
</Grid>
</Grid>
</UserControl>
模块 B - .cs
private void button1_Click(object sender, RoutedEventArgs e)
{
var regionMan = _container.Resolve<IRegionManager>();
var prevView2 = regionMan.Regions["Main"].GetView("uxMod1");
regionMan.Regions["Main"].Activate(prevView2);
}
我希望这对这个问题有更多的了解。