Xamarin.Mac 中的应用程序后台 [英] Application backgrounding in Xamarin.Mac

查看:40
本文介绍了Xamarin.Mac 中的应用程序后台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Xamarin.Mac 中实现应用程序后台.我想要一些类似于 android 服务的东西:

How do I achieve Application backgrounding in Xamarin.Mac. I want something that work similar as android services:

Android 服务 - 服务是可以在后台执行长时间运行的操作的应用程序组件,它不会提供用户界面.另一个应用程序组件可以启动一个服务,它会继续在后台运行,即使用户切换到另一个应用程序.此外,组件可以绑定到与它交互甚至执行进程间的服务通信(IPC).例如,一个服务可以处理网络交易、播放音乐、执行文件 I/O 或与内容交互提供者,全部来自后台.~Android.com

Android Services - A Service is an application component that can perform long-running operations in the background, and it does not provide a user interface. Another application component can start a service, and it continues to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service can handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background. ~Android.com

提前致谢.

推荐答案

正确的方法是将您的应用程序安装为 LaunchAgent (instance-per-user) 或 LaunchDaemonstrong>(每台机器实例)使用 launchd 和应用程序类型"设置(在Xamarin Studio"或Visual Studio for Mac"项目选项对话框中)应该设置为Executable"而不是Executable with a UI"(或类似的).

The proper way is to install your application as a LaunchAgent (instance-per-user) or a LaunchDaemon (instance-per-machine) using launchd, and the 'application type' setting (within "Xamarin Studio" or "Visual Studio for Mac" project options dialog) should be set to "Executable" instead of "Executable with a UI" (or similar.)

作为LaunchAgent",您拥有与桌面交互的选项,并且您的代码在每个登录用户的上下文中运行,而不是在系统进程帐户下运行.您不会在停靠栏"中看到一个磁贴(除非您分配了一个 NSApp),并且如果您显式调用 Mac API 来实现它,您只会看到一个状态菜单栏".此外,launchd 将定期检查并启动您的进程,确保它始终运行.)不要求您使用任何 Cocoa/Mac API 来实现代理/守护程序,您可以运行任何launchd 下的控制台应用程序,上述内容仍然有效.

As a "LaunchAgent" you have the option of interacting with the desktop, and your code runs under the context of each logged in user instead of a system process account. You will not see a tile in the 'dock bar' (unless you allocate an NSApp), and you only see a 'status menu bar' if you explicitly call a Mac API to make it happen. Additionally, launchd will periodically check for and launch your process, ensuring that it is always running.) There is no requirement that you use any Cocoa/Mac APIs to implement an agent/daemon, you could run any console app under launchd and the above would still remain true.

与launchd的集成是重要,但详细记录适用于 macOS 平台,它同样适用于 Xamarin.Mac 开发的应用程序,就像它适用于任何其他平台/工具链(Java, C++, Obj-C, Swift, ...),如果我不得不猜测,对于大多数开发者来说,学习如何使用 launchd 需要 2-4 小时的投资.

Integration with launchd is non-trivial, but well documented for the macOS platform, and it applies equally to Xamarin.Mac-developed applications as it does any other platform/tool-chain (Java, C++, Obj-C, SWift, ...), if I had to guess learning how to use launchd is a 2-4 hour investment for most devs.

因为 Xamarin.Mac 本身没有对 launchd 的明确支持(IMO 不应该),您还需要自己调用 launchctl.作为经验丰富的 .NET 开发人员,我发现将所有launchctl 丑陋"包装在安装程序类"中效果很好(然后您只需要运行 installutil 来安装/卸载您的代理/服务/应用程序.)这些安装程序类也可以实现在 Windows 和 Linux 上工作,这意味着跨平台安装只需要使用 installutil.

Because Xamarin.Mac does not itself have explicit support for launchd (and IMO should not) you will also be tasked with calling launchctl yourself. As a seasoned .NET developer I find that wrapping all of the 'launchctl ugliness' inside an "installer class" works well (then you only need to run installutil to install/uninstall your agent/service/application.) These installer classes can also be implemented to work on Windows and Linux, meaning cross-platform installs only ever require the use of installutil.

另外,您可以在应用包信息中设置特定属性.plist,但这不是必需的.然而,大多数人会发现编辑现有的 plist 比与 launchd 集成更容易.值得注意的是,一个行为差异是编辑 plist 以包含 LSBackgroundOnlyLSUIElement 不保证您的应用程序正在运行,但使用 launchctl 会.

Separately, you can set particular properties in your app bundle Info.plist, but this is not necessary. However, most will find editing the existing plist easier than integrating with launchd. It's worth noting that one behavioral difference is that editing the plist to include either LSBackgroundOnly or LSUIElement doesn't guarantee your application is running, but using launchctl will.

HTH

  • Creating Launch Daemons and Agents Article on Apple.com
  • How to Create a Background Running Cocoa Application on StackOverflow.com
  • launchd Article on Wikipedia.org
  • launchctl Manual Page on Apple.com
  • LSBackgroundOnly Reference on Apple.com
  • LSUIElement Reference on Apple.com
  • installutil Reference on Microsoft.com (applies to Mono/Xamarin Linux/Mac platforms as well, though Windows uses its own 'Service Control Manager (SCM)' APIs instead of launchctl.)

这篇关于Xamarin.Mac 中的应用程序后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆