【问题标题】:How to open a secondary window with a shell?如何用外壳打开辅助窗口?
【发布时间】:2017-12-08 16:06:52
【问题描述】:

我尝试将在 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,对象参数,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.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.d.MoveNext()

MySecondaryShell 的构造如下:

public static HamburgerMenu HamburgerMenu => Instance.EmulatorHamburgerMenu;

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

当我打开带有外壳的主窗口时,我创建外壳对象,然后将 NavigationService 分配给它。

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

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

【问题讨论】:

  • 你有什么更新吗?
  • @NicoZhu-MSFT 嗨,很抱歉 - 谢谢你的精彩回答!

标签: c# uwp template10


【解决方案1】:

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

问题是你没有将navigationService 传递给MySecondaryShell。然后MySecondaryShell 将在没有导航服务的情况下初始化失败。你可以让你的 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));
    });

});

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 2022-08-22
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多