-
Notifications
You must be signed in to change notification settings - Fork 232
/
StartProgram.cs
72 lines (62 loc) · 2.36 KB
/
StartProgram.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using Microsoft.UI.Dispatching;
using Microsoft.Windows.AppLifecycle;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.ApplicationModel.Activation;
namespace TestOAuthCSharp
{
public static class StartProgram
{
private static DispatcherQueue dispatcherQueue;
[STAThread]
static void Main(string[] args)
{
WinRT.ComWrappersSupport.InitializeComWrappers();
AppInstance.GetCurrent().Activated += OnActivated;
bool isRedirect = DecideRedirection().GetAwaiter().GetResult();
if (!isRedirect)
{
Microsoft.UI.Xaml.Application.Start((p) =>
{
dispatcherQueue = DispatcherQueue.GetForCurrentThread();
var context = new DispatcherQueueSynchronizationContext(dispatcherQueue);
SynchronizationContext.SetSynchronizationContext(context);
new App();
});
}
}
private static async Task<bool> DecideRedirection()
{
var mainInstance = Microsoft.Windows.AppLifecycle.AppInstance.FindOrRegisterForKey("main");
var activatedEventArgs = Microsoft.Windows.AppLifecycle.AppInstance.GetCurrent().GetActivatedEventArgs();
if (!mainInstance.IsCurrent)
{
// Redirect the activation (and args) to the "main" instance, and exit.
await mainInstance.RedirectActivationToAsync(activatedEventArgs);
return true;
}
return false;
}
// Handle Redirection
public static void OnActivated(object sender, AppActivationArguments args)
{
if (args.Kind == ExtendedActivationKind.Protocol)
{
var protocolArgs = (ProtocolActivatedEventArgs)args.Data;
dispatcherQueue.TryEnqueue(() =>
{
App.Window.OnUriCallback(protocolArgs.Uri);
SetForegroundWindow(App.WindowHandle);
});
}
}
// P/Invoke declaration for SetForegroundWindow
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
}
}