Skip to content

Commit 1210168

Browse files
pictosPedro Jesus
andauthored
Use MenuItemResult record as CommandParameter (#12)
* windows update * update sample * Android update * small refactor * bump xcode version * ios implementation --------- Co-authored-by: Pedro Jesus <pedrojesus@Pedros-MacBook-Pro.local>
1 parent ae66238 commit 1210168

File tree

10 files changed

+67
-56
lines changed

10 files changed

+67
-56
lines changed

.github/workflows/dotnet-macos.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- name: Setup Xcode
2929
uses: maxim-lobanov/setup-xcode@v1
3030
with:
31-
xcode-version: '26.1'
31+
xcode-version: '26.2'
3232

3333
- name: Workload install
3434
run: dotnet workload install maui

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<PropertyGroup>
33
<NetVersion>net10.0</NetVersion>
4-
<MauiPackageVersion>10.0.10</MauiPackageVersion>
4+
<MauiPackageVersion>10.0.30</MauiPackageVersion>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<LangVersion>preview</LangVersion>

samples/PJ.ContextActions.Sample/MainPage.xaml

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<ContentPage
3-
x:Class="PJ.ContextActions.Sample.MainPage"
4-
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
5-
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6-
xmlns:local="clr-namespace:PJ.ContextActions.Sample"
7-
xmlns:pj="clr-namespace:PJ.ContextActions.Maui;assembly=PJ.ContextActions.Maui">
3+
x:Class="PJ.ContextActions.Sample.MainPage"
4+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:local="clr-namespace:PJ.ContextActions.Sample"
7+
xmlns:pj="clr-namespace:PJ.ContextActions.Maui;assembly=PJ.ContextActions.Maui">
88

9-
<Image Source="dotnet_bot.png">
10-
<Image.Behaviors>
11-
<pj:ContextActionBehavior>
12-
<pj:ContextActionBehavior.MenuItems>
13-
<pj:MenuItem
14-
Clicked="MenuItem_Clicked"
15-
Icon="dotnet_bot.png"
16-
Text="Primeiro" />
17-
<pj:MenuItem Command="{Binding ClickCommand}" Text="Segundo" />
18-
</pj:ContextActionBehavior.MenuItems>
19-
</pj:ContextActionBehavior>
20-
</Image.Behaviors>
21-
</Image>
9+
<Image Source="dotnet_bot.png">
10+
<Image.Behaviors>
11+
<pj:ContextActionBehavior>
12+
<pj:ContextActionBehavior.MenuItems>
13+
<pj:MenuItem
14+
Clicked="MenuItem_Clicked"
15+
Icon="dotnet_bot.png"
16+
Text="Primeiro"
17+
UseMenuResultModel="True" />
18+
<pj:MenuItem
19+
Command="{Binding ClickCommand}"
20+
Text="Segundo"
21+
UseMenuResultModel="True" />
22+
</pj:ContextActionBehavior.MenuItems>
23+
</pj:ContextActionBehavior>
24+
</Image.Behaviors>
25+
</Image>
2226

2327

24-
<!--<CollectionView x:Name="cv">
28+
<!--<CollectionView x:Name="cv">
2529
<pj:ContextActions.ContextActions>
2630
<pj:MenuItem
2731
Clicked="MenuItem_Clicked"
2832
Icon="dotnet_bot.png"
29-
Text="Primeiro" />
30-
<pj:MenuItem Command="{Binding ClickCommand}" Text="Segundo" />
33+
Text="Primeiro"
34+
UseMenuResultModel="true" />
35+
<pj:MenuItem
36+
Command="{Binding ClickCommand}"
37+
Text="Segundo"
38+
UseMenuResultModel="true" />
3139
</pj:ContextActions.ContextActions>
3240
3341
<CollectionView.ItemTemplate>
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System.Diagnostics;
2+
using PJ.ContextActions.Maui;
23

34
namespace PJ.ContextActions.Sample;
45

56
public partial class MainPage : ContentPage
67
{
7-
public Command<object> ClickCommand { get; }
8+
public Command<MenuItemResult> ClickCommand { get; }
89

910
public MainPage()
1011
{
@@ -14,22 +15,23 @@ public MainPage()
1415
for (var i = 0; i < 100; i++)
1516
list.Add($"Item {i}");
1617

17-
//cv.ItemsSource = list;
18+
// cv.ItemsSource = list;
1819

19-
ClickCommand = new Command<object>((i) =>
20+
ClickCommand = new Command<MenuItemResult>((i) =>
2021
{
2122
Debug.Assert(i is not null);
2223

2324
//Debug.Assert(i is string);
2425

25-
System.Diagnostics.Debug.WriteLine($"Segundo item clicado: {i}");
26+
System.Diagnostics.Debug.WriteLine($"item {i.Text} clicado: {i.Item}");
2627
});
2728

2829
BindingContext = this;
2930
}
3031

3132
void MenuItem_Clicked(object sender, EventArgs e)
3233
{
33-
System.Diagnostics.Debug.WriteLine($"Primeiro item clicado: {sender}");
34+
var i = (MenuItemResult)sender;
35+
System.Diagnostics.Debug.WriteLine($"item {i.Text} clicado: {i.Item}");
3436
}
3537
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,7 @@ protected virtual void OnContainerContentChanging(ListViewBase sender, Container
5454

5555
mauiCommand ??= new Command<CommandBag>(static bag =>
5656
{
57-
bag.item.FireClicked(bag.cvItem);
58-
var command = bag.item.Command;
59-
60-
if (command?.CanExecute(bag.cvItem) is true)
61-
{
62-
command.Execute(bag.cvItem);
63-
}
57+
bag.HandleCommandBag();
6458
});
6559

6660
foreach (var item in menuItems)

src/PJ.ContextActions.Maui/Controls/ContextActionBehavior.windows.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using WMenyFlyoutItem = Microsoft.UI.Xaml.Controls.MenuFlyoutItem;
44

55
namespace PJ.ContextActions.Maui;
6+
67
partial class ContextActionBehavior : PlatformBehavior<View, FrameworkElement>
78
{
89
protected override void OnAttachedTo(View bindable, FrameworkElement platformView)
@@ -27,13 +28,7 @@ WMenuFlyout CreateMenu(View view)
2728

2829
var mauiCommand = new Command<CommandBag>(static bag =>
2930
{
30-
bag.item.FireClicked(bag.cvItem);
31-
var command = bag.item.Command;
32-
33-
if (command?.CanExecute(bag.cvItem) is true)
34-
{
35-
command.Execute(bag.cvItem);
36-
}
31+
bag.HandleCommandBag();
3732
});
3833

3934
foreach (var item in MenuItems)

src/PJ.ContextActions.Maui/Helpers.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ static class Helpers
1111
public static T? GetValueOrDefault<T>(this WeakReference<T> weak)
1212
where T : class
1313
=> weak.TryGetTarget(out var value) ? value : default;
14+
15+
public static void HandleCommandBag(this CommandBag bag)
16+
{
17+
var item = bag.item;
18+
var command = item.Command;
19+
object result = item.UseMenuResultModel ? new MenuItemResult(item.Text, bag.cvItem) : bag.cvItem;
20+
21+
item.FireClicked(result);
22+
23+
if (command?.CanExecute(result) is true)
24+
{
25+
command.Execute(result);
26+
}
27+
}
28+
1429
#if WINDOWS
1530
public static IconElement? CreateIconElementFromIconPath(this string iconPath)
1631
{
@@ -47,11 +62,12 @@ public static IEnumerable<UIMenuElement> CreateMenuItems(ICollection<MenuItem> i
4762
index.ToString(),
4863
_ =>
4964
{
50-
item.FireClicked(element);
65+
object result = item.UseMenuResultModel ? new MenuItemResult(item.Text, element) : element;
66+
item.FireClicked(result);
5167
var command = item.Command;
52-
if (command?.CanExecute(element) is true)
68+
if (command?.CanExecute(result) is true)
5369
{
54-
command.Execute(element);
70+
command.Execute(result);
5571
}
5672
});
5773

src/PJ.ContextActions.Maui/MenuItem.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ public ICommand? Command
2121
set => SetValue(CommandProperty, value);
2222
}
2323

24+
public bool UseMenuResultModel { get; set; }
25+
2426
public event EventHandler? Clicked;
2527

26-
internal void FireClicked(object cell) => Clicked?.Invoke(cell, EventArgs.Empty);
28+
internal void FireClicked(object result) => Clicked?.Invoke(result, EventArgs.Empty);
2729

2830
protected override void OnParentSet()
2931
{

src/PJ.ContextActions.Maui/MenuItemClickListener.android.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,7 @@ public bool OnMenuItemClick(IMenuItem item)
1818
return false;
1919
}
2020

21-
var menuItem = bag.item;
22-
var element = bag.cvItem;
23-
24-
menuItem.FireClicked(element);
25-
26-
if (menuItem.Command?.CanExecute(element) is true)
27-
{
28-
menuItem.Command.Execute(element);
29-
}
21+
bag.HandleCommandBag();
3022

3123
return true;
3224
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
namespace PJ.ContextActions.Maui;
2-
record CommandBag(object cvItem, MenuItem item);
2+
record CommandBag(object cvItem, MenuItem item);
3+
4+
public record MenuItemResult(string Text, object Item);

0 commit comments

Comments
 (0)