问题描述
我有一个 Xamarin.Forms
应用程序,适用于 iOS、Android 和
,现在希望是 Mac.我对 UI 进行了所有调整,以便在 Mac 上看起来很棒.在被拒绝的地方提交以供批准,因为用户可以在应用程序和菜单栏仍在运行时关闭窗口.所以我想我只需要添加一个确认弹出窗口,询问他们在尝试关闭窗口时是否要退出应用程序.
I have a Xamarin.Forms
application for iOS, Android,
and now hopefully Mac. I made all the adjustments for the UI to look great on Mac. Submitted it for approval where it was rejected because the user can close the window while the app and menu bar is still running. So I figure I would just add a confirmation pop-up asking if they want to exit the app when they try to close the window.
- OK = 终止应用程序.
- 取消 = 保持窗口打开.
我找到了很多关于如何使用 Xamarin.Mac
应用程序处理此问题的文章,但没有关于如何在 Mac 上处理 Xamarin.Forms
的文章.FormsApplicationDelegate
不会为了覆盖 WindowShouldClose
方法而授予对视图控制器或窗口代理的访问权限.我发现我可以使用 NSAlert
来做效果很好的弹出窗口.现在我找不到关于用户响应时该怎么做的任何信息.接受建议.
I find lots of articles on how to handle this with a Xamarin.Mac
app, but nothing on how to handle Xamarin.Forms
on Mac. The FormsApplicationDelegate
does not give access to the View Controller or the Window Delegate in order to override the WindowShouldClose
method. I found that I can use NSAlert
to do the pop-up which works great. Now I cannot find anything on what to do when the user responds. Open to suggestions.
private void Window_WillClose(object sender, System.EventArgs e)
{
NSNotification senderNotification = ((NSNotification)sender);
NSWindow closingWindow = (NSWindow)senderNotification.Object;
var confirmation = new NSAlert()
{
AlertStyle = NSAlertStyle.Warning,
InformativeText = "Are you sure you want to exit the App?",
MessageText = "Exit?"
};
confirmation.AddButton("OK");
confirmation.AddButton("Cancel");
var result = confirmation.RunModal();
if (result == 1001)
{
//Cancel closing the window
}
else
{
//terminate the app
}
}
推荐答案
经过大量试验,我确实找到了解决方案.这是正式通过Apple审查的内容.它要求将 n 个菜单操作链接为新窗口".它会跟踪打开的窗口,当只剩下一个窗口时,它会提示关闭应用程序.如果用户关闭所有窗口并保持应用运行,他们可以选择在菜单中打开一个新窗口.
After a lot of experimenting, I did find a solution. Here is what officially passed Apple's review. It requires that n menu action is linked as "New Window". It keeps tracks of the open windows and when there is only one left, it prompts to close the app. If the user closes all the windows and keeps the app running, they have the option to open a new window in the menu.
[Register("AppDelegate")]
public class AppDelegate : FormsApplicationDelegate
{
public NSWindow window;
private bool closeApp;
private List<NSWindow> openWindows;
public override NSWindow MainWindow
{
get { return window; }
}
public AppDelegate()
{
this.closeApp = false;
this.openWindows = new List<NSWindow>();
createNewWindow();
}
[Action("newWindow:")]
public void newWindow(NSObject sender)
{
createNewWindow();
this.window.MakeKeyAndOrderFront(sender);
LoadApplication(new App());
base.DidFinishLaunching(null);
}
private void createNewWindow()
{
var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;
var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
window = new MainWindow(rect, style, NSBackingStore.Buffered, false);
window.Title = "MyApp"; // choose your own Title here
window.TitleVisibility = NSWindowTitleVisibility.Hidden;
window.WillClose += Window_WillClose;
openWindows.Add(window);
}
private void Window_WillClose(object sender, System.EventArgs e)
{
openWindows.Remove((NSWindow)((NSNotification)sender).Object);
if (openWindows.Count == 0)
{
var confirmation = new NSAlert()
{
AlertStyle = NSAlertStyle.Warning,
InformativeText = "Do you want to exit the app?",
MessageText = "Exit?"
};
confirmation.AddButton("Yes");
confirmation.AddButton("No");
var result = confirmation.RunModal();
if (result == 1001)
{
this.closeApp = false;
}
else
{
//terminate the app
this.closeApp = true;
}
}
}
public override bool ApplicationShouldTerminateAfterLastWindowClosed(NSApplication sender)
{
return closeApp;
}
public override void DidFinishLaunching(NSNotification notification)
{
Forms.Init();
LoadApplication(new App());
base.DidFinishLaunching(notification);
}
}
这篇关于如何使用 Xamarin Forms mac 应用程序进行关闭确认?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!