微软的wp8.1 sdk相比之前wp8 sdk以及相关dll类库,微软又重新编译过,相关系统类库也经过精简,删改了部分传统dll库中的方法对象,很多常用方法对象被写进Windows.UI为前缀的命名空间中,可以看出微软wp8.1经过了一定的优化。
此处功能设计描述为,触摸一次返回键,提示是否退出app,再点一次即关闭app。
1 <Grid Background="#F5F5F5" DataContext="{Binding Path=MainPageViewModel, Source={StaticResource Locator}}"> 2 <Grid Canvas.ZIndex="10000" Visibility="{Binding ExitNotificationVisible}" Height="30" Background="Red" VerticalAlignment="Top"> 3 <TextBlock FontSize="14" Text="提示: 再按一次返回键退出支付宝" VerticalAlignment="Center" Margin="10 0 0 0" /> 4 </Grid> 5 <ContentControl HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Content="{Binding DynamicContent}" /> 6 </Grid>
1 using System; 2 using Windows.UI.Xaml; 3 using Windows.UI.Xaml.Controls; 4 using Windows.UI.Popups;
1 public class MainPageViewModel : Core.ViewModelBase 2 { 3 public MainPageViewModel() 4 { 5 InitData(); 6 if(DynamicContent == null) 7 { 8 DynamicContent = ViewControlLocator.Instance.RegisterViewControl<HomePage, HomePageViewModel>(true); 9 } 10 //---物理返回键api--- 11 Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed; 12 } 13 14 #region attribute 15 //---计时器--- 16 DispatcherTimer timer = null; 17 int timesToTick = 2; 18 19 private UserControl m_DynamicContent; 20 public UserControl DynamicContent 21 { 22 get { return m_DynamicContent; } 23 set { m_DynamicContent = value; RaisePropertyChanged("DynamicContent"); } 24 } 25 26 private Visibility m_ExitNotificationVisible; 27 public Visibility ExitNotificationVisible 28 { 29 get { return m_ExitNotificationVisible; } 30 set { m_ExitNotificationVisible = value; RaisePropertyChanged("ExitNotificationVisible"); } 31 } 32 #endregion 33 34 #region method 35 private void InitData() 36 { 37 ExitNotificationVisible = Visibility.Collapsed; 38 } 39 40 //---关闭事件--- 41 void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e) 42 { 43 e.Handled = true; 44 if (ExitNotificationVisible == Visibility.Collapsed) 45 { 46 ExitNotificationVisible = Visibility.Visible; 47 48 if(timer == null) 49 { 50 timer = new DispatcherTimer(); 51 } 52 timer.Interval = TimeSpan.FromSeconds(2); 53 timer.Tick += timer_Tick; 54 timer.Start(); 55 } 56 else 57 { 58 App.Current.Exit(); 59 } 60 } 61 62 //---到时没有执行关闭操作,隐藏关闭操作提示--- 63 void timer_Tick(object sender, object e) 64 { 65 timesToTick--; 66 if (timesToTick == 0) 67 { 68 timesToTick = 2; 69 timer.Tick -= timer_Tick; 70 timer.Stop(); 71 ExitNotificationVisible = Visibility.Collapsed; 72 } 73 } 74 #endregion 75 }