错误信息如下,出现频率很高:
Navigation is not allowed when the task is not in the foreground.
at System.Windows.Navigation.NavigationService.Navigate(Uri source)
at carquey.StartMain.dTimer_Tick(Object sender, EventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
public partial class StartMain : PhoneApplicationPage
{
DispatcherTimer dRanTimer;
int CountdownCount = 5;
public StartMain()
{
InitializeComponent();
Countdown();
}
private void Countdown()
{
dRanTimer = new System.Windows.Threading.DispatcherTimer();
dRanTimer.Interval = new TimeSpan(0, 0, 1);
//设置事件处理函数
dRanTimer.Tick += new EventHandler(dTimer_Tick);
dRanTimer.Start();
}
private void dTimer_Tick(object sender, EventArgs e)
{
CountdownCount--;
if (CountdownCount <= 0)
{
dRanTimer.Stop();
// 这里出错误了啊?
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
}
}
}
我将:
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
换成
this.Dispatcher.BeginInvoke(() =>
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
});
问题依然存在.此错误只在部分机型,不是所有机型.