在不始终显示主页的情况下在Template10中使用多个视图? [英] Using multiple views with Template10 without always showing the Main Page?

查看:84
本文介绍了在不始终显示主页的情况下在Template10中使用多个视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是T10的新手,正在尝试学习它. 这是 Template 10 Multiple Windows

I am new to T10 and trying to learn it. This is a follow-up to Template 10 Multiple Windows

在"regular"(表示非Template10)UWP应用中,我学会了执行以下操作(作为一个简短示例),以支持多种视图:

In 'regular' (meaning non-Template10) UWP app I've learned to do something like this (as a short example) in order to support multiple views:

public App() { InitializeComponent(); Suspending += OnSuspending; }

readonly List<CoreDispatcher> _dispatchers = new List<CoreDispatcher>();
protected override async void OnLaunched(LaunchActivatedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;
    if (rootFrame == null) 
    {
        rootFrame = new Frame();
        rootFrame.NavigationFailed += OnNavigationFailed;

        Window.Current.Content = rootFrame;
        if (rootFrame.Content == null)
        {
            rootFrame.Navigate(typeof(MainPage), e.Arguments);
        }

        Window.Current.Activate();
        _dispatchers.Add(CoreWindow.GetForCurrentThread().Dispatcher);
    }
    else 
    {
        var view = CoreApplication.CreateNewView();
        int windowId = 0;
        await view.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            windowId = ApplicationView.GetApplicationViewIdForWindow(CoreWindow.GetForCurrentThread());
            var frame = new Frame();
            frame.Navigate(typeof(MainPage), null);
            Window.Current.Content = frame;
            Window.Current.Activate();
            ApplicationView.GetForCurrentView().Consolidated += View_Consolidated;
        });

        await _dispatchers[_dispatchers.Count - 1].RunAsync
        (
            CoreDispatcherPriority.Normal, async () => { var _ = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(windowId); }
        );
        _dispatchers.Add(view.Dispatcher);
    }
}

private void View_Consolidated(ApplicationView sender, ApplicationViewConsolidatedEventArgs args)
{
    _dispatchers.Remove(CoreWindow.GetForCurrentThread().Dispatcher);
    ApplicationView.GetForCurrentView().Consolidated -= View_Consolidated;
}

现在:如何使用Template10做到这一点?我看过 https://github.com/Windows-XAML/Template10/wiki/Multiple-Views 示例,但无法弄清楚.更具体地说,我想在使用协议激活(使用Hamburger模板)时转到特定页面. 到目前为止,这是我要提出的内容:

Now: how do I do this with Template10? I've looked at the https://github.com/Windows-XAML/Template10/wiki/Multiple-Views sample but can't figure it out. More specifically, I want to go to a specific page when using protocol activation (using Hamburger template). Here's what I've come up so far :

public override async Task OnStartAsync(StartKind startKind, IActivatedEventArgs args)
{
    var protocolArgs = args as ProtocolActivatedEventArgs;
    if (protocolArgs != null && protocolArgs.Uri != null)
    {
        await NavigationService.OpenAsync(typeof(Views.DetailPage)); // protocol activation
    }
    else
    {
        await NavigationService.NavigateAsync(typeof(Views.MainPage)); // regular activation
    }
}

除了OpenAsync也显示主页(以及DetailPage)外,此方法都有效.使用上面描述的常规" UWP方法,我没有这个问题.我怎样才能使它按我的意愿工作? 我敢肯定这很简单.

This works EXCEPT that the Main page is also displayed (along with the DetailPage) with the OpenAsync. Using the 'regular' UWP approach described above I do not have the issue. How can I get this to work as I'd like? I'm sure this is something simple.

到目前为止,我喜欢T10-感谢杰里和团队的贡献.

I like T10 so far - thanks Jerry and team for the contribution.

编辑详细信息

基于以下建议,我将代码(在App.xaml.cs中)更改为:

Based on suggestions below, I changed the code (in App.xaml.cs) to:

public override async Task OnStartAsync(StartKind startKind, IActivatedEventArgs args)
{
    var protocolArgs = args as ProtocolActivatedEventArgs;
    if (protocolArgs != null)
    {
        var pageName = protocolArgs.Uri.AbsolutePath;
        if (!string.IsNullOrEmpty(pageName))
        {
            string pageId = protocolArgs.Uri.LocalPath;
            var pageQuery = protocolArgs.Uri.Query;
            // Here would navigate to the page specified by "pageId"... as an example:
            if (pageId == "foo")
                await NavigationService.OpenAsync(typeof(Views.FooPage), null, pageQuery);
            else if (pageId == "bar")
                await NavigationService.OpenAsync(typeof(Views.BarPage), null, pageQuery);
            else
                await NavigationService.NavigateAsync(typeof(Views.MainPage));
        }
    }
    else
    {
        await NavigationService.NavigateAsync(typeof(Views.MainPage));
    }

}

和:

public override UIElement CreateRootElement(IActivatedEventArgs args)
{
    var service = NavigationServiceFactory(BackButton.Attach, ExistingContent.Exclude);

    var protocolArgs = args as ProtocolActivatedEventArgs;
    var pageName = protocolArgs?.Uri.AbsolutePath;
    if (!string.IsNullOrEmpty(pageName))
    {
        return new Frame();  <<---------- WRONG?
    }

    return new ModalDialog
    {
        DisableBackButtonWhenModal = true,
        Content = new Views.Shell(service),
        ModalContent = new Views.Busy(),
    };

}

现在,我有一个空白表格,使用协议激活时也会显示该表格(不再像Sunteen所指出的那样是Shell表格),因为(我认为)上面标记为"WRONG"的行.我的理解是CreateRootElement需要执行,但是当应用程序通过协议激活时,我没有/不想显示根框架;但是CreateRootElement必须返回一些内容. 如您在我的示例中所看到的,它并不完全与MultipleViews示例相同,因为该示例始终具有根框架. 注意:此外,我认为对于T10,我不应该/不能按照Sunteen的建议使用直接导航:导航必须全部由T10处理.

Now, I have a blank form that is also displayed when using protocol activation (and no longer the Shell form as noted by Sunteen), because (I think) of the line flagged 'WRONG' above. My understanding is that CreateRootElement needs to be executed, but when the app is activated by protocol I don't have / don't want a root frame to display; but CreateRootElement must return something. As you can see by my example, it's not exactly as the MultipleViews sample because that example always have a root frame. Note: Furthermore, I think that with T10 I should not/cannot use direct navigation as suggested by Sunteen: the navigation must all be handled by T10.

谢谢.

推荐答案

此方法有效,除了OpenAsync还显示主页(以及DetailPage)

This works EXCEPT that the Main page is also displayed (along with the DetailPage) with the OpenAsync

我认为您的意思是还会显示Shell.xaml页.这是因为当前 NavigationService 属于以下框架如果没有内部Shell页面,则在通过CreateRootElement方法导航之前已经创建了Shell页面.

I think you mean the Shell.xaml page is also displayed. This is because the currently NavigationService is belonged to the frame that without Shell page inside, the Shell page already created before navigation by CreateRootElement method.

使用协议激活(使用Hamburger模板)时,我想转到特定页面

I want to go to a specific page when using protocol activation (using Hamburger template)

为满足您的要求,我建议您不要破坏项目中的导航结构,而是为协议启动的特殊情况创建一个新的框架.例如:

To meet your requirements,I'd recommend you not to break the navigation structure in your project, but create a new frame for the special scenario that launched by protocol. For example:

public override async Task OnStartAsync(StartKind startKind, IActivatedEventArgs args)
{
    // TODO: add your long-running task here    
    var protocolArgs = args as ProtocolActivatedEventArgs;
    if (protocolArgs != null && protocolArgs.Uri != null)
    {
        Frame newframe = new Frame();
        newframe.Navigate(typeof(Views.DetailPage));
        Window.Current.Content = newframe; // protocol activation
    }
    else
    {
        await NavigationService.NavigateAsync(typeof(Views.MainPage)); // regular activation
    }
}

这篇关于在不始终显示主页的情况下在Template10中使用多个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆