-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathMainWindow.xaml.cs
86 lines (79 loc) · 2.48 KB
/
MainWindow.xaml.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.UI.Xaml.Controls;
using Windows.ApplicationModel.DataTransfer;
namespace Microsoft.JavaScript.NodeApi.Examples;
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Title = "C# WinUI + JS Fluid Framework Demo";
}
private void OnClosed(object sender, WindowEventArgs args)
{
// The Fluid container must be disposed to allow the Node.js thread to exit.
this.collabEditBox.CloseCollabSession();
}
private void OnSessionTextChanged(object sender, TextChangedEventArgs e)
{
string sessionId = this.sessionTextBox.Text.Trim();
if (sessionId == this.collabEditBox.SessionId)
{
this.joinButton.Content = "Copy";
}
else if (sessionId.Length > 0)
{
this.joinButton.Content = "Join";
}
else
{
this.joinButton.Content = "Start";
}
}
private async void OnJoinButtonClick(object sender, RoutedEventArgs e)
{
string sessionId = this.sessionTextBox.Text.Trim();
if (sessionId == this.collabEditBox.SessionId)
{
DataPackage clipData = new();
clipData.SetText(sessionId);
Clipboard.SetContent(clipData);
}
else if (sessionId.Length > 0)
{
try
{
await this.collabEditBox.ConnectCollabSessionAsync(sessionId);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
else
{
try
{
await this.collabEditBox.CreateCollabSessionAsync(LoadDocument());
this.sessionTextBox.Text = this.collabEditBox.SessionId;
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
}
private static string LoadDocument()
{
var resourceName = $"{typeof(MainWindow).Namespace}.README.md";
var readmeStream = typeof(MainWindow).Assembly.GetManifestResourceStream(resourceName)!;
return new StreamReader(readmeStream).ReadToEnd();
}
}