Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/dotnet-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Setup Xcode
uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: '26.1'
xcode-version: '26.2'

- name: Workload install
run: dotnet workload install maui
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<NetVersion>net10.0</NetVersion>
<MauiPackageVersion>10.0.10</MauiPackageVersion>
<MauiPackageVersion>10.0.30</MauiPackageVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
Expand Down
50 changes: 29 additions & 21 deletions samples/PJ.ContextActions.Sample/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="PJ.ContextActions.Sample.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:PJ.ContextActions.Sample"
xmlns:pj="clr-namespace:PJ.ContextActions.Maui;assembly=PJ.ContextActions.Maui">
x:Class="PJ.ContextActions.Sample.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:PJ.ContextActions.Sample"
xmlns:pj="clr-namespace:PJ.ContextActions.Maui;assembly=PJ.ContextActions.Maui">

<Image Source="dotnet_bot.png">
<Image.Behaviors>
<pj:ContextActionBehavior>
<pj:ContextActionBehavior.MenuItems>
<pj:MenuItem
Clicked="MenuItem_Clicked"
Icon="dotnet_bot.png"
Text="Primeiro" />
<pj:MenuItem Command="{Binding ClickCommand}" Text="Segundo" />
</pj:ContextActionBehavior.MenuItems>
</pj:ContextActionBehavior>
</Image.Behaviors>
</Image>
<Image Source="dotnet_bot.png">
<Image.Behaviors>
<pj:ContextActionBehavior>
<pj:ContextActionBehavior.MenuItems>
<pj:MenuItem
Clicked="MenuItem_Clicked"
Icon="dotnet_bot.png"
Text="Primeiro"
UseMenuResultModel="True" />
<pj:MenuItem
Command="{Binding ClickCommand}"
Text="Segundo"
UseMenuResultModel="True" />
</pj:ContextActionBehavior.MenuItems>
</pj:ContextActionBehavior>
</Image.Behaviors>
</Image>


<!--<CollectionView x:Name="cv">
<!--<CollectionView x:Name="cv">
<pj:ContextActions.ContextActions>
<pj:MenuItem
Clicked="MenuItem_Clicked"
Icon="dotnet_bot.png"
Text="Primeiro" />
<pj:MenuItem Command="{Binding ClickCommand}" Text="Segundo" />
Text="Primeiro"
UseMenuResultModel="true" />
<pj:MenuItem
Command="{Binding ClickCommand}"
Text="Segundo"
UseMenuResultModel="true" />
</pj:ContextActions.ContextActions>

<CollectionView.ItemTemplate>
Expand Down
12 changes: 7 additions & 5 deletions samples/PJ.ContextActions.Sample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Diagnostics;
using PJ.ContextActions.Maui;

namespace PJ.ContextActions.Sample;

public partial class MainPage : ContentPage
{
public Command<object> ClickCommand { get; }
public Command<MenuItemResult> ClickCommand { get; }

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

//cv.ItemsSource = list;
// cv.ItemsSource = list;

ClickCommand = new Command<object>((i) =>
ClickCommand = new Command<MenuItemResult>((i) =>
{
Debug.Assert(i is not null);

//Debug.Assert(i is string);

System.Diagnostics.Debug.WriteLine($"Segundo item clicado: {i}");
System.Diagnostics.Debug.WriteLine($"item {i.Text} clicado: {i.Item}");
});

BindingContext = this;
}

void MenuItem_Clicked(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine($"Primeiro item clicado: {sender}");
var i = (MenuItemResult)sender;
System.Diagnostics.Debug.WriteLine($"item {i.Text} clicado: {i.Item}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,7 @@ protected virtual void OnContainerContentChanging(ListViewBase sender, Container

mauiCommand ??= new Command<CommandBag>(static bag =>
{
bag.item.FireClicked(bag.cvItem);
var command = bag.item.Command;

if (command?.CanExecute(bag.cvItem) is true)
{
command.Execute(bag.cvItem);
}
bag.HandleCommandBag();
});

foreach (var item in menuItems)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using WMenyFlyoutItem = Microsoft.UI.Xaml.Controls.MenuFlyoutItem;

namespace PJ.ContextActions.Maui;

partial class ContextActionBehavior : PlatformBehavior<View, FrameworkElement>
{
protected override void OnAttachedTo(View bindable, FrameworkElement platformView)
Expand All @@ -27,13 +28,7 @@ WMenuFlyout CreateMenu(View view)

var mauiCommand = new Command<CommandBag>(static bag =>
{
bag.item.FireClicked(bag.cvItem);
var command = bag.item.Command;

if (command?.CanExecute(bag.cvItem) is true)
{
command.Execute(bag.cvItem);
}
bag.HandleCommandBag();
});

foreach (var item in MenuItems)
Expand Down
22 changes: 19 additions & 3 deletions src/PJ.ContextActions.Maui/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ static class Helpers
public static T? GetValueOrDefault<T>(this WeakReference<T> weak)
where T : class
=> weak.TryGetTarget(out var value) ? value : default;

public static void HandleCommandBag(this CommandBag bag)
{
var item = bag.item;
var command = item.Command;
object result = item.UseMenuResultModel ? new MenuItemResult(item.Text, bag.cvItem) : bag.cvItem;

item.FireClicked(result);

if (command?.CanExecute(result) is true)
{
command.Execute(result);
}
}

#if WINDOWS
public static IconElement? CreateIconElementFromIconPath(this string iconPath)
{
Expand Down Expand Up @@ -47,11 +62,12 @@ public static IEnumerable<UIMenuElement> CreateMenuItems(ICollection<MenuItem> i
index.ToString(),
_ =>
{
item.FireClicked(element);
object result = item.UseMenuResultModel ? new MenuItemResult(item.Text, element) : element;
item.FireClicked(result);
var command = item.Command;
if (command?.CanExecute(element) is true)
if (command?.CanExecute(result) is true)
{
command.Execute(element);
command.Execute(result);
}
});

Expand Down
4 changes: 3 additions & 1 deletion src/PJ.ContextActions.Maui/MenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ public ICommand? Command
set => SetValue(CommandProperty, value);
}

public bool UseMenuResultModel { get; set; }

public event EventHandler? Clicked;

internal void FireClicked(object cell) => Clicked?.Invoke(cell, EventArgs.Empty);
internal void FireClicked(object result) => Clicked?.Invoke(result, EventArgs.Empty);

protected override void OnParentSet()
{
Expand Down
10 changes: 1 addition & 9 deletions src/PJ.ContextActions.Maui/MenuItemClickListener.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@ public bool OnMenuItemClick(IMenuItem item)
return false;
}

var menuItem = bag.item;
var element = bag.cvItem;

menuItem.FireClicked(element);

if (menuItem.Command?.CanExecute(element) is true)
{
menuItem.Command.Execute(element);
}
bag.HandleCommandBag();

return true;
}
Expand Down
4 changes: 3 additions & 1 deletion src/PJ.ContextActions.Maui/Records.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
namespace PJ.ContextActions.Maui;
record CommandBag(object cvItem, MenuItem item);
record CommandBag(object cvItem, MenuItem item);

public record MenuItemResult(string Text, object Item);