从xaml

时间:2018-12-14 15:34:14

标签: xamarin.forms navigation prism

我正在尝试从xaml文件中加载页面,然后将其推入棱镜的导航堆栈。为此,我在NavigationService中添加了以下方法:

public class MyNavigationService : PageNavigationService, IMyNavigationService
{
    public async Task<INavigationResult> NavigateAsync(Page page, INavigationParameters parameters, bool? useModalNavigation, bool animated)
    {
        var result = new NavigationResult();
        try
        {
            NavigationSource = PageNavigationSource.NavigationService;

            await ProcessNavigation(GetCurrentPage(), page, parameters, useModalNavigation, animated);
            result.Success = true;
            return result;
        }
        catch (Exception ex)
        {
            _logger.Log(ex.ToString(), Category.Exception, Priority.High);
            result.Exception = ex;
            return result;
        }
        finally
        {
            NavigationSource = PageNavigationSource.Device;
        }
    }

    protected virtual async Task ProcessNavigation(Page currentPage, Page nextPage, INavigationParameters parameters, bool? useModalNavigation, bool animated)
    {
        if (nextPage == null)
            return;

        if (currentPage == null)
        {
            throw new NotImplementedException();
            //await ProcessNavigationForRootPage(nextPage, parameters, useModalNavigation, animated);
            return;
        }

        if (currentPage is ContentPage)
        {
            await ProcessNavigationForContentPage(currentPage, nextPage, parameters, useModalNavigation, animated);
        }
        else if (currentPage is NavigationPage)
        {
            //await ProcessNavigationForNavigationPage((NavigationPage)currentPage, nextPage, parameters, useModalNavigation, animated);
            throw new NotImplementedException();
        }
        else if (currentPage is TabbedPage)
        {
            //await ProcessNavigationForTabbedPage((TabbedPage)currentPage, nextPage, parameters, useModalNavigation, animated);
            throw new NotImplementedException();
        }
        else if (currentPage is CarouselPage)
        {
            //await ProcessNavigationForCarouselPage((CarouselPage)currentPage, nextPage, parameters, useModalNavigation, animated);
            throw new NotImplementedException();
        }
        else if (currentPage is MasterDetailPage)
        {
            //await ProcessNavigationForMasterDetailPage((MasterDetailPage)currentPage, nextPage, parameters, useModalNavigation, animated);
            throw new NotImplementedException();
        }
    }

    /// <summary>
    ///     Processes a navigation for a content page.
    /// </summary>
    /// <param name="currentPage">The current page.</param>
    /// <param name="nextPage">The page to navigate to.</param>
    /// <param name="parameters">Navigation parameters</param>
    /// <param name="useModalNavigation">If <c>true</c> uses PopModalAsync, if <c>false</c> uses PopAsync.</param>
    /// <param name="animated">If <c>true</c> the transition is animated, if <c>false</c> there is no animation on transition.</param>
    /// <returns>Navigation task</returns>
    protected virtual async Task ProcessNavigationForContentPage(Page currentPage, Page nextPage, INavigationParameters parameters, bool? useModalNavigation, bool animated)
    {
        await ProcessNavigation(nextPage, new Queue<string>(), parameters, useModalNavigation, animated);

        await DoNavigateAction(currentPage, string.Empty, nextPage, parameters, async () =>
        {
            await DoPush(currentPage, nextPage, useModalNavigation, animated);
        });
    }
}

此页面的导航有效并正确显示。 但是,当我尝试进一步导航(通过navigationService.NavigateAsync("ViewName");时,即使NavigationResult.Success是正确的,它也不起作用。页面“ ViewName”是已注册的ContentPage(通过container.Register< ViewName, ViewNameViewModel >()注册)。

如上所述,我认为我的页面导航缺少进一步导航的重要内容。

有人做了类似的事情可以帮助我吗?

编辑: 我注意到,如果我想离开已加载xaml的页面,则以某种方式GetCurrentPage()和局部字段_page返回的页面将所有内容都设置为null,而不是我从xaml加载的页面。

0 个答案:

没有答案