Skip to content

Commit ff6c58e

Browse files
committed
Add icon support to context menu items
- Updated `MainPage.xaml` to include an icon for the first menu item. - Added `CreateIconElementFromIconPath` method in `Helpers.cs` to create a `BitmapIcon` from a path, with error handling. - Introduced `CreateImage` method in `PJDelegator` to generate a `UIImage` from an icon path. - Modified `PJCollectionViewHandler.windows.cs` to set icons for `WMenuFlyoutItem` using the new helper method.
1 parent 7330361 commit ff6c58e

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

samples/PJ.ContextActions.Sample/MainPage.xaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
<CollectionView x:Name="cv">
1111
<pj:ContextActions.ContextActions>
12-
<pj:MenuItem Clicked="MenuItem_Clicked" Text="Primeiro" />
12+
<pj:MenuItem
13+
Clicked="MenuItem_Clicked"
14+
Icon="dotnet_bot.png"
15+
Text="Primeiro" />
1316
<pj:MenuItem Command="{Binding ClickCommand}" Text="Segundo" />
1417
</pj:ContextActions.ContextActions>
1518

src/PJ.ContextActions.Maui/Helpers.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using static System.Reflection.BindingFlags;
1+
#if WINDOWS
2+
using Microsoft.UI.Xaml.Controls;
3+
#endif
4+
using static System.Reflection.BindingFlags;
25

36
namespace PJ.ContextActions.Maui;
47
static class Helpers
@@ -20,5 +23,29 @@ public static object ToCollectionViewModel(this object item)
2023

2124
return value;
2225
}
26+
27+
public static IconElement? CreateIconElementFromIconPath(this string iconPath)
28+
{
29+
try
30+
{
31+
// Create a BitmapIcon from the path
32+
var bitmapIcon = new BitmapIcon
33+
{
34+
ShowAsMonochrome = false
35+
};
36+
37+
// First, try to load from app resources without prepending any path
38+
// This works for files set as MauiImage in the .csproj
39+
var uri = new System.Uri($"ms-appx:///{iconPath}");
40+
bitmapIcon.UriSource = uri;
41+
42+
return bitmapIcon;
43+
}
44+
catch (Exception ex)
45+
{
46+
System.Diagnostics.Debug.WriteLine($"Failed to create icon from {iconPath}: {ex.Message}");
47+
return null;
48+
}
49+
}
2350
#endif
2451
}

src/PJ.ContextActions.Maui/PJCollectionViewHandler.ios.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static IEnumerable<UIMenuElement> CreateMenuItems(List<MenuItem> items, Bindable
6969
item.BindingContext = cv.BindingContext;
7070
var action = UIAction.Create(
7171
item.Text,
72-
null,
72+
CreateImage(item.Icon),
7373
index.ToString(),
7474
_ =>
7575
{
@@ -83,6 +83,11 @@ static IEnumerable<UIMenuElement> CreateMenuItems(List<MenuItem> items, Bindable
8383

8484
yield return action;
8585
}
86+
87+
static UIImage? CreateImage(string? icon)
88+
{
89+
return string.IsNullOrEmpty(icon) ? null : new UIImage(icon);
90+
}
8691
}
8792
}
8893
}

src/PJ.ContextActions.Maui/PJCollectionViewHandler.windows.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Maui.Controls.Handlers.Items;
22
using Microsoft.UI.Xaml.Controls;
3+
using Microsoft.UI.Xaml.Media.Imaging;
34
using WMenuFlyout = Microsoft.UI.Xaml.Controls.MenuFlyout;
45
using WMenuFlyoutItem = Microsoft.UI.Xaml.Controls.MenuFlyoutItem;
56

@@ -56,12 +57,19 @@ void OnContainerContentChanging(ListViewBase sender, ContainerContentChangingEve
5657
{
5758
item.BindingContext = VirtualView.BindingContext;
5859

59-
menuFlyout.Items.Add(new WMenuFlyoutItem
60+
var menuFlyoutItem = new WMenuFlyoutItem
6061
{
6162
Text = item.Text,
6263
Command = mauiCommand,
6364
CommandParameter = new CommandBag(model, item)
64-
});
65+
};
66+
67+
if (!string.IsNullOrEmpty(item.Icon))
68+
{
69+
menuFlyoutItem.Icon = item.Icon.CreateIconElementFromIconPath();
70+
}
71+
72+
menuFlyout.Items.Add(menuFlyoutItem);
6573
}
6674

6775
args.ItemContainer.ContextFlyout = menuFlyout;

0 commit comments

Comments
 (0)