Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Problem/Bug]: UWP Sample + AddHostObjectToScript fails with Exception #4856

Closed
whilename opened this issue Oct 9, 2024 · 1 comment
Closed
Assignees
Labels
bug Something isn't working

Comments

@whilename
Copy link

whilename commented Oct 9, 2024

What happened?

I've been trying to get AddHostObjectToScript to work in my UWP application. But it always fails with Exception. I tried using the WPF example browser and it worked #494.

The UWP program exception message is as follows:

System.Exception: 'The group or resource is not in the correct state to perform the requested operation.
The group or resource is not in the correct state to perform the requested operation.'

Importance

Blocking. My app's basic functions are not working due to this issue.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

Microsoft Edge WebView2 Runtime 129.0.2792.79

SDK Version

Microsoft.UI.XAML v2.8.6 & Microsoft.Web.WebView2(>=1.0.1264.42)

Framework

WinUI2/UWP

Operating System

Windows 11

OS Version

23H2 , OS build 22631.4169

Repro steps

1.Create a UWP sample application using Visual Studio 2022

2.Add the Microsoft.UI.XAML v2.8.6 NuGet package to the sample application

3.Add the use of WebView2 controls to the MainPage.xaml code file
e.g:

<Page
    x:Class="MyWebView2Project.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyWebView2Project"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:controls="using:Microsoft.UI.Xaml.Controls"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <controls:WebView2 x:Name="webView2" Source="https://bing.com" />
    </Grid>
</Page>

4.Change the code to the following in MainPage.xml.cs:

    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            InitializeWebView2();
        }

        private async void InitializeWebView2()
        {
            await webView2.EnsureCoreWebView2Async();
            webView2.CoreWebView2.AddHostObjectToScript("bridge", new Bridge());
        }

    }

    // Bridge and BridgeAnotherClass are C# classes that implement IDispatch and works with AddHostObjectToScript.
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisible(true)]
    public class BridgeAnotherClass
    {
        // Sample property.
        public string Prop { get; set; } = "Example";
    }

    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisible(true)]
    public class Bridge
    {
        public string Func(string param)
        {
            return "Example: " + param;
        }

        public BridgeAnotherClass AnotherObject { get; set; } = new BridgeAnotherClass();

        // Sample indexed property.
        [System.Runtime.CompilerServices.IndexerName("Items")]
        public string this[int index]
        {
            get { return m_dictionary[index]; }
            set { m_dictionary[index] = value; }
        }
        private Dictionary<int, string> m_dictionary = new Dictionary<int, string>();
    }

5.Debug and run the program, and the final program raises an exception as shown:
image

Repros in Edge Browser

Yes, issue can be reproduced in the corresponding Edge version

Regression

No, this never worked

Last working version (if regression)

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants