Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DynamicResource not working for inner views #3745

Closed
MuneeshKumarG opened this issue Dec 13, 2021 · 16 comments · Fixed by #16046
Closed

DynamicResource not working for inner views #3745

MuneeshKumarG opened this issue Dec 13, 2021 · 16 comments · Fixed by #16046
Assignees
Labels
area-xaml XAML, CSS, Triggers, Behaviors fixed-in-8.0.0-preview.7.8842 Look for this fix in 8.0.0-preview.7.8842! p/1 Work that is important, and has been scheduled for release in this or an upcoming sprint partner Issue or Request from a partner team platform/android 🤖 s/needs-info Issue needs more info from the author s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working

Comments

@MuneeshKumarG
Copy link

MuneeshKumarG commented Dec 13, 2021

Description

We have created a simple custom control that has multiple inner views. DynamicResource only works for the main custom control view and does not work for inner views.

Find the code for simple custom control implementation. We have implemented IVisualTreeElement also.

 public class ViewExt : View, IContentView, IVisualTreeElement
    {
        public Grid ParentGrid { get; set; }
        public ViewExt()
        {
            ParentGrid = new Grid();
        }
        public View BackgroundContent
        {
            get { return (View)this.GetValue(BackgroundContentProperty); }
            set { this.SetValue(BackgroundContentProperty, value); }
        }

        public static readonly BindableProperty BackgroundContentProperty =
           BindableProperty.Create(nameof(BackgroundContent), typeof(View), typeof(ViewExt), null, propertyChanged: OnBackgroundContentPropertyChanged);

        private static void OnBackgroundContentPropertyChanged(BindableObject bindable, object oldValue, object newValue)
        {
            if(newValue is View view && bindable is ViewExt view1)
            {
                view1.ParentGrid.Children.Add(view);
            }
        }

        IReadOnlyList<IVisualTreeElement> IVisualTreeElement.GetVisualChildren()
        {
            List<Element> _logicalChildren = new();
           
            if (BackgroundContent != null)
                _logicalChildren.Add(BackgroundContent);

            return _logicalChildren.AsReadOnly();
        }

        object IContentView.Content
        {
            get
            {
                return ParentGrid;
            }
        }

        IView? IContentView.PresentedContent
        {
            get
            {
                return ParentGrid;
            }
        }

        Thickness IPadding.Padding
        {
            get
            {
                return new Thickness(0);
            }
        }

        Size IContentView.CrossPlatformMeasure(double widthConstraint, double heightConstraint)
        {
            return this.MeasureContent(widthConstraint, heightConstraint);
        }

        Size IContentView.CrossPlatformArrange(Rectangle bounds)
        {
            this.ArrangeContent(bounds);
            return bounds.Size;
        }
    }
 <VerticalStackLayout x:Name="layout">
        <VerticalStackLayout.Resources>
            <Grid Background="yellow" x:Key="bg">
                <Label Text="Background" HorizontalOptions="Center"/>
            </Grid>
            <Grid Background="red" x:Key="bg1">
                <Label Text="Background" HorizontalOptions="Center"/>
            </Grid>
        </VerticalStackLayout.Resources>

     <!--Not working-->
        <local:ViewExt >
            <local:ViewExt.BackgroundContent>
                <local:ViewExt BackgroundContent="{DynamicResource bg1}"/>
            </local:ViewExt.BackgroundContent>
        </local:ViewExt>
        
        <!--Working-->
        <local:ViewExt BackgroundContent="{DynamicResource bg}"/>

</VerticalStackLayout>

Steps to Reproduce

DynamicResourceSample.zip

  1. Run the sample.
  2. Set the break point in C# OnBackgroundContentPropertyChanged method.
  3. See the break point not hit for inner view BackgroundContent change.
  4. Breakpoint hit only for outer view property change alone.

Version with bug

Preview 10 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 10

Did you find any workaround?

StaticResource working

<local:ViewExt >
local:ViewExt.BackgroundContent
<local:ViewExt BackgroundContent="{StaticResource bg1}"/>
</local:ViewExt.BackgroundContent>
</local:ViewExt>

DynamicResource not working

<local:ViewExt >
local:ViewExt.BackgroundContent
<local:ViewExt BackgroundContent="{DynamicResource bg1}"/>
</local:ViewExt.BackgroundContent>
</local:ViewExt>

Relevant log output

No response

@MuneeshKumarG MuneeshKumarG added the t/bug Something isn't working label Dec 13, 2021
@jsuarezruiz jsuarezruiz added the area-xaml XAML, CSS, Triggers, Behaviors label Dec 13, 2021
@dotMorten
Copy link
Contributor

Just confirming I'm seeing this with Preview 11 as well

@kristinx0211
Copy link

Repro on Version 17.2.0 Preview 2.0 [32216.282.main].

@kristinx0211 kristinx0211 added the s/verified Verified / Reproducible Issue ready for Engineering Triage label Mar 3, 2022
@Redth Redth added this to the 6.0.300-rc.2 milestone Mar 22, 2022
@Redth Redth modified the milestones: 6.0.300-rc.2, 6.0.300-rc.3 Apr 20, 2022
@StephaneDelcroix
Copy link
Contributor

I'm wondering if Bindings are working in this situation...

@Redth Redth modified the milestones: 6.0.300-rc.3, 6.0.300 Apr 27, 2022
@davidortinau davidortinau added the p/0 Work that we can't release without label Apr 28, 2022
@Redth Redth added p/1 Work that is important, and has been scheduled for release in this or an upcoming sprint and removed p/0 Work that we can't release without labels May 3, 2022
@VincentBu
Copy link

Repro with vs main(32507.29.main).

<local:ViewExt >
            <local:ViewExt.BackgroundContent>
                <local:ViewExt BackgroundContent="{DynamicResource bg1}"/>
            </local:ViewExt.BackgroundContent>
</local:ViewExt>

we expect to have red background like this
image

but the background isn't rendered
image

@StephaneDelcroix
Copy link
Contributor

it looks like ParentGrid is never Parented, probably never gets BindingContext applied, and can't walk the tree for DynamicResources propagations

@Redth Redth modified the milestones: 6.0.300, 6.0.300-servicing May 10, 2022
@samhouts samhouts added the partner Issue or Request from a partner team label Jul 20, 2022
@Redth Redth modified the milestones: 6.0-servicing, Backlog Aug 30, 2022
@ghost
Copy link

ghost commented Aug 30, 2022

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@samhouts samhouts modified the milestones: Backlog, .NET 8 Planning Jan 26, 2023
@samhouts samhouts removed the s/verified Verified / Reproducible Issue ready for Engineering Triage label Apr 5, 2023
@jinxinjuan jinxinjuan added the s/triaged Issue has been reviewed label Apr 17, 2023
@QianaJiao
Copy link

Verified this issue still repro on Visual Studio Enterprise 17.6.0 Preview 2.0 with sample project.

@QianaJiao QianaJiao added the s/verified Verified / Reproducible Issue ready for Engineering Triage label Apr 20, 2023
@samhouts samhouts modified the milestones: .NET 8 Planning, .NET 8 May 16, 2023
@gabsamples6
Copy link

This is still an issue in latest visual studio .net 7 and prevents us switching from darkMode to light mode on some controls . Most of the syncfusion controls do not work in dark mode when using DynamicResource . We do not use appthemebinding but we have multiple resource dictionaries .. LightMode -DarkMode etc.. and we swap them at runtime so DynamicResource is critical for this to work...

Any indications if this will be looked at all? Many thanks

@Ahamed-Ali
Copy link
Contributor

Any update on this?

@samhouts
Copy link
Member

I believe that this will be fixed in NET 8 if you use #16046. It should set the Parent and the BindingContext if you use AddLogicalChild. Are you able to test with 8.0 preview 7?

@samhouts samhouts added the fixed-in-8.0.0-preview.7.8842 Look for this fix in 8.0.0-preview.7.8842! label Sep 12, 2023
@MattePozzy
Copy link

MattePozzy commented Jan 15, 2024

I believe that this will be fixed in NET 8 if you use #16046. It should set the Parent and the BindingContext if you use AddLogicalChild. Are you able to test with 8.0 preview 7?

Still an issue on net 8.0.1 and VS 17.8.4.
image
image

@Redth
Copy link
Member

Redth commented Feb 9, 2024

I looked at your repro...

It's still net6.0 based, so just checking, even though you've installed net8 and vs 17.8.4, is your project targeting net8.0? The fixes/changes will be available only when targeting net8.0.

After I took your repro and put it into a net8.0 project, I could see the issue happening, however what I did was this:

  1. Remove IVisualTreeElement interface
  2. Remove the implemented members for that interface
  3. Instead, use this.AddLogicalChild(ParentGrid) that was suggested, in your ViewExt() ctor (this Parent Grid was not in the logical tree, and you're adding the background content view to that element).

After this, the content shows up as expected here.

@gabsamples6
Copy link

Hi @Redth what about this one as there is quite a bit of interests and is getting no love...
Dynamic resources not working with gradients
#18545 (comment)

@Redth Redth added the s/needs-info Issue needs more info from the author label Feb 13, 2024
@ghost
Copy link

ghost commented Feb 13, 2024

Hi @MuneeshKumarG. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@ghost ghost added the s/no-recent-activity Issue has had no recent activity label Feb 19, 2024
@ghost
Copy link

ghost commented Feb 19, 2024

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

@ghost ghost closed this as completed Feb 22, 2024
@Ahamed-Ali
Copy link
Contributor

Hi @Redth ,

We have developed a custom control inherited from the entry and have encountered a dynamic resource issue when updating the text color from another class, although it functions correctly with static resources. Please refer to the GitHub link for more details: Issue with the usage of DynamicResource for inner views of a custom control

Kindly review the sample provided in the repository linked in the above github link. This is a high-priority issue, and we are urgently seeking either a workaround or a prompt fix. Additionally, a video has been attached for your reference.

@ghost ghost removed the s/no-recent-activity Issue has had no recent activity label Mar 4, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Apr 4, 2024
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-xaml XAML, CSS, Triggers, Behaviors fixed-in-8.0.0-preview.7.8842 Look for this fix in 8.0.0-preview.7.8842! p/1 Work that is important, and has been scheduled for release in this or an upcoming sprint partner Issue or Request from a partner team platform/android 🤖 s/needs-info Issue needs more info from the author s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.