This example demonstrates how to integrate the SpeedOf.Me speed test API into a Windows WPF application using WebView2.
The SpeedOf.Me API is JavaScript-based, so Windows apps use WebView2 to:
In Visual Studio: File > New > Project > WPF Application
Install-Package Microsoft.Web.WebView2
SomApi.account = "YOUR_API_KEY";
SomApi.domainName = "your-domain.com";
<ItemGroup>
<Content Include="speedtest.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
WPF Application
└── SpeedTestWindow (WPF Window)
└── WebView2 Control
└── speedtest.html
└── SomApi.js
▼
window.chrome.webview.postMessage(json)
▼
CoreWebView2.WebMessageReceived event
The HTML page sends messages via:
window.chrome.webview.postMessage(JSON.stringify({
type: 'completed',
data: result
}));
C# receives them:
SpeedTestWebView.CoreWebView2.WebMessageReceived += (sender, e) => {
string json = e.WebMessageAsJson;
// Parse and handle
};
string htmlPath = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"speedtest.html"
);
SpeedTestWebView.CoreWebView2.Navigate($"file:///{htmlPath}");
SpeedTestWebView.CoreWebView2.Navigate("https://your-domain.com/speedtest.html");
Check availability:
try {
string version = CoreWebView2Environment.GetAvailableBrowserVersionString();
// Runtime is available
} catch {
// Prompt user to install from:
// https://developer.microsoft.com/microsoft-edge/webview2/
}
using Microsoft.Web.WebView2.WinForms;
public partial class SpeedTestForm : Form {
private WebView2 webView;
private async void InitializeWebView() {
webView = new WebView2();
webView.Dock = DockStyle.Fill;
Controls.Add(webView);
await webView.EnsureCoreWebView2Async();
webView.CoreWebView2.WebMessageReceived += OnWebMessageReceived;
webView.CoreWebView2.Navigate("https://your-domain.com/speedtest.html");
}
}
window.chrome.webview exists