仅在移动设备上从Windows Runtime组件访问UI线程时抛出异常
Hey there, let's work through this WebView-Windows Runtime Component communication issue together. I’ve dealt with similar setups plenty of times, so here’s what’s likely missing or needs tweaking based on your code snippet:
First off, your Runtime Component code looks solid—you’ve got the [AllowForWeb] attribute, a sealed class, and the delegate/event setup checks out. The problem is almost certainly in how you’re initializing the WebView and binding the event in your app code, or how you’re calling the component from the web side.
1. Complete Your WebView Initialization in OnNavigatedTo
You mentioned you started setting up the WebView in OnNavigatedTo, so let’s fill in the missing pieces. You need to create an instance of your component, bind the event handler, and inject the object into the WebView’s script context:
protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); // Create an instance of your injection object var webBridge = new WebViewInjectionObject(); // Bind your event handler to catch messages from the web webBridge.OnNotifyApp += WebBridge_OnNotifyApp; // Inject the object into the WebView's global scope (name it something like "AppBridge" for web-side access) MyWebViewControl.AddWebAllowedObject("AppBridge", webBridge); } // Your event handler to process messages from the WebView private void WebBridge_OnNotifyApp(string message) { // Do something with the incoming message—log it, update UI, etc. System.Diagnostics.Debug.WriteLine($"Message received from WebView: {message}"); // If you need to update the UI, make sure to run it on the UI thread Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { MyStatusTextBlock.Text = $"Received: {message}"; }); }
2. Verify the Web-Side Call
Make sure your web page is correctly calling the injected object. Here’s how the JavaScript should look:
// Call the NotifyApp method on the injected object to send a message to the app window.AppBridge.NotifyApp("Hello from the web!");
Quick Check for Local Web Pages
If your web content is local (packaged with your app), double-check your Package.appxmanifest has the right ContentUriRules to allow the local URI to access WinRT components. Add this under the Application node if it’s missing:
<Applications> <Application ...> <uap:ContentUriRules> <uap:Rule Match="ms-appx-web:///" Type="include" WindowsRuntimeAccess="all" /> </uap:ContentUriRules> </Application> </Applications>
3. Common Pitfalls to Avoid
- Timing is key: Inject the object and bind the event before the WebView finishes loading its content.
OnNavigatedTois a good spot, but if you’re navigating dynamically, make sure to re-inject if the WebView navigates to a new page. - UI Thread Requirements: Any UI updates from the event handler must be dispatched to the UI thread (like the
Dispatcher.RunAsyncexample above)—WinRT events from WebView fire on a background thread. - No Typos: Double-check the name you use in
AddWebAllowedObjectmatches exactly what you call in JavaScript (AppBridgein our example). Case matters!
If you’re still hitting issues, share your full WebView initialization code and any error messages you’re seeing, and we can dig deeper.
内容的提问来源于stack exchange,提问作者Corcus

