c# - Sharpshell with .NET Core, silently ignore menu when loading an image - Stack Overflow

admin2025-04-22  2

I created a Visual Studio project targeting net8.0-windows. I use SharpShell 2.7.2

I have the following code to create the context menu:

protected override bool CanShowMenu() {
    log("CanShowMenu");

    /* redacted */

    log("YES");
    return true;
}

protected override ContextMenuStrip CreateMenu() {
    var menu = new ContextMenuStrip();

    try {
        log("getting menu");
        var mainItem = new ToolStripMenuItem {
            Text = "Assembler A->Z"
            //,Image =  Properties.Resources.AssembleAZ

        };

        mainItem.Click += (sender, e) => {
            log(SelectedItemPaths);
        };

        menu.Items.Add(mainItem);
    } catch (Exception? ex) {
        log(ex);
    }

    return menu;
}

Everything runs fine until I uncomment

,Image =  Properties.Resources.AssembleAZ

The image (Properties.Resources.AssembleAZ) is a copy/paste of a file from the SharpShell samples loaded with a .resx file. I checked the properties of both the .resx file and the image file. Both are coherent with SharpShell sample.

Then I don't even see "getting menu" in the logs, and no error is logged by log(ex).

But I get logs from CanShowMenu().

And by checking the registry *\shellex\ContextMenuHandlers\MyKey, it seems that the server is correctly installed.

Any help welcome.

I also try by directly load the image from an existing file:

Image.FromFile(@"c:\temp\AssembleAZ.png");

I enable SharpShell debug and get the following error:

System.MissingMethodException: Method not found: 'Void System.Windows.Forms.ToolStripItem.set_Image(System.Drawing.Image)'

转载请注明原文地址:http://anycun.com/QandA/1745303146a90606.html