c# - Windows 8 应用程序中长时间启动后无法导航到页面

标签 c# xaml windows-runtime winrt-xaml async-await

我按照 Microsoft 提供的指南创建了扩展的启动屏幕。屏幕显示,数据加载,但应用程序无法导航到登陆页面。这怎么可能?

ExtendedSplash.xaml.cs

public sealed partial class ExtendedSplash
{

    public ExtendedSplash(SplashScreen splash)
    {
        this.InitializeComponent();

        // Position the extended splash screen image in the same location as the splash screen image.
        this.extendedSplashImage.SetValue(Canvas.LeftProperty, splash.ImageLocation.X);
        this.extendedSplashImage.SetValue(Canvas.TopProperty, splash.ImageLocation.Y);
        this.extendedSplashImage.Height = splash.ImageLocation.Height;
        this.extendedSplashImage.Width = splash.ImageLocation.Width;

        // Position the extended splash screen's progress ring.
        this.ProgressRing.SetValue(Canvas.TopProperty, splash.ImageLocation.Y + splash.ImageLocation.Height + 32);
        this.ProgressRing.SetValue(Canvas.LeftProperty,
     splash.ImageLocation.X +
             (splash.ImageLocation.Width / 2) - 15);
    }

    public void onSplashScreenDismissed(SplashScreen sender, object args)
    {

    }
}

来自App.xaml.cs

    protected override async void OnLaunched(LaunchActivatedEventArgs args)
    {
        _newsDataSource = (NewsDataSource)App.Current.Resources["newsDataSource"];
        _movieDataSource = (MovieDataSource)App.Current.Resources["moviesDataSource"];
        await PerformDataFetch();

        // extended splash screen loading
        ExtendedSplash eSplash = new ExtendedSplash(args.SplashScreen);
        args.SplashScreen.Dismissed +=
            new TypedEventHandler<SplashScreen, object>(eSplash.onSplashScreenDismissed);

        // Place the frame in the current Window
        Window.Current.Content = eSplash;
        Window.Current.Activate();
    }

    internal async Task PerformDataFetch()
    {
        // load news
        if (_newsDataSource != null)
        {
            if (!_newsDataSource.News.Any())
            {
                await _newsDataSource.GetNewsAsync();
            }
        }

        // load movies
        if (_movieDataSource != null)
        {
            if (!_movieDataSource.Movies.Any())
            {
                await _movieDataSource.GetMoviesAsync();
            }
        }

        RemoveExtendedSplash();
    }

    internal void RemoveExtendedSplash()
    {
        Window.Current.Content = new MainPage();
        Window.Current.Activate();
    }

如果在最后一个方法上放置断点,则会触发该方法,但不会转换到该页面。我喜欢 ProgressRing 的动画,但应用程序也应该做其他事情:)

最佳答案

ExtendedSplash 必须是一个页面。所以ExtendSplash.xaml.cs:

public sealed partial class ExtendedSplash : Page
{
    public ExtendedSplash()
    {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        SplashScreen splash = (SplashScreen) e.Parameter;

        // Position the extended splash screen image in the same location as the splash screen image.
        this.extendedSplashImage.SetValue(Canvas.LeftProperty, splash.ImageLocation.X);
        this.extendedSplashImage.SetValue(Canvas.TopProperty, splash.ImageLocation.Y);
        this.extendedSplashImage.Height = splash.ImageLocation.Height;
        this.extendedSplashImage.Width = splash.ImageLocation.Width;

        // Position the extended splash screen's progress ring.
        this.ProgressRing.SetValue(Canvas.TopProperty, splash.ImageLocation.Y + splash.ImageLocation.Height + 32);
        this.ProgressRing.SetValue(Canvas.LeftProperty,
     splash.ImageLocation.X +
             (splash.ImageLocation.Width / 2) - 15);
    }
}

并且所有数据获取操作都应该在App.xaml.cs中执行。完成后,只需将框架导航到登陆页面就足够了。

sealed partial class App : Application
{
    private Frame _rootFrame;

    protected override async void OnLaunched(LaunchActivatedEventArgs args)
    {
        if(_rootFrame == null)
            _rootFrame = new Frame();

        // Place the frame in the current Window
        _rootFrame.Navigate(typeof (ExtendedSplash), args.SplashScreen);
        Window.Current.Content = _rootFrame;
        Window.Current.Activate();

        await PerformDataFetch();
    }

    internal async Task PerformDataFetch()
    {
        // data loading here

        RemoveExtendedSplash();
    }

    internal void RemoveExtendedSplash()
    {
        if (_rootFrame != null) _rootFrame.Navigate(typeof (MainPage));
    }
}

关于c# - Windows 8 应用程序中长时间启动后无法导航到页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12556184/

相关文章:

c# - 在 View 中使用 expando 对象?

c# - 是否可以扩展 List<T> 但仅限于 T = 精确类型?

WPF 列表框数据绑定(bind)适用于设计器,但不适用于运行时

c# - ReportViewer不显示报告-URL问题?

c# - 在 Metro Windows 8 中将 ImageSource 转换为 WriteableBitmap

windows - 如何在 Windows 11 22H2 的应用程序中支持调用静音(通用静音)?

c# - 如何对列表进行排序,将 map 从旧位置保存到新位置?

c# - 使用 IEnumerator 对项目进行异步操作

wpf - 如何使用 XAML 绑定(bind)到 GridView 的 ColumnCollection

c# - 在 GridView 上分组项目