Dynamic resources are not applied issue for the layouts in .NET MAUI
Description
When attempting to use dynamic resource binding to set properties of a layout that is a child of the ContentView, an issue arises. The problem is that the parent reference of the layout becomes null.
Below code snippet is used to reproduce the issue.
Code
public class DemoClass : View, IContentView, IVisualTreeElement
{
private Grid grid;
public DemoClass()
{
this.grid = new Grid();
Label label = new Label();
label.VerticalOptions = LayoutOptions.Center;
label.HorizontalOptions = LayoutOptions.Center;
label.Text = "Dynamic resource";
label.SetDynamicResource(Label.TextColorProperty, "SomeColor");
this.grid.Children.Add(label);
this.grid.parent = this;
}
object IContentView.Content
{
get
{
return grid;
}
}
IView IContentView.PresentedContent
{
get
{
return grid;
}
}
IReadOnlyList<IVisualTreeElement> IVisualTreeElement.GetVisualChildren()
{
if (this.grid != null)
{
return new List<IVisualTreeElement>() { grid };
}
return new List<IVisualTreeElement>();
}
/// <summary>
/// Gets the content padding.
/// </summary>
Thickness IPadding.Padding
{
get
{
return new Thickness(0);
}
}
public Size CrossPlatformArrange(Rect bounds)
{
this.ArrangeContent(bounds);
return bounds.Size;
}
public Size CrossPlatformMeasure(double widthConstraint, double heightConstraint)
{
return this.MeasureContent(widthConstraint, heightConstraint);
}
}
[GaugeDynamicBinding.zip](https://github.com/dotnet/maui/files/12124858/GaugeDynamicBinding.zip)
Steps to Reproduce
1 Create an instance of the DemoClass in the application. 2. Use the DemoClass instance to set up a ContentView that contains the child layout, e.g., a Grid. 3. Attempt to apply dynamic resource binding on properties of the child layout (e.g., Grid) from within the DemoClass.
Link to public reproduction project repository
Version with bug
7.0.49
Last version that worked well
Unknown/Other
Affected platforms
Windows
Affected platform versions
Windows : Windows 10 Enterprise 10.0.19044
Did you find any workaround?
No response
Relevant log output
No response
We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.
The code here might help you out in NET8 https://github.com/dotnet/maui/pull/16046
Verified this issue with Visual Studio Enterprise 17.9.0 Preview 3. Can repro on .NET 7/.NET 8 Android platforms with sample project. DynamicResourceSample.zip