From 55f4632ad8d35034d9eaee515ef70b14da1270b7 Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Wed, 8 Jan 2025 15:36:50 -0600 Subject: [PATCH] Fix form submits, once and for all (#284) if you don't hang onto a reference to the RenderedAdaptiveCard, then the GC might clean it up sometime, even while the card is in the UI tree. If this gets GC'd, then it'll revoke our Action handler, and the form will do seemingly nothing. --- .../Microsoft.CmdPal.UI/Controls/FormControl.xaml.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/FormControl.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/FormControl.xaml.cs index 0cfe02cd8b..9cba332050 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/FormControl.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/FormControl.xaml.cs @@ -14,6 +14,12 @@ public sealed partial class FormControl : UserControl private static readonly AdaptiveCardRenderer _renderer; private FormViewModel? _viewModel; + // LOAD-BEARING: if you don't hang onto a reference to the RenderedAdaptiveCard + // then the GC might clean it up sometime, even while the card is in the UI + // tree. If this gets GC'd, then it'll revoke our Action handler, and the + // form will do seemingly nothing. + private RenderedAdaptiveCard? _renderedCard; + public FormViewModel? ViewModel { get => _viewModel; set => AttachViewModel(value); } static FormControl() @@ -71,10 +77,10 @@ public sealed partial class FormControl : UserControl private void DisplayCard(AdaptiveCardParseResult result) { - var rendered = _renderer.RenderAdaptiveCard(result.AdaptiveCard); - rendered.Action += Rendered_Action; + _renderedCard = _renderer.RenderAdaptiveCard(result.AdaptiveCard); ContentGrid.Children.Clear(); - ContentGrid.Children.Add(rendered.FrameworkElement); + ContentGrid.Children.Add(_renderedCard.FrameworkElement); + _renderedCard.Action += Rendered_Action; } private void Rendered_Action(RenderedAdaptiveCard sender, AdaptiveActionEventArgs args) =>