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
精彩评论