Skip to content
Draft
4 changes: 3 additions & 1 deletion samples/ControlGallery/ControlGallery/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public partial class MainPage : FlyoutPage
[typeof(ScreenshotPage)] = () => new ScreenshotPage(),
[typeof(PreferencesPage)] = () => new PreferencesPage(),
[typeof(FilePickerPage)] = () => new FilePickerPage(),
[typeof(AppActionsPage)] = () => new AppActionsPage(),
// Settings
[typeof(ThemePage)] = () => new ThemePage(),
// Embedding
Expand Down Expand Up @@ -282,9 +283,10 @@ private void InitializeSamples()

new SampleGroup("Essentials", new List<SampleItem>
{
new("App Actions", "Add custom actions to your app", typeof(AppActionsPage)),
new("File Picker", "Pick files from the device", typeof(FilePickerPage)),
new("Preferences", "Key/value storage for app settings", typeof(PreferencesPage)),
new("Screenshot", "Capture window screenshots", typeof(ScreenshotPage)),
new("Screenshot", "Capture window screenshots", typeof(ScreenshotPage))
}),

new SampleGroup("Embedding", new List<SampleItem>
Expand Down
44 changes: 44 additions & 0 deletions samples/ControlGallery/ControlGallery/Pages/AppActionsPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<ContentPage x:Class="ControlGallery.Pages.AppActionsPage"
Title="AppActions">

<VerticalStackLayout Padding="20" Spacing="12">

<!-- Entry for new action -->
<Entry x:Name="ActionTitleEntry"
Placeholder="Enter action title" />

<Entry x:Name="ActionIdEntry"
Placeholder="Enter action ID" />

<!-- Buttons -->
<HorizontalStackLayout Spacing="10">
<Button x:Name="AddActionbutton" Text="Add Action"
Clicked="OnAddClicked" />

<Button x:Name="RemoveSelectedButton" Text="Remove Selected"
Clicked="OnRemoveClicked" />
</HorizontalStackLayout>

<!-- List of actions -->
<CollectionView x:Name="ActionsList"
SelectionMode="Single"
HeightRequest="250">
<CollectionView.ItemTemplate>
<DataTemplate>
<Border Stroke="Gray" Padding="10" Margin="5">
<VerticalStackLayout>
<Label Text="{Binding Title}" FontAttributes="Bold"/>
<Label Text="{Binding Id}" FontSize="12"/>
</VerticalStackLayout>
</Border>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

<!-- Last triggered action -->
<Label x:Name="LastActionLabel"
Text="Last triggered: None"
FontAttributes="Italic" />

</VerticalStackLayout>
</ContentPage>
86 changes: 86 additions & 0 deletions samples/ControlGallery/ControlGallery/Pages/AppActionsPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.Collections.ObjectModel;

namespace ControlGallery.Pages;

public partial class AppActionsPage : ContentPage
{
private ObservableCollection<AppAction> _actions = new();

public AppActionsPage()
{
InitializeComponent();

if (!AppActions.Current.IsSupported)
{
AddActionbutton.IsEnabled = false;
RemoveSelectedButton.IsEnabled = false;
}

ActionsList.ItemsSource = _actions;
}

private async void OnAddClicked(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(ActionTitleEntry.Text) ||
string.IsNullOrWhiteSpace(ActionIdEntry.Text))
return;

_actions.Add(new AppAction(ActionIdEntry.Text, ActionTitleEntry.Text));

try
{
await AppActions.Current.SetAsync(_actions);
}
catch (Exception ex)
{
await DisplayAlert("Error", ex.Message, "OK");
}

ActionTitleEntry.Text = string.Empty;
ActionIdEntry.Text = string.Empty;
}

private async void OnRemoveClicked(object sender, EventArgs e)
{
if (ActionsList.SelectedItem is AppAction selected)
{
_actions.Remove(selected);

try
{
await AppActions.Current.SetAsync(_actions);
}
catch (Exception ex)
{
await DisplayAlert("Error", ex.Message, "OK");
}
}
}

private void OnAppActionActivated(object? sender, AppActionEventArgs e)
{
Dispatcher.Dispatch(async () =>
{
LastActionLabel.Text = $"Last triggered: {e.AppAction.Title} ({e.AppAction.Id})";

await Task.Delay(5000);

LastActionLabel.Text = "Last triggered: None";
});
}

protected override void OnAppearing()
{
base.OnAppearing();

AppActions.Current.AppActionActivated += OnAppActionActivated;
}

protected override void OnDisappearing()
{
base.OnDisappearing();

// Avoid memory leaks
AppActions.Current.AppActionActivated -= OnAppActionActivated;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.Maui.ApplicationModel;

namespace Avalonia.Controls.Maui.Essentials
{
partial class AvaloniaAppActions
{
public bool PlatformIsSupported() => false;

public Task<bool> PlatformSetAsync(IEnumerable<AppAction> actions) =>
throw new FeatureNotSupportedException();
}
}
Loading