【问题标题】:Xamarin Navigation with Modal page带有模态页面的 Xamarin 导航
【发布时间】:2017-12-18 15:01:08
【问题描述】:

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

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

调用是在另一个视图模型中完成的,它显示了模态页面。一切完成后,我必须去根页面。我打电话

await _navigation.PopToRootAsync(false);

然后

await _navigation.PopModalAsync();

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

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

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

【问题讨论】:

  • 设置动画参数为true时是否也会闪烁?
  • 是的,同样的闪烁

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


【解决方案1】:

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

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)

【讨论】:

    【解决方案2】:

    使用绝对布局!

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多