Sample01
1.0.0.0
Sample01 사용 방법을 보여 주는 예제 프로젝트입니다.
Toggle main menu visibility
로딩중...
검색중...
일치하는것 없음
MainWindow.xaml.cs
이 파일의 문서화 페이지로 가기
1
6
using
Dreamine.Hybrid.Wpf.Controls;
7
using
Dreamine.Hybrid.Wpf.Hosting;
8
using
Sample01.Blazor.Components;
9
using
Microsoft.Web.WebView2.Wpf;
10
using
System;
11
using
System.ComponentModel;
12
using
System.Diagnostics;
13
using
System.Net.Http;
14
using
System.Threading;
15
using
System.Threading.Tasks;
16
using
System.Windows;
17
18
namespace
Sample01.Views
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
}
Sample01.Views
Definition
MainWindow.xaml.cs:19
Sample01.App
Definition
App.xaml.cs:23
Sample01.App.ServiceProvider
static ? IServiceProvider ServiceProvider
Definition
App.xaml.cs:32
Sample01.Views.MainWindow.NavigateServerAsync
static async Task NavigateServerAsync(WebView2 webView, string url, int timeoutMs=15000, int intervalMs=500)
Definition
MainWindow.xaml.cs:134
Sample01.Views.MainWindow.MainWindow
MainWindow()
Definition
MainWindow.xaml.cs:38
Sample01.Views.MainWindow.OnLoaded
async void OnLoaded(object sender, RoutedEventArgs e)
Definition
MainWindow.xaml.cs:69
Views
MainWindow.xaml.cs
다음에 의해 생성됨 :
1.17.0