开发者

Hook Up Command for BackKeyPress in XAML

开发者 https://www.devze.com 2023-02-17 17:05 出处:网络
Is there a way to hook up the开发者_开发知识库 BackKeyPressed event to a command in a view (XAML)? I\'m using MVVM Light.

Is there a way to hook up the开发者_开发知识库 BackKeyPressed event to a command in a view (XAML)? I'm using MVVM Light.

I have a few login/signup screens that can possibly be shown. If they're in the login/signup process, which is just showing/hiding user controls, I want to be able to intercept the back button so I can show/hide the appropriate control.


Note that I'm not familiar with WP7 development, but I believe MVVM Light handles WPF4 & WP7 similarly:

<PhoneApplicationPage
    xmlns:Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP7"
    xmlns:Interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
    <Interactivity:Interaction.Triggers>
        <Interactivity:EventTrigger
            EventName="BackKeyPress">
            <Command:EventToCommand
                Command="{Binding BackKeyPressCommand}" />
        </Interactivity:EventTrigger>
    </Interactivity:Interaction.Triggers>
</PhoneApplicationPage>


You might be able to do this by writing your own BasePage class that inherits from PhoneApplicationPage and that exposes a public event, and which then triggers that event in the override OnBackKeyPress method

In your custom page, you can then inherit your new BasePage class instead of PhoneApplicationPage and can then hopefully bind that event to a command in the XAML.

However, in this particular case, I think I might be tempted to use C# rather than XAML - e.g. calling Execute directly in OnBackKeyPress

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号