我试图在Xamarin表单应用程序中使用每个平台的依赖服务为我的所有屏幕添加一个浮动按钮。iOS可以很好地处理任何ContentPage但是当我设置Shell.PresentationMode="ModalAnimated">,按钮未显示或在ContentPage后面打印与动画模态。
在Xamarin Forms的所有视图中调用依赖服务
DependencyService.Get<IDeviceService>().ShowFloatingButton();
iOS的依赖服务实现
namespace WebviewSample.iOS.DependencySeivices
{
public class DeviceService : IDeviceService
{
private void ShowFloatingButton()
{
var vc = UIApplication.SharedApplication.KeyWindow.RootViewController;
var btn = UIButton.FromType(UIButtonType.System);
btn.Frame = new CGRect(20, 200, 280, 44);
btn.SetTitle("Click Me", UIControlState.Normal);
vc.View.BackgroundColor = UIColor.DarkGray;
vc.View.AddSubview(btn);
vc.View.BringSubviewToFront(btn);
}
}
}
我已经尝试获得顶部控制器如下示例:
if (UIApplication.SharedApplication.KeyWindow.WindowLevel != UIWindowLevel.Normal)
{
foreach (var item in UIApplication.SharedApplication.Windows)
{
if (item.WindowLevel == UIWindowLevel.Normal)
{
vc = item.RootViewController;
break;
}
}
}
也 while (vc.PresentedViewController != null)
{
vc = vc.PresentedViewController;
}
根据本文档:https://devblogs.microsoft.com/xamarin/xamarin-forms-shell-quick-tip-modal-navigation/
Xamarin shell导航与传统的导航不同,在传统的导航中,一个页面被推送到堆栈上,所以一个新的页面会在当前页面上弹出。