Skip to content

MarkupExtension instance is not properly kept alive #14502

Open
@Xiaoy312

Description

@Xiaoy312

Current behavior

Currently on uno, MarkupExtension is used like a oneshot value-provider:

xaml: <TextBlock Text="{local:TestAsdExtensions}" />
generated:
new global::Microsoft.UI.Xaml.Controls.TextBlock { IsParsing = true, /*...*/ }
.GenericApply(__that, __nameScope, ((c0, __that, __nameScope) =>
{
	c0.Text = (string)Convert.ChangeType(
		(((global::Microsoft.UI.Xaml.Markup.IMarkupExtensionOverrides)new global::MarkupExtLifetime.TestAsdExtensions())
			.ProvideValue(global::Uno.UI.Helpers.MarkupHelper.CreateParserContext(
				c0, 
				typeof(global::Microsoft.UI.Xaml.Controls.TextBlock), 
				"Text", 
				typeof(object)
		)))?.ToString(), 
		typeof(string)
	);
	//...
}))

the instance dies immediately

Expected behavior

the markup instance should be kept alive for at least the duration of its owner's lifetime.

How to reproduce it (as minimally and precisely as possible)

MarkupExtLifetime.zip

  1. click on "Test" button
  2. notice the text is updated
    ^ different result when comparing uno to windows

Workaround

keep a hard reference of markup-instance, and clear that when the owning target is removed from the visual-tree

Works on UWP/WinUI

Yes

Environment

Uno.WinUI / Uno.WinUI.WebAssembly / Uno.WinUI.Skia

NuGet package version(s)

[email protected]

Affected platforms

WebAssembly, Android, iOS, Skia (WPF)

IDE

No response

IDE version

No response

Relevant plugins

No response

Anything else we need to know?

on windows, it is unclear yet what the exact lifetime would be, but it seems to even outlive the TextBlock?
for context, it lived past this:

if (Content is StackPanel sp &&
	sp.Children.FirstOrDefault(x => x is TextBlock) is TextBlock tb)
{
	sp.Children.Remove(tb);

	GC.Collect();
	GC.Collect();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/code-generationCategorizes an issue or PR as relevant to code generationdifficulty/starter 🚀Categorizes an issue for which the difficulty level is reachable by newcomerskind/bugSomething isn't workingproject/binding 🪢Categorizes an issue or PR as relevant to the binding engine

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions