static void Main(string[] args) { IActivatedEventArgs activatedArgs = AppInstance.GetActivatedEventArgs(); if (activatedArgs is ToastNotificationActivatedEventArgs) { return; } if (activatedArgs is CommandLineActivatedEventArgs CmdActivate) { if (CmdActivate.Operation.Arguments.StartsWith("RX-Explorer.exe")) { if (AppInstance.RecommendedInstance != null) { AppInstance.RecommendedInstance.RedirectActivationTo(); } else if (ApplicationData.Current.LocalSettings.Values["LastActiveGuid"] is string LastGuid && !string.IsNullOrWhiteSpace(LastGuid) && AppInstance.FindOrRegisterInstanceForKey(LastGuid) is AppInstance TargetInstance && !TargetInstance.IsCurrentInstance) { TargetInstance.RedirectActivationTo(); } else if (AppInstance.GetInstances().FirstOrDefault() is AppInstance ExistInstance) { ExistInstance.RedirectActivationTo(); } else { string InstanceId = Guid.NewGuid().ToString(); AppInstance Instance = AppInstance.FindOrRegisterInstanceForKey(InstanceId); ApplicationData.Current.LocalSettings.Values["LastActiveGuid"] = InstanceId; Application.Start((p) => new App()); } } else { string InstanceId = Guid.NewGuid().ToString(); AppInstance Instance = AppInstance.FindOrRegisterInstanceForKey(InstanceId); ApplicationData.Current.LocalSettings.Values["LastActiveGuid"] = InstanceId; Application.Start((p) => new App()); } }
static void Main(string[] args) { IActivatedEventArgs activatedArgs = AppInstance.GetActivatedEventArgs(); if (AppInstance.GetInstances().Count == 0) { AppInstanceIdContainer.ClearAll(); } switch (activatedArgs) { case ToastNotificationActivatedEventArgs ToastActivate: { switch (ToastActivate.Argument) { case "EnterBackgroundTips": { if (AppInstance.RecommendedInstance != null) { AppInstance.RecommendedInstance.RedirectActivationTo(); } else if (!string.IsNullOrWhiteSpace(AppInstanceIdContainer.LastActiveId)) { do { if (AppInstance.GetInstances().Any((Ins) => Ins.Key == AppInstanceIdContainer.LastActiveId)) { if (AppInstance.FindOrRegisterInstanceForKey(AppInstanceIdContainer.LastActiveId) is AppInstance TargetInstance) { TargetInstance.RedirectActivationTo(); } break; } else { AppInstanceIdContainer.UngisterId(AppInstanceIdContainer.LastActiveId); } }while (!string.IsNullOrEmpty(AppInstanceIdContainer.LastActiveId)); } break; } case "Restart": { string InstanceId = Guid.NewGuid().ToString(); AppInstance Instance = AppInstance.FindOrRegisterInstanceForKey(InstanceId); AppInstanceIdContainer.RegisterId(InstanceId); Application.Start((p) => new App()); break; } } break; } case CommandLineActivatedEventArgs CmdActivate: { if (CmdActivate.Operation.Arguments.StartsWith("RX-Explorer.exe", StringComparison.OrdinalIgnoreCase)) { if (AppInstance.RecommendedInstance != null) { AppInstance.RecommendedInstance.RedirectActivationTo(); } else if (!string.IsNullOrWhiteSpace(AppInstanceIdContainer.LastActiveId)) { do { if (AppInstance.GetInstances().Any((Ins) => Ins.Key == AppInstanceIdContainer.LastActiveId)) { if (AppInstance.FindOrRegisterInstanceForKey(AppInstanceIdContainer.LastActiveId) is AppInstance TargetInstance) { TargetInstance.RedirectActivationTo(); } break; } else { AppInstanceIdContainer.UngisterId(AppInstanceIdContainer.LastActiveId); } }while (!string.IsNullOrEmpty(AppInstanceIdContainer.LastActiveId)); } else { string InstanceId = Guid.NewGuid().ToString(); AppInstance Instance = AppInstance.FindOrRegisterInstanceForKey(InstanceId); AppInstanceIdContainer.RegisterId(InstanceId); Application.Start((p) => new App()); } } else { if (!ApplicationData.Current.LocalSettings.Values.ContainsKey("AlwaysStartNew")) { ApplicationData.Current.LocalSettings.Values["AlwaysStartNew"] = true; } bool AlwaysStartNew = Convert.ToBoolean(ApplicationData.Current.LocalSettings.Values["AlwaysStartNew"]); if (AlwaysStartNew) { string InstanceId = Guid.NewGuid().ToString(); AppInstance Instance = AppInstance.FindOrRegisterInstanceForKey(InstanceId); AppInstanceIdContainer.RegisterId(InstanceId); Application.Start((p) => new App()); } else { if (!string.IsNullOrWhiteSpace(AppInstanceIdContainer.LastActiveId)) { do { if (AppInstance.GetInstances().Any((Ins) => Ins.Key == AppInstanceIdContainer.LastActiveId)) { if (AppInstance.FindOrRegisterInstanceForKey(AppInstanceIdContainer.LastActiveId) is AppInstance TargetInstance) { TargetInstance.RedirectActivationTo(); } break; } else { AppInstanceIdContainer.UngisterId(AppInstanceIdContainer.LastActiveId); } }while (!string.IsNullOrEmpty(AppInstanceIdContainer.LastActiveId)); } else { string InstanceId = Guid.NewGuid().ToString(); AppInstance Instance = AppInstance.FindOrRegisterInstanceForKey(InstanceId); AppInstanceIdContainer.RegisterId(InstanceId); Application.Start((p) => new App()); } } } break; } case LaunchActivatedEventArgs LaunchArg: { if (!ApplicationData.Current.LocalSettings.Values.ContainsKey("AlwaysStartNew")) { ApplicationData.Current.LocalSettings.Values["AlwaysStartNew"] = true; } bool AlwaysStartNew = Convert.ToBoolean(ApplicationData.Current.LocalSettings.Values["AlwaysStartNew"]); if (AlwaysStartNew) { string InstanceId = Guid.NewGuid().ToString(); AppInstance Instance = AppInstance.FindOrRegisterInstanceForKey(InstanceId); AppInstanceIdContainer.RegisterId(InstanceId); Application.Start((p) => new App()); } else { if (!string.IsNullOrWhiteSpace(AppInstanceIdContainer.LastActiveId)) { do { if (AppInstance.GetInstances().Any((Ins) => Ins.Key == AppInstanceIdContainer.LastActiveId)) { if (AppInstance.FindOrRegisterInstanceForKey(AppInstanceIdContainer.LastActiveId) is AppInstance TargetInstance) { TargetInstance.RedirectActivationTo(); } break; } else { AppInstanceIdContainer.UngisterId(AppInstanceIdContainer.LastActiveId); } }while (!string.IsNullOrEmpty(AppInstanceIdContainer.LastActiveId)); } else { string InstanceId = Guid.NewGuid().ToString(); AppInstance Instance = AppInstance.FindOrRegisterInstanceForKey(InstanceId); AppInstanceIdContainer.RegisterId(InstanceId); Application.Start((p) => new App()); } } break; } case FileActivatedEventArgs _: { if (!string.IsNullOrWhiteSpace(AppInstanceIdContainer.LastActiveId)) { do { if (AppInstance.GetInstances().Any((Ins) => Ins.Key == AppInstanceIdContainer.LastActiveId)) { if (AppInstance.FindOrRegisterInstanceForKey(AppInstanceIdContainer.LastActiveId) is AppInstance TargetInstance) { TargetInstance.RedirectActivationTo(); } break; } else { AppInstanceIdContainer.UngisterId(AppInstanceIdContainer.LastActiveId); } }while (!string.IsNullOrEmpty(AppInstanceIdContainer.LastActiveId)); } else { string InstanceId = Guid.NewGuid().ToString(); AppInstance Instance = AppInstance.FindOrRegisterInstanceForKey(InstanceId); AppInstanceIdContainer.RegisterId(InstanceId); Application.Start((p) => new App()); } break; } default: { string InstanceId = Guid.NewGuid().ToString(); AppInstance Instance = AppInstance.FindOrRegisterInstanceForKey(InstanceId); AppInstanceIdContainer.RegisterId(InstanceId); Application.Start((p) => new App()); break; } } }