Electron WinML Sample
February 17, 2026 ยท View on GitHub
This sample demonstrates how to integrate Windows Machine Learning (WinML) into an Electron application using the Windows App Development CLI. The app uses the SqueezeNet 1.1 model to classify images directly on the user's device, and the Phi model for text generation.
What's Included
- Image Classification: Uses SqueezeNet 1.1 ONNX model for real-time image classification
- C# Native Addon: A .NET 10 Native AOT addon that bridges JavaScript and WinML APIs
- Hardware Acceleration: Automatically uses CPU, GPU, or NPU based on device capabilities
- Production Ready: Includes MSIX packaging configuration and ASAR handling for distribution
Features
- ๐ผ๏ธ Classify Images: Select any image and get top predictions with confidence scores
- โก Fast Performance: Native AOT compilation with hardware acceleration
- ๐ฆ MSIX Packaging: Ready for distribution via Microsoft Store or direct download
- ๐จ Modern UI: Simple, clean interface for testing image classification
Prerequisites
- Windows 11 or Windows 10 (version 1809+)
- Node.js -
winget install OpenJS.NodeJS --source winget - .NET SDK v10 -
winget install Microsoft.DotNet.SDK.10 --source winget - Visual Studio with the Native Desktop Workload -
winget install --id Microsoft.VisualStudio.Community --source winget --override "--add Microsoft.VisualStudio.Workload.NativeDesktop --includeRecommended --passive --wait"
Getting Started
1. Install Dependencies
npm install
This automatically runs the postinstall script which:
- Restores Windows SDK packages to
.winapp/ - Adds debug identity to Electron
2. Download the Model
The models are available in the AI Dev Gallery. Install the gallery to download the models. You don't need both models if you only care about one or the other. The models folder will need to be created.
SqueezeNet
- Navigate to the Classify Image sample
- Download the SqueezeNet 1.1 model
- Click Open Containing Folder to locate the
.onnxfile - Copy
squeezenet1.1-7.onnxto themodels/folder in this project
Phi
- Navigate to the Generate Text sample
- Download any of the Phi models from Custom models
- Click Open Containing Folder to locate model files
- Copy all the contents of the folder (should have .onnx and .json files) to the
models/phifolder in this project.
3. Build the C# Addon
npm run build-winMlAddon
This compiles the C# addon using Native AOT, creating a .node binary that requires no .NET runtime on target machines.
4. Run the App
npm start
Note: If you encounter a blank window or crash, add
--no-sandboxto the start script inpackage.jsonas a workaround for a known Windows issue.
Learn More
- Full Guide - Step-by-step tutorial
- winapp CLI Documentation - CLI reference
- WinML Documentation - Official WinML docs
- AI Dev Gallery
Troubleshooting
App crashes or shows blank window:
Add -- --no-sandbox to the start script in package.json.
Model not found:
Ensure squeezenet1.1-7.onnx is in the models/ folder.
Build errors:
Run npx winapp restore to restore SDK packages.
Certificate errors:
Reinstall the certificate (as admin): npx winapp cert install .\devcert.pfx
License
See LICENSE for details.