Xamarin.Macでアプリ自身を再起動する
ソフトウェアのアップデートなどアプリ自身を再起動したいときがある。
実装
AppDelegateクラスのWillTerminateメソッドに再起動する処理を記述する。 この状態でRestartRequestedフラグにtrueを設定してアプリを終了すれば再起動になる。
[Register("AppDelegate")]
public class AppDelegate : NSApplicationDelegate
{
// 〜〜〜 中略 〜〜〜
// 再起動フラグ
public bool RestartRequested = false;
// アプリ終了時に呼ばれる
public override void WillTerminate (NSNotification notification)
{
// 再起動フラグ
if (RestartRequested)
{
// 自分自身を起動
NSWorkspace.SharedWorkspace.LaunchApp (
NSBundle.MainBundle.BundleIdentifier,
NSWorkspaceLaunchOptions.NewInstance | NSWorkspaceLaunchOptions.Async,
NSAppleEventDescriptor.NullDescriptor,
IntPtr.Zero);
}
}
}