Skip to content

Instantly share code, notes, and snippets.

@axa88
Last active September 7, 2024 18:09
Show Gist options
  • Save axa88/fe2c31ba1ec7fc12730bc4b826313e1b to your computer and use it in GitHub Desktop.
Save axa88/fe2c31ba1ec7fc12730bc4b826313e1b to your computer and use it in GitHub Desktop.
Preproducing Microsoft.Maui.Controls.Element: Warning: The RealParent on Microsoft.Maui.Controls.*** has been Garbage Collected....
Warnings:
- Microsoft.Maui.Controls.Element: Warning: CommonMaui.Presentation.Views.Diagnostics.DiagnosticLabel is already a child of Microsoft.Maui.Controls.VerticalStackLayout. Remove CommonMaui.Presentation.Views.Diagnostics.DiagnosticLabel from Microsoft.Maui.Controls.VerticalStackLayout before adding to Microsoft.Maui.Controls.VerticalStackLayout.
Followed by:
- Microsoft.Maui.Controls.Element: Warning: The RealParent on CommonMaui.Presentation.Views.Diagnostics.DiagnosticLabel has been Garbage Collected. This should never happen. Please log a bug: https://github.com/dotnet/maui
In my scenario I have a custom Label:
public DiagnosticLabel(string path)
{
var target = path.Split('.').Last();
Span prefix = new() { Text = target, TextColor = Colors.Blue, FontAttributes = FontAttributes.Bold };
Span delimiter = new() { Text = " : " };
Span data = new() { TextColor = Colors.Wheat };
data.SetBinding(targetProperty: Span.TextProperty, binding: new Binding(path: path));
FormattedText = new() { Spans = { prefix, delimiter, data } };
}
Which is in turn used in a custom Data Template:
public class BleDeviceDiagnosticItemDataTemplate : DataTemplate
{
public BleDeviceDiagnosticItemDataTemplate()
{
LoadTemplate = static () =>
{
DiagnosticLabel idLabel = new(nameof(BleDevice.Id));
ScrollView scrollView = new() { Content = new VerticalStackLayout { Children = { idLabel } };
var copy = new VerticalStackLayout { Children = { idLabel } }; // creating this, even when unused causes Warning
return scrollView;
};
}
}
**** Note: the variable "copy" even while unused, will causes the Warning ****, ie. Remove it and there is no Warning...
The above is used in what I call a View Selector:
public class BleDiagnosticsViewSelector
{
public View LoadView(object bindingContext)
{
return bindingContext switch
{
null => ControlTemplateLoadTemplate<NoDeviceDataTemplate>(),
BleDeviceItem => ControlTemplateLoadTemplate<BleDeviceDiagnosticItemDataTemplate>(),
_ => throw new ArgumentOutOfRangeException(nameof(bindingContext), bindingContext, null)
};
}
private static View ControlTemplateLoadTemplate<TDt>() where TDt : DataTemplate, new() => (View)new ContentView { ControlTemplate = new() { LoadTemplate = new TDt().LoadTemplate } }.ControlTemplate.LoadTemplate();
}
Which in turn is used by a custom ContentPage in order to populate and repopulate the Page with different Content:
internal class BleBasicDeviceDetailsPage : ContentPage
{
private readonly BleDiagnosticsViewSelector _viewSelector = new();
internal void SetContent(BleDeviceItem bleDeviceItem) => Content = _viewSelector.LoadView(someBindingContextForDataTemplate);
}
And at some point when this Content is set, or set agian... the warning will spawn AS LONG AS A CONTROL VARIABLE WITH THE CUSTOM LABEL IS MERELY CREATED as indicated above
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment