Hook Up Command for BackKeyPress in XAML

风流意气都作罢 提交于 2019-12-02 05:28:24

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

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