Xamarin导航与模态页面

时间:2017-12-18 15:01:09

标签: c# xamarin xamarin.forms xamarin.ios

我需要在我的应用程序中打开模态窗口,为此我们使用这样的方法:

 public async Task OpenModal<T>(object parameter = null)
     where T : BaseViewModel
 {
     var modal = _pageService.CreatePageFor<T>(parameter);
     await _navigation.PushModalAsync(modal, false);
 }

调用在另一个viewmodel中完成,该模型显示模态页面。一切都完成后,我必须转到根页面。我打电话给

await _navigation.PopToRootAsync(false);

之后

await _navigation.PopModalAsync();

问题是PopToRootAsync永远不会完成,并且iOS的呼叫等待永远(不适用于Android)。如果我更改PopToRootAsyncPopModalAsync的顺序,那么我会闪烁,这在我们的案例中是不可接受的。

我读到了这个: https://forums.xamarin.com/discussion/22156/poptorootasync-with-modal

但仍无法找到解决方案,有什么建议吗?

2 个答案:

答案 0 :(得分:0)

如果你想确保一个平滑的过渡动画我的建议是手动删除导航堆栈中的所有页面,但根目录然后继续弹出你的模态。下面是一些示例代码,可以让您了解可能需要添加到导航服务实现中的内容。

var navigationStack = MainPage.Navigation.NavigationStack.ToList();
var rootPage = navigationStack.FirstOrDefault();

for (int i = navigationStack.Count - 1; i >= 0; i--)
{
    var page = navigationStack[i];
    if (page.GetType() == rootPage.GetType())
    {
        return;
    }
    else
    {
        Navigation.RemovePage(page);
    }
}

Navigation.PopModalAsync(true)

答案 1 :(得分:0)

使用绝对布局!

<AbsoluteLayout  xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="className">
    <!-- Transparent Background -->
    <StackLayout AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
                 AbsoluteLayout.LayoutFlags="All"
                 BackgroundColor="Gray"
                 Opacity="0.5"
                 >       
    </StackLayout>
    <!-- Content -->
    <ContentView x:Name="Overlay"
                 AbsoluteLayout.LayoutBounds="0.5, 0.5, 0.5, 0.5"
                 AbsoluteLayout.LayoutFlags="All"
                 BackgroundColor="Transparent"
                 Padding="10, 0">
    <Label Text="myLabel">
        <!-- Overlay -->
    </ContentView>
</AbsoluteLayout>
相关问题