johann.z
Goto Top

Method not found Void System.Windows.Forms.ToolStripItem.set Image

Hallo zusammen,
ich habe eine DLL erstellt, die über den Kontext ausgewählte Dateien in der WPF-Window DataGrid anzeigt.
Es funktioniert einwandfrei.

Wenn ich jedoch ein Icon dem Kontext-Eintrag zuweise "mainItem.Image = image;", läuft die DLL auf eine Exception:
2024-08-31 10:03:49.113Z - explorer - SharpShell Diagnostics Initialised. Process explorer.
2024-08-31 10:03:49.132Z - explorer - SimpleContextMenu: Initializing shell extension...
2024-08-31 10:03:49.137Z - explorer - SimpleContextMenu: Shell extension initialised.
Parent folder: <none>
Items: 
D:\Textdokument (neu) - Kopie - Kopie.mpr
2024-08-31 10:03:49.138Z - explorer - SimpleContextMenu: Query Context Menu for items: 
D:\Textdokument (neu) - Kopie - Kopie.mpr
2024-08-31 10:03:49.140Z - explorer - error: SimpleContextMenu: An exception occured building the context menu.
2024-08-31 10:03:49.152Z - explorer - error: System.MissingMethodException: Method not found: 'Void System.Windows.Forms.ToolStripItem.set_Image(System.Drawing.Image)'.  
   at SharpShell_DLL.SimpleContextMenu.CreateMenu()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at SharpShell.SharpContextMenu.SharpContextMenu.SharpShell.Interop.IContextMenu.QueryContextMenu(IntPtr hMenu, UInt32 indexMenu, Int32 idCmdFirst, Int32 idCmdLast, CMF uFlags)

Logging ist über diese Anwendung umgesetzt: Log

Ich nutze Windows 11 22H2 + VS2022 + .NET8. + SharpShell + Microsoft.Windows.Compatibility + CommunityToolkit.Mvvm

Die Umsetzung ist nach folgender Anleitung umgesetzt, da ich unbedingt .NET8 + WPF verwenden möchte:
Creating Shell Extensions in .NET 8 with SharpShell

Ohne "mainItem.Image = image;" läuft alles korrekt.

Mein Code:
 protected override ContextMenuStrip CreateMenu()
 {
     ToolStripMenuItem mainItem;
     ContextMenuStrip menu = new ContextMenuStrip();

     try
     {
         mainItem = new ToolStripMenuItem();
         image = new Bitmap(@"C:\Users\zajze\Documents\GitHub\Sauger_Automation\SharpShell_DLL\SharpShell_DLL\Resources\SG_16.ico");  
         //image = Resource_SharpShell.SG_16.ToBitmap();
         //menuItem.Image = new Bitmap(Resource_SharpShell.SG_16.ToBitmap(), new Size(16, 16));
         //image = Image.FromFile(@"C:\Users\zajze\Documents\GitHub\Sauger_Automation\SharpShell_DLL\SharpShell_DLL\Icons\SG_16.ico"); 
         //image = new Bitmap(@"C:\Users\zajze\Documents\GitHub\Sauger_Automation\SharpShell_DLL\SharpShell_DLL\Icons\SG_16.ico"); 
         mainItem.Image = image;
         mainItem.Text = name;
         menu.Items.Add(mainItem);

         mainItem.Click += (sender, e) =>
         {
             var builder2 = new StringBuilder();
             foreach (var FulPath in SelectedItemPaths) { builder2.AppendLine(string.Format("{0}", Path.GetFullPath(FulPath))); }  
             RunWpfApp(SelectedItemPaths);
         };
     }
     catch (Exception ex)
     {
         File.WriteAllLines("d:\\DEBUGssss.txt", new string[] { ex.Message });  
     }

     return menu;
 }

Project.csproj
<Project Sdk="Microsoft.NET.Sdk">  

	<PropertyGroup>
		<TargetFramework>net8.0-windows</TargetFramework>
		<ImplicitUsings>enable</ImplicitUsings>
		<Nullable>enable</Nullable>
		<UseWindowsForms>true</UseWindowsForms>
		<EnableComHosting>true</EnableComHosting>
	</PropertyGroup>

	<ItemGroup>
		<None Remove="Icons\Icon Entry_71.ico" />  
		<None Remove="Icons\SG_16.ico" />  
		<None Remove="Icons\SG_16_Image.png" />  
		<None Remove="Icons\SG_16_Image_JPG.jpg" />  
		<None Remove="Icons\SG_64.ico" />  
		<None Remove="Resources\Icon Entry_32.ico" />  
		<None Remove="Resources\SG_16.ico" />  
		<None Remove="Resources\SG_16_Image.png" />  
		<None Remove="Resources\SG_16_Image_JPG.jpg" />  
		<None Remove="Resources\SG_64.ico" />  
		<None Remove="Resources\Wwop_48.ico" />  
	</ItemGroup>

	<ItemGroup>
		<COMReference Include="{d37e2a3e-8545-3a39-9f4f-31827c9124ab}">  
			<WrapperTool>tlbimp</WrapperTool>
			<VersionMinor>4</VersionMinor>
			<VersionMajor>2</VersionMajor>
			<Guid>d37e2a3e-8545-3a39-9f4f-31827c9124ab</Guid>
		</COMReference>
	</ItemGroup>

	<ItemGroup>
		<PackageReference Include="Microsoft.Windows.Compatibility" Version="8.0.8" />  
		<PackageReference Include="SharpShell" Version="2.7.2" />  
	</ItemGroup>

	<ItemGroup>
		<ProjectReference Include="..\..\Sauger_Automation\Sauger_Automation\Sauger_Automation.csproj" />  
	</ItemGroup>

	<ItemGroup>
		<Resource Include="Icons\Icon Entry_71.ico" />  
		<Resource Include="Icons\SG_16.ico" />  
		<Resource Include="Icons\SG_16_Image.png" />  
		<Resource Include="Icons\SG_16_Image_JPG.jpg" />  
		<Resource Include="Icons\SG_64.ico" />  
		<Resource Include="Resources\Icon Entry_32.ico" />  
		<Resource Include="Resources\SG_16_Image.png" />  
		<Resource Include="Resources\SG_16_Image_JPG.jpg" />  
		<Resource Include="Resources\SG_64.ico" />  
		<Resource Include="Resources\Wwop_48.ico" />  
	</ItemGroup>

	<ItemGroup>
		<Resource Include="Resources\SG_16.ico">  
			<CopyToOutputDirectory>Always</CopyToOutputDirectory>
		</Resource>
	</ItemGroup>

	<ItemGroup>
		<Compile Update="Resource_SharpShell.Designer.cs">  
			<DesignTime>True</DesignTime>
			<AutoGen>True</AutoGen>
			<DependentUpon>Resource_SharpShell.resx</DependentUpon>
		</Compile>
	</ItemGroup>

	<ItemGroup>
		<EmbeddedResource Update="Resource_SharpShell.resx">  
			<Generator>ResXFileCodeGenerator</Generator>
			<LastGenOutput>Resource_SharpShell.Designer.cs</LastGenOutput>
		</EmbeddedResource>
	</ItemGroup>

</Project>

Kann mir jemand einen Hinweis geben, warum die Stelle "mainItem.Image = image;" Probleme verursacht?
Oder einen anderen Ansatz, um ein Icon im Kontextmenü zu erzeugen.

Vielen Dank im Voraus.

Content-ID: 667787

Url: https://administrator.de/contentid/667787

Printed on: August 31, 2024 at 23:08 o'clock