Hook Up Command for BackKeyPress in XAML

瘦欲@ 提交于 2019-12-04 05:21:32

问题


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.


回答1:


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>



回答2:


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



来源:https://stackoverflow.com/questions/5363958/hook-up-command-for-backkeypress-in-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!