如果在Windows手机中访问应用程序,如何导航到运行屏幕?

时间:2013-06-21 14:49:52

标签: .net windows-phone-7 xaml c#-4.0 windows-phone-8

我正在尝试开发位置跟踪功能。在这个应用程序中,第一个用户打开主屏幕。然后他选择运行导航到第二个屏幕的应用程序。然后它也可以在后台运行。现在,如果用户打开应用程序,则应该将其直接转发到第二个屏幕,而不是再次转发到第一个屏幕。怎么样?任何建议,请发布。
//代码

 private void Timer_Tick(object sender, EventArgs e)
        {  TimeSpan runTime = TimeSpan.FromMilliseconds(System.Environment.TickCount - _startTime);
    timeLabel.Text = runTime.ToString(@"hh\:mm\:ss");
        }

1 个答案:

答案 0 :(得分:1)

最简单的方法(我认为)是,在加载第一个屏幕时,重定向到第二个屏幕,然后从后台堆栈中删除页面。

在第一个屏幕中:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    NavigationService.Navigate(new Uri("/SecondScreen.xaml", UriKind.Relative));
}

在第二个屏幕中:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    NavigationService.RemoveBackEntry();
}

另一种方法是在调用第一个屏幕时使用自定义URI映射器显示第二个屏幕。它有点复杂,但你可以避免现在无用的导航到第一个屏幕。此代码应仅在导航之前调用一次,理想情况是在应用程序在App.xaml.cs中初始化期间调用:

var mapper = new UriMapper();

int random = new Random().Next(0, 3);

mapper.UriMappings.Add(new UriMapping
{
    Uri = new Uri("/FirstScreen.xaml", UriKind.Relative),
    MappedUri = new Uri("/SecondScreen.xaml", UriKind.Relative)
});

Application.Current.RootFrame.UriMapper = mapper;