diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue29930.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue29930.cs new file mode 100644 index 000000000000..98a55a9719ee --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue29930.cs @@ -0,0 +1,62 @@ +namespace Maui.Controls.Sample.Issues; + +[Issue(IssueTracker.Github, 29930, "[Windows] Setting a ContentView with a content of StaticResource Style Causes a System.Runtime.InteropServices.COMException.", PlatformAffected.UWP)] +public class Issue29930 : ContentPage +{ + ContentView _mainContentView; + public Issue29930() + { + var button = new Button + { + Text = "Add ContentView", + HorizontalOptions = LayoutOptions.Center, + AutomationId = "ChangeInnerContent" + }; + button.Clicked += OnButtonClicked; + + _mainContentView = new ContentView(); + _mainContentView.Content = new Issue29930InnerContentView(); + + var layout = new VerticalStackLayout + { + Spacing = 25, + Padding = new Thickness(30, 0), + VerticalOptions = LayoutOptions.Center, + Children = + { + button, + _mainContentView + } + }; + + Content = layout; + } + private void OnButtonClicked(object sender, EventArgs e) + { + _mainContentView.Content = new Issue29930InnerContentView() { BackgroundColor = Colors.Red }; + } +} + +public class Issue29930InnerContentView : ContentView +{ + public Issue29930InnerContentView() + { + Application.Current.Resources ??= new ResourceDictionary(); + + if (!Application.Current.Resources.ContainsKey("SubContentStyle")) + { + Application.Current.Resources["SubContentStyle"] = new Style(typeof(ContentView)) + { + Setters = + { + new Setter + { + Property = ContentView.ContentProperty, + Value = new Label { Text = "SubContent" } + } + } + }; + } + Style = (Style)Application.Current.Resources["SubContentStyle"]; + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue29930.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue29930.cs new file mode 100644 index 000000000000..a8d8e4bd49c8 --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue29930.cs @@ -0,0 +1,23 @@ +#if TEST_FAILS_ON_ANDROID // Test fails on Android , see https://github.com/dotnet/maui/issues/11812 +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues; + +public class Issue29930 : _IssuesUITest +{ + public Issue29930(TestDevice device) : base(device) { } + + public override string Issue => "[Windows] Setting a ContentView with a content of StaticResource Style Causes a System.Runtime.InteropServices.COMException."; + + [Test] + [Category(UITestCategories.Border)] + public void InnerContentViewShouldNotCrashWhenDynamicallyChange() + { + App.WaitForElement("ChangeInnerContent"); + App.Tap("ChangeInnerContent"); + App.WaitForElement("ChangeInnerContent"); + } +} +#endif \ No newline at end of file diff --git a/src/Core/src/Handlers/Border/BorderHandler.Windows.cs b/src/Core/src/Handlers/Border/BorderHandler.Windows.cs index 62d60f019438..d9a32ce0caa0 100644 --- a/src/Core/src/Handlers/Border/BorderHandler.Windows.cs +++ b/src/Core/src/Handlers/Border/BorderHandler.Windows.cs @@ -24,7 +24,12 @@ static partial void UpdateContent(IBorderHandler handler) handler.PlatformView.EnsureBorderPath(); if (handler.VirtualView.PresentedContent is IView view) + { + // Detach the old handler if it exists (prevents WinUI COM exception on reuse) + view.Handler?.DisconnectHandler(); handler.PlatformView.Content = view.ToPlatform(handler.MauiContext); + } + } protected override ContentPanel CreatePlatformView() diff --git a/src/Core/src/Handlers/ContentView/ContentViewHandler.Windows.cs b/src/Core/src/Handlers/ContentView/ContentViewHandler.Windows.cs index 685bd71fe7e6..1de8cc154b89 100644 --- a/src/Core/src/Handlers/ContentView/ContentViewHandler.Windows.cs +++ b/src/Core/src/Handlers/ContentView/ContentViewHandler.Windows.cs @@ -24,6 +24,8 @@ static void UpdateContent(IContentViewHandler handler) if (handler.VirtualView.PresentedContent is IView view) { + // Detach the old handler if it exists (prevents WinUI COM exception on reuse) + view.Handler?.DisconnectHandler(); handler.PlatformView.CachedChildren.Add(view.ToPlatform(handler.MauiContext)); } }