Sample01 1.0.0.0
Sample01 사용 방법을 보여 주는 예제 프로젝트입니다.
로딩중...
검색중...
일치하는것 없음
MainWindow.xaml.cs
이 파일의 문서화 페이지로 가기
1
6using Dreamine.Hybrid.Wpf.Controls;
7using Dreamine.Hybrid.Wpf.Hosting;
8using Sample01.Blazor.Components;
9using Microsoft.Web.WebView2.Wpf;
10using System;
11using System.ComponentModel;
12using System.Diagnostics;
13using System.Net.Http;
14using System.Threading;
15using System.Threading.Tasks;
16using System.Windows;
17
19{
28 public partial class MainWindow : Window
29 {
38 public MainWindow()
39 {
40 InitializeComponent();
41 if (DesignerProperties.GetIsInDesignMode(this)) return;
42 Loaded += OnLoaded;
43 }
44
69 private async void OnLoaded(object sender, RoutedEventArgs e)
70 {
71 var hybrid = new HybridHostControl
72 {
73 HostPage = "wwwroot/index.html",
74 Services = App.ServiceProvider,
75 RootComponentType = typeof(LocalCounter),
76 RootSelector = "#app"
77 };
78 EmbeddedTab.Content = hybrid;
79
80 var webView = HybridWebViewHost.CreateWebView();
81 ServerTab.Content = webView;
82
83 await NavigateServerAsync(webView, "http://localhost:5000");
84 }
85
134 private static async Task NavigateServerAsync(WebView2 webView, string url, int timeoutMs = 15000, int intervalMs = 500)
135 {
136 try
137 {
138 await Task.Delay(1500);
139
140 using var cts = new CancellationTokenSource(timeoutMs);
141 using var http = new HttpClient();
142 var alive = false;
143
144 while (!cts.IsCancellationRequested)
145 {
146 try
147 {
148 var res = await http.GetAsync(url, cts.Token);
149 if ((int)res.StatusCode >= 200 && (int)res.StatusCode < 400)
150 {
151 alive = true;
152 break;
153 }
154 }
155 catch (HttpRequestException)
156 {
157 // Server may not be ready yet during polling; ignore and retry until timeout.
158 }
159 catch (TaskCanceledException)
160 {
161 // Ignore per-request timeout/cancellation during polling and continue retrying until overall timeout.
162 }
163
164 await Task.Delay(intervalMs, cts.Token);
165 }
166
167 if (!alive)
168 {
169 Debug.WriteLine($"[ServerNav] Timeout: {url}");
170 await HybridWebViewHost.ShowOfflineMessageAsync(webView, url);
171 return;
172 }
173
174 webView.Source = new Uri(url);
175 }
176 catch (TaskCanceledException)
177 {
178 Debug.WriteLine("[ServerNav] Canceled.");
179 }
180 catch (Exception ex)
181 {
182 Debug.WriteLine($"[ServerNav] {ex}");
183 }
184 }
185 }
186}
static ? IServiceProvider ServiceProvider
Definition App.xaml.cs:32
static async Task NavigateServerAsync(WebView2 webView, string url, int timeoutMs=15000, int intervalMs=500)
async void OnLoaded(object sender, RoutedEventArgs e)