簡體   English   中英

C#-如何在MVVM中處理XAML鍵盤?

[英]C# - How to handle XAML Keyboard in MVVM?

到目前為止,我一直使用.xaml.cs來處理輸入。

這是我的xaml頭代碼:

<Window x:Class="My_Windows_App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:My_Windows_App.ViewModel"
Title="MainWindow" Height="600" Width="900"
Keyboard.KeyDown="keyDownEventHandler" Keyboard.KeyUp="keyUpEventHandler">

這是MainWindow.xaml.cs代碼的一部分:

public partial class MainWindow : Window
    {
        private bool pushToTalk;

        public void keyDownEventHandler(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.LeftCtrl)
                pushToTalk = true;
        }

        public void keyUpEventHandler(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.LeftCtrl)
                pushToTalk = false;
        }
}

如何在MVVM中設置相同的內容? 因為據我了解,我們不能綁定方法,只能綁定屬性,對嗎?

似乎您已找到答案,但這是解決方案的另一種說法。
您可以使用i:interactionInputBindings處理鍵盤事件。 使用DatePicker進行交互的一些示例為:

<DatePicker Name="fancy_name_here" SelectedDate="{Binding your_date_property_here}">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="PreviewKeyDown">               
    <i:InvokeCommandAction Command="{Binding Path=TestMeCommand}" />
    </i:EventTrigger>
  </i:Interaction.Triggers>
</DatePicker>

(請注意,您需要在窗口定義中的下一行才能使i:Interaction起作用:)

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

對於輸入綁定:

<DatePicker Name="fancy_name_here"  SelectedDate="{Binding your_date_property_here}">
  <DatePicker.InputBindings>
    <KeyBinding Key="Up" Modifiers="Shift" Command="{Binding your_command_name}" />
    <KeyBinding Key="Up" Modifiers="Alt" Command="{Binding TestMeCommand}" />       
    <KeyBinding Key="Up" Command="{Binding TestMeCommand}" />        
  </DatePicker.InputBindings>
</DatePicker>

請隨意查看我的其他答案 ,它討論了i:interactionInputBindings ,並指出了另一篇有關處理關鍵事件的文章:datepicker的上/下 ,並討論了/ mvvm方法背后的代碼。

希望您會發現它們有用。

輸入處理是View的關注點,而不是ViewModel的關注點,為什么要將其移至ViewModel?

而是將應用程序/業務邏輯委托給ViewModel,同時將鍵盤輸入處理保留在View中:

public partial class MainWindow : Window
{
    private MyViewModel ViewModel;

    public MainWindow()
    {
        ViewModel = new MyViewModel();
    }


    public void keyDownEventHandler(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.LeftCtrl)
            ViewModel.PushToTalk = true;
    }

    public void keyUpEventHandler(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.LeftCtrl)
            ViewModel.PushToTalk = false;
    }
}

請注意,我是如何將PushToTalk屬性移到ViewModel的,因為這實際上是應用程序邏輯的一部分,而不是UI的一部分,同時將Keyboard事件保持在View級別。 這不會破壞MVVM,因為您沒有將UI和應用程序邏輯混合在一起,只是將它們放置在真正屬於它們的地方。

以下用於處理TextBox中的“ Enter”鍵:

<TextBox Text="{Binding UploadNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
    <KeyBinding 
      Key="Enter" 
      Command="{Binding FindUploadCommand}" 
      CommandParameter="{Binding Path=Text, RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}" />
</TextBox.InputBindings>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM