如何使用计时器导航到 phone sdk 8.1 (xaml c#) 中的不同页面

How to navigate to a different page in phone sdk 8.1 (xaml c#) using a timer

我设法开始学习如何制作 phone 应用程序。我是 c# 和 xaml 的新手。但是我做了visual basic到一个基础知识。

我正在尝试在 windows phone 中重新创建我的项目,我在计时器中有基本的进度条,但是当计时器到达终点时,我希望它导航到我创建的新页面。我见过很多不同的例子,但 none 有帮助它要么不起作用,要么有错误。

这是目前主页上的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Windows.Threading;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using ProgramOSMobile.Resources;

namespace ProgramOSMobile
{
    public partial class MainPage : PhoneApplicationPage
    {

        private DispatcherTimer timer;
        private int i, j;

        public MainPage()
        {
            InitializeComponent();
            timer = new DispatcherTimer();
            timer.Tick += timer_tick;
            Init();
            timer.Start();
        }

        private void Init()
        {
            j = i = Convert.ToInt32(3);
            timer.Interval = TimeSpan.FromMilliseconds(i);
        }

        private void timer_tick(object sender, EventArgs e)
        {
            progressBar1.Value = i;
            i = i + j;
            if (i == 1010)
            {

                timer.Stop();
            }
        }
    }
}

这一切都有效,但是当涉及到使用此代码时:

NavigationService.Navigate(new Uri("/LoginScreen.xaml", UriKind.Relative));

或者确实是 Phone 8.1 的新版本:

this.Frame.Navigate(typeof(LoginScreen));

我在 'Frame' 部分遇到错误。

抱歉这个问题太长了,但我真的很困惑。

谢谢, 旦

据我所知,您正在使用 Windows Phone 8.1 Silverlight 项目,您遇到的错误在 WinRT 版本中使用。这是定时器后的导航代码。

对于 WinRT 受保护的覆盖 void OnNavigatedTo(NavigationEventArgs e) {

        var timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(300) };
        timer.Start();
        timer.Tick += ((ax, by) =>
        {
            timer.Stop();
            Loader.IsActive = false;


            this.Frame.Navigate(typeof(HomePage));
        });
    }

对于您正在使用的 Silverlight 项目

public MainPage()
        {
            InitializeComponent();
            Loaded+=MainPage_Loaded;
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            var timer = new System.Windows.Threading.DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(2);
            timer.Start();
            timer.Tick += ((ax, by) => { timer.Stop();
            NavigationService.Navigate(new Uri("/Source Code/Recieve.xaml", UriKind.RelativeOrAbsolute));
            });            
        }

你正在使用 Init 方法来初始化定时器,你可以按照我做的那样固定或动态地做。如果抛出任何错误,则检查输出中的异常,这可能是因为下一页(即主页)出现 Xaml 错误。希望对你有帮助。

以下代码对我有用..试试看

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Windows.Threading;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using ProgramOSMobile.Resources;

namespace ProgramOSMobile
{
    public partial class MainPage : PhoneApplicationPage
    {

        DispatcherTimer timer = new DispatcherTimer();
        int tick=0;
        public MainPage()
        {
            InitializeComponent();

            timer.Interval = TimeSpan.FromSeconds(0.5);
            timer.Start();
            timer.Tick += new EventHandler(splash);
        }

        private void splash(object sender, EventArgs e)
        {
            tick++;

            if(tick==5){
                NavigationService.Navigate(new Uri("/Menu.xaml", UriKind.Relative));
            }
        }


    }
}

以上代码的定时器间隔为 0.5 秒,时间节拍率为 1