0

次の問題を解決しようとして苦労しています。

ビュー付きの 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);
    }

これが問題にもう少し光を当てることを願っています。

4

1 に答える 1

0

「Enter」キーは、OnKeyDown イベントでクリック イベントをトリガーしています。Enter キーを押すとモジュール B が閉じ、モジュール A が再びアクティブになります。そのため、キーが離されるまでに親フォームがアクティブになり、キーアップメッセージを取得します。

つまり、OnKeyUp ではなく、OnKeyDown で Enter キーを処理します。

(ところで:「スペース」キーはOnKeyUpでクリックイベントをトリガーしています。)

于 2010-07-06T14:12:59.550 に答える