diff --git a/src/CommunityToolkit.Maui.Core/Interfaces/IPopup.shared.cs b/src/CommunityToolkit.Maui.Core/Interfaces/IPopup.shared.cs index e515df40c..228f5d58b 100644 --- a/src/CommunityToolkit.Maui.Core/Interfaces/IPopup.shared.cs +++ b/src/CommunityToolkit.Maui.Core/Interfaces/IPopup.shared.cs @@ -6,7 +6,7 @@ namespace CommunityToolkit.Maui.Core; /// /// Represents a small View that pops up at front the Page. /// -public interface IPopup : IElement +public interface IPopup : IElement, IVisualTreeElement { /// /// Gets the View that Popup will be anchored. diff --git a/src/CommunityToolkit.Maui/HandlerImplementation/Popup/Popup.macios.cs b/src/CommunityToolkit.Maui/HandlerImplementation/Popup/Popup.macios.cs index 9c9610513..77dcd0a4d 100644 --- a/src/CommunityToolkit.Maui/HandlerImplementation/Popup/Popup.macios.cs +++ b/src/CommunityToolkit.Maui/HandlerImplementation/Popup/Popup.macios.cs @@ -25,8 +25,7 @@ static PageHandler CreatePageHandler(IPopup virtualView) var view = (View?)virtualView.Content ?? throw new InvalidOperationException($"{nameof(IPopup.Content)} can't be null here."); var contentPage = new ContentPage { - Content = view, - Parent = Application.Current?.MainPage + Content = view }; contentPage.SetBinding(BindingContextProperty, new Binding { Source = virtualView, Path = BindingContextProperty.PropertyName }); diff --git a/src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs b/src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs index 8e9a13e4d..43067da4a 100644 --- a/src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs +++ b/src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs @@ -1,4 +1,5 @@ using CommunityToolkit.Maui.Core; +using Microsoft.Maui.Controls.Internals; using LayoutAlignment = Microsoft.Maui.Primitives.LayoutAlignment; namespace CommunityToolkit.Maui.Views; @@ -7,7 +8,7 @@ namespace CommunityToolkit.Maui.Views; /// Represents a small View that pops up at front the Page. Implements . /// [ContentProperty(nameof(Content))] -public partial class Popup : Element, IPopup +public partial class Popup : Element, IPopup, IWindowController, IPropertyPropagationController { /// /// Backing BindableProperty for the property. @@ -44,6 +45,7 @@ public partial class Popup : Element, IPopup readonly Lazy> platformConfigurationRegistry; TaskCompletionSource taskCompletionSource = new(); + Window? window; /// /// Instantiates a new instance of . @@ -53,7 +55,6 @@ public Popup() platformConfigurationRegistry = new Lazy>(() => new(this)); VerticalOptions = HorizontalOptions = LayoutAlignment.Center; - #if WINDOWS this.HandlerChanged += OnPopupHandlerChanged; #endif @@ -163,6 +164,23 @@ public bool CanBeDismissedByTappingOutsideOfPopup /// public View? Anchor { get; set; } + /// + /// Property that represents the Window that's showing the Popup. + /// + public Window? Window + { + get => window; + set + { + window = value; + + if (Content is IWindowController controller) + { + controller.Window = value; + } + } + } + /// /// Gets or sets the result that will return when user taps outside of the Popup. /// @@ -174,6 +192,7 @@ public bool CanBeDismissedByTappingOutsideOfPopup /// IView? IPopup.Content => Content; + /// /// Resets the Popup. /// @@ -231,6 +250,7 @@ protected override void OnBindingContextChanged() if (Content is not null) { SetInheritedBindingContext(Content, BindingContext); + Content.Parent = this; } } @@ -250,4 +270,12 @@ static void OnColorChanged(BindableObject bindable, object oldValue, object newV void IPopup.OnOpened() => OnOpened(); void IPopup.OnDismissedByTappingOutsideOfPopup() => OnDismissedByTappingOutsideOfPopup(); + + void IPropertyPropagationController.PropagatePropertyChanged(string propertyName) => + PropertyPropagationExtensions.PropagatePropertyChanged(propertyName, this, ((IVisualTreeElement)this).GetVisualChildren()); + + IReadOnlyList IVisualTreeElement.GetVisualChildren() => + Content is null + ? Enumerable.Empty().ToList() + : new List { Content }; } \ No newline at end of file