Codemaru 1.0.0.0
QR 코드, 모바일 랜딩 페이지, vCard 연락처 저장과 명함 디자인을 한 화면에서 제공하는 디지털 명함 서비스입니다.
로딩중...
검색중...
일치하는것 없음
MainWindow.xaml.cs
이 파일의 문서화 페이지로 가기
1using Dreamine.Hybrid.Wpf.Controls;
2using Dreamine.Hybrid.Wpf.Hosting;
3using Microsoft.Web.WebView2.Wpf;
4using System.ComponentModel;
5using System.Diagnostics;
6using System.Net.Http;
7using System.Windows;
8
9namespace Codemaru.Views;
10
19public partial class MainWindow : Window
20{
29 private readonly DreamineBlazorServerHostOptions _serverOptions;
30
55 public MainWindow(DreamineBlazorServerHostOptions serverOptions)
56 {
57 _serverOptions = serverOptions ?? throw new ArgumentNullException(nameof(serverOptions));
58
59 InitializeComponent();
60
61 if (DesignerProperties.GetIsInDesignMode(this))
62 {
63 return;
64 }
65
66 Loaded += OnLoaded;
67 }
68
93 private async void OnLoaded(object sender, RoutedEventArgs e)
94 {
95 var hybrid = new HybridHostControl
96 {
97 HostPage = "wwwroot/index.html",
99 RootComponentType = typeof(Codemaru.Blazor.Pages.Index),
100 RootSelector = "#app"
101 };
102 EmbeddedTab.Content = hybrid;
103
104 var webView = HybridWebViewHost.CreateWebView();
105 ServerTab.Content = webView;
106
107 await NavigateServerAsync(webView, $"http://localhost:{_serverOptions.Port}", _serverOptions.InstanceId);
108 }
109
166 private static async Task NavigateServerAsync(WebView2 webView, string baseUrl, string expectedInstanceId,
167 int timeoutMs = 15000, int intervalMs = 500)
168 {
169 var cardHybridUrl = $"{baseUrl}/cardhybrid";
170 var instanceUrl = $"{baseUrl}/_dreamine/instance";
171 try
172 {
173 await Task.Delay(1200);
174
175 using var cts = new CancellationTokenSource(timeoutMs);
176 using var http = new HttpClient();
177 var alive = false;
178
179 while (!cts.IsCancellationRequested)
180 {
181 try
182 {
183 var instanceId = await http.GetStringAsync(instanceUrl, cts.Token);
184 if (string.Equals(instanceId.Trim(), expectedInstanceId, StringComparison.Ordinal))
185 {
186 alive = true;
187 break;
188 }
189 }
190 catch (HttpRequestException)
191 {
192 }
193 catch (TaskCanceledException)
194 {
195 }
196
197 await Task.Delay(intervalMs, cts.Token);
198 }
199
200 if (!alive)
201 {
202 Debug.WriteLine($"[CardHybrid] Server instance mismatch or timeout: {baseUrl}");
203 await HybridWebViewHost.ShowOfflineMessageAsync(webView, cardHybridUrl);
204 return;
205 }
206
207 webView.Source = new Uri(cardHybridUrl);
208 }
209 catch (TaskCanceledException)
210 {
211 Debug.WriteLine("[CardHybrid] Server navigation canceled.");
212 }
213 catch (Exception ex)
214 {
215 Debug.WriteLine($"[CardHybrid] {ex}");
216 }
217 }
218}
static ? IServiceProvider ServiceProvider
Definition App.xaml.cs:28
static async Task NavigateServerAsync(WebView2 webView, string baseUrl, string expectedInstanceId, int timeoutMs=15000, int intervalMs=500)
MainWindow(DreamineBlazorServerHostOptions serverOptions)
readonly DreamineBlazorServerHostOptions _serverOptions
async void OnLoaded(object sender, RoutedEventArgs e)