「Xamarin.Macでアプリ自身を再起動する」の版間の差分

提供:MonoBook
編集の要約なし
1行目: 1行目:
ソフトウェアのアップデートなどアプリ自身を再起動したいときがある。
[[ソフトウェア]]のアップデートなど[[アプリ]]自身を再起動したいときがある。


== 実装 ==
== 実装 ==

2020年3月5日 (木) 03:17時点における版

ソフトウェアのアップデートなどアプリ自身を再起動したいときがある。

実装

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);
        }
    }
}