Skip to content

Commit 26a56bb

Browse files
Add Support for Shell + IQueryAttributable for PopupV2 (#2661)
1 parent 5245ca0 commit 26a56bb

File tree

15 files changed

+1309
-168
lines changed

15 files changed

+1309
-168
lines changed

samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/PopupsPage.xaml.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@ public PopupsPage(PopupsViewModel multiplePopupViewModel, IPopupService popupSer
1919

2020
async void HandleSimplePopupButtonClicked(object sender, EventArgs e)
2121
{
22-
await popupService.ShowPopupAsync<SimplePopup>(Navigation, new PopupOptions
22+
var queryAttributes = new Dictionary<string, object>
2323
{
24-
Shape = new RoundRectangle
24+
["DescriptionLabel"] = "This is a popup where this text is being passed in using IQueryAttributable"
25+
};
26+
27+
await popupService.ShowPopupAsync<SimplePopup>(Shell.Current, new PopupOptions
2528
{
26-
CornerRadius = new CornerRadius(4),
27-
Stroke = Colors.White
28-
},
29-
}, CancellationToken.None);
29+
Shape = new RoundRectangle
30+
{
31+
CornerRadius = new CornerRadius(4),
32+
Stroke = Colors.White
33+
}
34+
}, queryAttributes
35+
, CancellationToken.None);
3036
}
3137

3238
async void HandleButtonPopupButtonClicked(object sender, EventArgs e)

samples/CommunityToolkit.Maui.Sample/ViewModels/Views/Popup/CsharpBindingPopupViewModel.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33

44
namespace CommunityToolkit.Maui.Sample.ViewModels.Views;
55

6-
public sealed partial class CsharpBindingPopupViewModel : BaseViewModel
6+
public sealed partial class CsharpBindingPopupViewModel : BaseViewModel, IQueryAttributable
77
{
88
[ObservableProperty]
9-
public partial string Title { get; set; } = "C# Binding Popup";
9+
public partial string Title { get; private set; } = string.Empty;
1010

1111
[ObservableProperty]
12-
public partial string Message { get; set; } = "This message uses a ViewModel binding";
12+
public partial string Message { get; private set; } = string.Empty;
1313

1414
public TaskCompletionSource<IPopupResult>? PopupResultManager { get; set; }
15+
16+
void IQueryAttributable.ApplyQueryAttributes(IDictionary<string, object> query)
17+
{
18+
Title = (string)query[nameof(Title)];
19+
Message = (string)query[nameof(Message)];
20+
}
1521
}

samples/CommunityToolkit.Maui.Sample/ViewModels/Views/Popup/PopupsViewModel.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ public partial class PopupsViewModel(IPopupService popupService) : BaseViewModel
99
[RelayCommand]
1010
void OnCsharpBindingPopup()
1111
{
12-
popupService.ShowPopup<CsharpBindingPopupViewModel>(currentNavigation);
12+
var queryAttributes = new Dictionary<string, object>
13+
{
14+
[nameof(CsharpBindingPopupViewModel.Title)] = "C# Binding Popup",
15+
[nameof(CsharpBindingPopupViewModel.Message)] = "This message uses a ViewModel binding that was set using IQueryAttributable"
16+
};
17+
popupService.ShowPopup<CsharpBindingPopupViewModel>(Shell.Current, null, queryAttributes);
1318
}
1419

1520
[RelayCommand]

samples/CommunityToolkit.Maui.Sample/Views/Popups/SimplePopup.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
<Label Style="{StaticResource Title}" Text="Simple Popup"/>
3535
<BoxView Style="{StaticResource Divider}" />
36-
<Label Style="{StaticResource Content}" Text="This is a platform specific popup with a .NET MAUI View being rendered. " />
36+
<Label x:Name="DescriptionLabel" Style="{StaticResource Content}" HorizontalOptions="Center"/>
3737
<Image Source="shield.png"
3838
Margin="0,10,0,10"
3939
WidthRequest="30"
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
namespace CommunityToolkit.Maui.Sample.Views.Popups;
22

3-
public partial class SimplePopup : ContentView
3+
public partial class SimplePopup : ContentView, IQueryAttributable
44
{
55
public SimplePopup()
66
{
77
InitializeComponent();
88
}
9+
10+
void IQueryAttributable.ApplyQueryAttributes(IDictionary<string, object> query)
11+
{
12+
DescriptionLabel.Text = (string)query[nameof(DescriptionLabel)];
13+
}
914
}
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
namespace CommunityToolkit.Maui.Core;
22

33
/// <inheritdoc/>
4-
public interface IPopupResult<T> : IPopupResult
4+
public interface IPopupResult<out TResult> : IPopupResult
55
{
66
/// <summary>
7-
/// PopupResult
7+
/// The result returned when the popup is closed programmatically.
88
/// </summary>
9-
T? Result { get; }
9+
/// <remarks>
10+
/// Make sure to check the <see cref="IPopupResult.WasDismissedByTappingOutsideOfPopup"/> value to determine how the popup was closed.
11+
/// This will always return <c>null</c> when <see cref="IPopupResult.WasDismissedByTappingOutsideOfPopup"/> is <c>true</c>.
12+
/// </remarks>
13+
TResult? Result { get; }
1014
}
1115

1216
/// <summary>
13-
/// Represents the result of a popup.
17+
/// Represents a result that can be returned when a popup is closed.
1418
/// </summary>
19+
/// <remarks>
20+
/// Make sure to check the <see cref="IPopupResult.WasDismissedByTappingOutsideOfPopup"/> value to determine how the popup was closed.
21+
/// </remarks>
1522
public interface IPopupResult
1623
{
1724
/// <summary>
18-
/// True if Popup is closed by tapping outside the popup
25+
/// Gets whether the popup was closed by tapping outside the popup.
1926
/// </summary>
2027
bool WasDismissedByTappingOutsideOfPopup { get; }
2128
}

src/CommunityToolkit.Maui.UnitTests/BaseHandlerTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ static void InitializeServicesAndSetMockApplication(out IServiceProvider service
8585
#endregion
8686

8787
var mauiApp = appBuilder.Build();
88+
serviceProvider = mauiApp.Services;
8889

8990
var application = (MockApplication)mauiApp.Services.GetRequiredService<IApplication>();
9091
application.AddWindow(new Window { Page = page });
91-
serviceProvider = mauiApp.Services;
9292

9393
IPlatformApplication.Current = application;
9494

0 commit comments

Comments
 (0)