2010-08-26 15 views
6

मैं वर्तमान कीबोर्ड शॉर्टकट बनाने के लिए onKeyDown घटना और एक if/else बयान का उपयोग करें:लागू कीबोर्ड शॉर्टकट

if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift && e.Key == Key.Tab) { 

} else if (e.Key == Key.Tab) { 

} ... 

हालांकि, अगर मैं काफी कुछ और कीबोर्ड शॉर्टकट है, यह गंदा हो जाता है।

क्या कोई बेहतर कार्यान्वयन है?

उत्तर

17

आप <CommandBindings> और <InputBindings> को लागू करने पर गौर करना चाहिए:

<Window.CommandBindings> 
    <CommandBinding Command="Settings" CanExecute="SettingsCanExecute" Executed="SettingsExecuted" /> 
</Window.CommandBindings> 

<Window.InputBindings> 
    <KeyBinding Command="Settings" Key="S" Modifiers="Alt" /> 
</Window.InputBindings> 

आपका <Button> तो हो जाता है:

<Button Height="50" Width="50" Margin="50,5,0,0" Command="Settings" /> 

SettingsCanExecute तरीका निर्धारित करता है जब बटन सक्षम है और SettingsExecuted विधि जब बटन कहा जाता है दबाया जाता है या कुंजी संयोजन मारा जाता है।

आपको KeyDown हैंडलर की आवश्यकता नहीं है।

कोड पर स्विच पर full tutorial है।

एमएसडीएन पर CommandBindings और InputBindings पर अधिक जानकारी मिल सकती है।

+0

हम्म ... जब मैं करने की कोशिश की '

+0

मुझे यकीन नहीं है कि अगर मुझे ट्यूटोरियल में कुछ याद आया। लेकिन [जॉन स्मिथ ऑन डब्ल्यूपीएफ: अंडरस्टैंडिंग रूटेड कमांड] से डेमो कोड देख रहे हैं (http://joshsmithonwpf.wordpress.com/2008/03/18/understanding-routed-commands/) मुझे लगता है कि मुझे एक संपत्ति बनाने की आवश्यकता है एक वर्ग फिर कोड में संदर्भित करता है ... [स्निपलर कोड स्निपलेट] (http://snipplr.com/view/39602/cwpf-routed-commands/) –

1

दूसरों के लिए यह उत्तर दस्तावेज करना, क्योंकि ऐसा करने का एक बहुत ही आसान तरीका है जिसे शायद ही कभी संदर्भित किया जाता है, और XAML को स्पर्श करने की आवश्यकता नहीं होती है।

कीबोर्ड कन्स्ट्रक्टर को जोड़ने के लिए, विंडो कन्स्ट्रक्टर में बस इनपुटबिंडिंग संग्रह में एक नया कीबाइंडिंग जोड़ें। कमांड के रूप में, अपनी मनमानी कमांड कक्षा में प्रवेश करें जो आईसीओएमएंड लागू करता है। निष्पादन विधि के लिए, आपको जो भी तर्क चाहिए उसे लागू करें। नीचे दिए गए मेरे उदाहरण में, मेरी विंडो कॉमांड क्लास एक प्रतिनिधि लेता है जो इसे जब भी लागू किया जाएगा निष्पादित करेगा। जब मैं अपने बाध्यकारी के साथ पास करने के लिए नया विंडो कॉमांड बनाता हूं, तो मैं बस अपने प्रारंभकर्ता में इंगित करता हूं, जिस विधि को मैं विंडो कमांड को निष्पादित करना चाहता हूं।

आप इस पैटर्न का उपयोग अपने त्वरित कीबोर्ड शॉर्टकट के साथ आने के लिए कर सकते हैं।

public YourWindow() //inside any WPF Window constructor 
{ 
    ... 
    //add this one statement to bind a new keyboard command shortcut 
    InputBindings.Add(new KeyBinding(//add a new key-binding, and pass in your command object instance which contains the Execute method which WPF will execute 
     new WindowCommand(this) 
     { 
     ExecuteDelegate = TogglePause //REPLACE TogglePause with your method delegate 
     }, new KeyGesture(Key.P, ModifierKeys.Control))); 
    ... 
} 

एक साधारण विंडो कॉमांड क्लास बनाएं जो किसी भी विधि सेट को बंद करने के लिए निष्पादन प्रतिनिधि लेता है।

public class WindowCommand : ICommand 
{ 
    private MainWindow _window; 

    //Set this delegate when you initialize a new object. This is the method the command will execute. You can also change this delegate type if you need to. 
    public Action ExecuteDelegate { get; set; } 

    //You don't have to add a parameter that takes a constructor. I've just added one in case I need access to the window directly. 
    public WindowCommand(MainWindow window) 
    { 
     _window = window; 
    } 

    //always called before executing the command, mine just always returns true 
    public bool CanExecute(object parameter) 
    { 
     return true; //mine always returns true, yours can use a new CanExecute delegate, or add custom logic to this method instead. 
    } 

    public event EventHandler CanExecuteChanged; //i'm not using this, but it's required by the interface 

    //the important method that executes the actual command logic 
    public void Execute(object parameter) 
    { 
     if (ExecuteDelegate != null) //let's make sure the delegate was set 
     { 
      ExecuteDelegate(); 
     } 
     else 
     { 
      throw new InvalidOperationException("ExecuteDelegate has not been set. There is no method to execute for this command."); 
     } 
    } 
} 
+3

डब्ल्यूपीएफ के पीछे मूल विचारों में से एक को छूने से बचने के लिए था कोड के पीछे। आदर्श WPF/MVVM प्रोग्राम के पास कोड के करीब 0 लाइनें हैं। हालांकि यह ठीक काम करता है, यह अच्छा अभ्यास नहीं है। – Christopher

+0

दिलचस्प। क्या आपके पास इसका संदर्भ है? मैंने इसे पहले प्रिंट में कभी नहीं देखा है, लेकिन यदि यह सच है, तो यह जानना अच्छा होगा। धन्यवाद! –

+0

एमवीवीएम के लिए हर एक सीखने का उदाहरण मुझे पता है कि एक व्यू मॉडल बनाता है। XAML में मॉडल देखें जो तत्काल करता है। और मानव के रूप में संभव के रूप में बेयरबोन के रूप में कोड रखता है। आपको जो कुछ भी पीछे कोड में लिखना है वह परिभाषा के अनुसार उस विशिष्ट दृश्य से जुड़ा हुआ है। और दृश्य की मध्यस्थता एमवीवीएम में एक मौलिक अवधारणा है। इसके अलावा मेरा गणित शिक्षक भी कहता था "गणितज्ञ आलसी लेखकों हैं - जितना अधिक वे लिखते हैं, उतना ही वे गलतियां कर सकते हैं"। मैं इसे पूरी तरह प्रोग्रामिंग के लिए लागू करता हूं। – Christopher

संबंधित मुद्दे