我怎样才能让 UIAlertController 出现在任何东西的顶部?
How can I make UIAlertController appear over the top of anything and everything?
我正在开发一个使用 Visual Studio 2022 的应用程序,它使用 ZXing.Net.Mobile,Forms
扫描条形码,一切都按预期工作,除了我无法在所有内容的顶部显示 UIAlertController 消息。我的代码是:
btnScan.TouchUpInside += (object sender, EventArgs e) =>
{
// First we must get permission to use the camera
AVAuthorizationStatus authStatus = AVCaptureDevice.GetAuthorizationStatus(AVAuthorizationMediaType.Video);
if (authStatus == AVAuthorizationStatus.Authorized)
{
scan();
}
else if (authStatus == AVAuthorizationStatus.NotDetermined)
{
Utils.ShowMessage(this,"Camera access not determined. Ask for permission.", "Error");
AVCaptureDevice.RequestAccessForMediaType(AVAuthorizationMediaType.Video, (granted) =>
{
if (granted)
{
scan();
}
else
{
camDenied();
}
});
}
else if (authStatus == AVAuthorizationStatus.Restricted)
{
Utils.ShowMessage(this, "This device doesn't have the camera feature", "Error");
}
else
{
// Denied
camDenied();
}
};
}
void HandleScanResult(ZXing.Result result)
{
var msg = "";
if (result != null && !string.IsNullOrEmpty(result.Text))
msg = "Found Barcode: " + result.Text;
else
msg = "Scanning Canceled!";
// I need this alert to appear over the top of everything
InvokeOnMainThread(() =>
{
Utils.ShowMessage(this, msg, "Scan Success");
});
}
void camDenied()
{
UIAlertController alert = UIAlertController.Create("Warning", "It looks like your privacy settings are preventing us from accessing your camera. You can fix this by doing the following...", UIAlertControllerStyle.Alert);
UIAlertAction goAction = UIAlertAction.Create("Go", UIAlertActionStyle.Default, (action) =>
{
// Open Settings
UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));
});
alert.AddAction(goAction);
PresentViewController(alert, true, null);
}
void scan ()
{
InvokeOnMainThread(() => {
scanner = new MobileBarcodeScanner(this.NavigationController);
scanner.UseCustomOverlay = false;
var opt = new MobileBarcodeScanningOptions() { DelayBetweenContinuousScans = 3000 };
scanner.TopText = "Hold camera up to barcode to scan";
scanner.BottomText = "Barcode will automatically scan";
//Start scanning
scanner.ScanContinuously(opt, false, HandleScanResult);
});
}
UIAlertController
代码执行没有错误,但没有出现警告。有没有办法强制警报显示在所有内容的顶部?
我想通了。我改变了我的 Utils.ShowMessage
函数如下:
public static void ShowMessage(UIViewController myview, string message, string messagetype)
{
var window = UIApplication.SharedApplication.KeyWindow;
var vc = window.RootViewController;
while (vc.PresentedViewController != null)
{
vc = vc.PresentedViewController;
}
var okAlertController = UIAlertController.Create(message, messagetype, UIAlertControllerStyle.Alert);
okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
vc.PresentViewController(okAlertController, true, null);
}
需要获取根视图控制器并从中显示警报。
我正在开发一个使用 Visual Studio 2022 的应用程序,它使用 ZXing.Net.Mobile,Forms
扫描条形码,一切都按预期工作,除了我无法在所有内容的顶部显示 UIAlertController 消息。我的代码是:
btnScan.TouchUpInside += (object sender, EventArgs e) =>
{
// First we must get permission to use the camera
AVAuthorizationStatus authStatus = AVCaptureDevice.GetAuthorizationStatus(AVAuthorizationMediaType.Video);
if (authStatus == AVAuthorizationStatus.Authorized)
{
scan();
}
else if (authStatus == AVAuthorizationStatus.NotDetermined)
{
Utils.ShowMessage(this,"Camera access not determined. Ask for permission.", "Error");
AVCaptureDevice.RequestAccessForMediaType(AVAuthorizationMediaType.Video, (granted) =>
{
if (granted)
{
scan();
}
else
{
camDenied();
}
});
}
else if (authStatus == AVAuthorizationStatus.Restricted)
{
Utils.ShowMessage(this, "This device doesn't have the camera feature", "Error");
}
else
{
// Denied
camDenied();
}
};
}
void HandleScanResult(ZXing.Result result)
{
var msg = "";
if (result != null && !string.IsNullOrEmpty(result.Text))
msg = "Found Barcode: " + result.Text;
else
msg = "Scanning Canceled!";
// I need this alert to appear over the top of everything
InvokeOnMainThread(() =>
{
Utils.ShowMessage(this, msg, "Scan Success");
});
}
void camDenied()
{
UIAlertController alert = UIAlertController.Create("Warning", "It looks like your privacy settings are preventing us from accessing your camera. You can fix this by doing the following...", UIAlertControllerStyle.Alert);
UIAlertAction goAction = UIAlertAction.Create("Go", UIAlertActionStyle.Default, (action) =>
{
// Open Settings
UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));
});
alert.AddAction(goAction);
PresentViewController(alert, true, null);
}
void scan ()
{
InvokeOnMainThread(() => {
scanner = new MobileBarcodeScanner(this.NavigationController);
scanner.UseCustomOverlay = false;
var opt = new MobileBarcodeScanningOptions() { DelayBetweenContinuousScans = 3000 };
scanner.TopText = "Hold camera up to barcode to scan";
scanner.BottomText = "Barcode will automatically scan";
//Start scanning
scanner.ScanContinuously(opt, false, HandleScanResult);
});
}
UIAlertController
代码执行没有错误,但没有出现警告。有没有办法强制警报显示在所有内容的顶部?
我想通了。我改变了我的 Utils.ShowMessage
函数如下:
public static void ShowMessage(UIViewController myview, string message, string messagetype)
{
var window = UIApplication.SharedApplication.KeyWindow;
var vc = window.RootViewController;
while (vc.PresentedViewController != null)
{
vc = vc.PresentedViewController;
}
var okAlertController = UIAlertController.Create(message, messagetype, UIAlertControllerStyle.Alert);
okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
vc.PresentViewController(okAlertController, true, null);
}
需要获取根视图控制器并从中显示警报。