maui icon indicating copy to clipboard operation
maui copied to clipboard

GetTemplateChild returns null when entry not in layout (e.g. VerticalStackLayout)

Open vsfeedback opened this issue 1 year ago • 1 comments

This issue has been moved from a ticket on Developer Community.


When a custom control has a control template with only the entry, the function GetTemplateChild returns null and that results in a null reference exception. When the control template holds a VerticalStackLayout with the same entry the function GetTemplateChild returns the entry.

This does not work:

	<ContentView.ControlTemplate>
		<ControlTemplate>
			<ctrls:CustomEntry x:Name="entry"
							   InputInfo="{TemplateBinding NumberInfo}" />
		</ControlTemplate>
	</ContentView.ControlTemplate>

This works:

	<ContentView.ControlTemplate>
		<ControlTemplate>
			<VerticalStackLayout>
				<ctrls:CustomEntry x:Name="entry"
								   InputInfo="{TemplateBinding NumberInfo}" />
			</VerticalStackLayout>
		</ControlTemplate>
	</ContentView.ControlTemplate>

This code returns null in the first case:

	protected override void OnApplyTemplate()
	{
		base.OnApplyTemplate();
		entry = (CustomEntry)GetTemplateChild("entry");
	}

To make it very clear, all I have changed is add the VerticalStackLayout to solve the null reference exception. All the other code has not been changed. I do not like adding layout elements without any function. I think this is a bug.


Original Comments

Lisa Gao (CSI Interfusion Inc) [MSFT] on 6/16/2024, 07:25 PM:

(private comment, text removed)

saskia on 6/16/2024, 10:42 PM:

(private comment, text removed)

saskia on 6/16/2024, 11:47 PM:

(private comment, text removed)

saskia on 6/17/2024, 00:01 AM:

(private comment, text removed)

saskia on 6/17/2024, 06:58 AM:

(private comment, text removed)

Feedback Bot on 6/19/2024, 10:13 PM:

(private comment, text removed)


Original Solutions

saskia solved on 6/17/2024, 06:57 AM, 0 votes:

When I change the customentry to a contentview with controltemplate instead of overriding the entry control, the problem is solved.
And I can derive the numberentry directly from the customentry with the binding being propagated.
See the changed example project.
MauiAppForBugSolved.zip

vsfeedback avatar Jun 25 '24 22:06 vsfeedback

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

github-actions[bot] avatar Jun 25 '24 22:06 github-actions[bot]