如何用shell打开辅助窗口?

时间:2017-12-08 16:06:52

标签: c# uwp template10

我尝试将在Template10 on GitHub中实现shell的指令调整到辅助窗口中的shell,但它无效。

此代码:

await DispatcherWrapper.Current().DispatchAsync(async () =>
        {
                //The next line gets the exception
                var control = await BootStrapper.Current.NavigationService.OpenAsync(
                                                    typeof(MySecondaryShell), null, "My Secondary Function");
                control.Released += Control_Released;
                BootStrapper.Current.NavigationService.Navigate(typeof(MySecondaryPage));
    });

获得此异常:

  

E VUI 1808 16:12:27.203 D:\ SVN_Trunk \ Source \ Uwp \ Gui \ UwpMain \ ViewModels \ MyPrimaryShellViewModel.cs.275.MyFunction       System.NullReferenceException:未将对象引用设置为对象的实例。       在Uwp.Main.UwpMain_XamlTypeInfo.XamlUserType.ActivateInstance()       在Windows.UI.Xaml.Controls.Frame.Navigate(类型sourcePageType,Object参数,NavigationTransitionInfo infoOverride)       在Template10.Services.NavigationService.FrameFacade.Navigate(类型页面,对象参数,NavigationTransitionInfo infoOverride)       在Template10.Services.NavigationService.NavigationService.d__34.MoveNext()       ---从抛出异常的先前位置开始的堆栈跟踪结束---       在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)       在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)       在Template10.Services.NavigationService.NavigationService.Navigate(类型页面,对象参数,NavigationTransitionInfo infoOverride)       在Template10.Services.ViewService.ViewService。<> c__DisplayClass1_0。< b__0> d.MoveNext()       ---从抛出异常的先前位置开始的堆栈跟踪结束---       在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)       在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)       在Template10.Services.ViewService.ViewService.d__1.MoveNext()       ---从抛出异常的先前位置开始的堆栈跟踪结束---       在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)       在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)       在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()       在Uwp.Main.ViewModels.MyPrimaryShellViewModel。<> c__DisplayClass63_0。< b__0> d.MoveNext()

MySecondaryShell构造如下:

public static HamburgerMenu HamburgerMenu => Instance.EmulatorHamburgerMenu;

    public MySecondaryShell(INavigationService navigationService)
    {
        this.InitializeComponent();
        HamburgerMenu.NavigationService = navigationService;
    }

当我用shell打开主窗口时,我创建了shell对象,然后将NavigationService分配给它。

但是当我打开一个辅助窗口时,我只是使用typeof(MySecondaryShell)作为参数调用NavigationService.OpenAsync。是不是在shell中没有正确设置NavigationService的问题? (通过阅读my last question中的Template10代码,我无法看到NavigationService的设置位置。)

我应该如何打开shell作为辅助窗口?

1 个答案:

答案 0 :(得分:1)

  

我应该如何打开shell作为辅助窗口?

问题是您尚未将navigationService传递给MySecondaryShell。然后MySecondaryShell将在没有navigationService的情况下初始化失败。你可以让你的shell如下。

public sealed partial class MyShell : Page
{
    public static MyShell Instance { get; set; }

    public static HamburgerMenu HamburgerMenu => Instance.MyHamburgerMenu;

    Services.SettingsServices.SettingsService _settings;
    public MyShell()
    {
        Instance = this;
        this.InitializeComponent();
        _settings = Services.SettingsServices.SettingsService.Instance;
        var service = BootStrapper.Current.NavigationServiceFactory(BootStrapper.BackButton.Attach, BootStrapper.ExistingContent.Exclude);          
        SetNavigationService(service);

    }  
    public void SetNavigationService(INavigationService navigationService)
    {
        MyHamburgerMenu.NavigationService = navigationService;
        HamburgerMenu.RefreshStyles(_settings.AppTheme, true);
        HamburgerMenu.IsFullScreen = _settings.IsFullScreen;
        HamburgerMenu.HamburgerButtonVisibility = _settings.ShowHamburgerButton ? Visibility.Visible : Visibility.Collapsed;
    }
}

<强>用法

await DispatcherWrapper.Current().DispatchAsync(async () =>
{
    var control = await BootStrapper.Current.NavigationService.OpenAsync(typeof(Views.MyShell), null, Guid.NewGuid().ToString());
    await control.CoreDispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        Views.MyShell.HamburgerMenu.NavigationService.Navigate(typeof(Views.TestPage));
    });

});