2014-02-24 189 views
0

应用程序流是这样;转到主屏幕

  1. 用户启动应用程序,看到一个页面,写有“后台任务启动”
  2. 如果用户点击后退按钮的应用程序终止,也后台任务结束
  3. 但如果用户点击菜单按钮,使用其他应用程序,然后我的应用程序继续运行

现在我有两种解决方案;请帮我解决这个问题。答:我应该有“OK”按钮;如果用户点击它,主菜单应该打开 B.在后退按钮中写什么,以便应用程序不终止。

In app.xaml

private static Geolocator locator;

public static Geolocator Locator 
    { 
     get 
     { 
      lock (typeof(App)) 
      { 
       if (locator == null) 
       { 
        locator = new Geolocator(); 
        locator.DesiredAccuracy = PositionAccuracy.High; 
        //locator.MovementThreshold = 50; 
        locator.ReportInterval = 10000; 
       } 
      } 
      return locator; 
     } 
    } 

在MainPage.xaml.cs中

保护覆盖无效的OnNavigatedTo(NavigationEventArgs E) { App.Locator.PositionChanged + = Locator_PositionChanged; }

感谢

+1

你需要给我们展示一些代码 –

+0

你应该更具体地描述你的问题。现在,我可以告诉你,你可以覆盖“后退”按钮,但我不知道如何使用原生的“主页”按钮行为,即使有方法,应用程序也可能无法通过认证处理。 – Olter

+0

只是告诉我如何创建主屏幕实例移动到家庭(瓷砖)屏幕上点击按钮! – user3102858

回答

1

最有可能的,有没有办法来实现这一目标。

您可以覆盖后退按钮点击事件,但不能覆盖主页按钮事件。

此外,你甚至不能以编程方式调用本地主页按钮事件。

有在msdn forums


更新有类似的问题的链接。你仍然认为,问题出在后退和菜单按钮上。这对我来说似乎是错误的。问题是,当Application_Closing事件被命中时,后台代理程序未被激活。

有在app.xaml.cs两种方法:

Application_Deactivated和Application_Closing。你的后台任务应该是活动的,如果其中任何一个被执行。

// Code to execute when the application is deactivated (sent to background, e.g. menu button is hit) 
// This code will not execute when the application is closing 
private void Application_Deactivated(object sender, DeactivatedEventArgs e) 
{ 

} 

// Code to execute when the application is closing (eg, user hit Back) 
// This code will not execute when the application is deactivated 
private void Application_Closing(object sender, ClosingEventArgs e) 
{ 

} 

在后台代理构造函数中设置一个断点,看它是否被命中。

顺便说一句,你的两个解决方案:

解决方法A无法做到的。 解决方案B可以通过重写OnNavigatedFrom方法来完成。

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) 
     {   
      // nothing will happen here 
      //base.OnNavigatedFrom(e); 
     } 

但是,这将是对windows-phone导航服务原生行为的粗鲁违反。那不会通过认证。

+0

那我该怎么办?当用户按下后退按钮;应用程序终止!其实它的一个页面的应用程序。一旦用户打开它;它会在后台运行,但如果用户按回来,那么应用程序终止 – user3102858

+0

后台代理是否也终止?这有点奇怪,因为windows-phone总是存在一个非关闭后台代理的问题,这让很多人恼火,但我从来没有听说过,代理初始化后会在应用退出时处理。你什么时候初始化你的代理(也许它没有在应用程序关闭时被初始化)? – Olter

+0

好吧,我没有在Close事件中初始化;好的谢谢;我打算在关闭时执行此操作:)再次感谢 – user3102858