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

提供: MonoBook
ナビゲーションに移動 検索に移動
(ページの作成:「ソフトウェアのアップデートなどアプリ自身を再起動したいときがある。 == 実装 == AppDelegateクラスのWillTerminateメソッドに再…」)
 
5行目: 5行目:
 
この状態でRestartRequestedフラグにtrueを設定してアプリを終了すれば再起動になる。
 
この状態でRestartRequestedフラグにtrueを設定してアプリを終了すれば再起動になる。
 
<source lang="csharp">
 
<source lang="csharp">
public override void WillTerminate (NSNotification notification)
+
[Register("AppDelegate")]
 +
public class AppDelegate : NSApplicationDelegate
 
{
 
{
     if (RestartRequested)
+
     // 〜〜〜 中略 〜〜〜
 +
 
 +
    // 再起動フラグ
 +
    public bool RestartRequested = false;
 +
 
 +
    // アプリ終了時に呼ばれる
 +
    public override void WillTerminate (NSNotification notification)
 
     {
 
     {
         NSWorkspace.SharedWorkspace.LaunchApp (NSBundle.MainBundle.BundleIdentifier,
+
         // 再起動フラグ
                                                NSWorkspaceLaunchOptions.NewInstance | NSWorkspaceLaunchOptions.Async,
+
        if (RestartRequested)
                                                NSAppleEventDescriptor.NullDescriptor,
+
        {
                                                IntPtr.Zero);
+
            // 自分自身を起動
 +
            NSWorkspace.SharedWorkspace.LaunchApp (
 +
                NSBundle.MainBundle.BundleIdentifier,
 +
                NSWorkspaceLaunchOptions.NewInstance | NSWorkspaceLaunchOptions.Async,
 +
                NSAppleEventDescriptor.NullDescriptor,
 +
                IntPtr.Zero);
 +
        }
 
     }
 
     }
 
}
 
}
 +
 
</source>
 
</source>
  
 
[[category: Xamarin.Mac]]
 
[[category: Xamarin.Mac]]

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