Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] HotReload does not reload CSS from linked embedded files #10299

Open
tomeverin opened this issue Apr 11, 2020 · 2 comments
Open

[Bug] HotReload does not reload CSS from linked embedded files #10299

tomeverin opened this issue Apr 11, 2020 · 2 comments
Labels
a/CSS external-hotreload Non-Forms bugs that affect Hot Reload s/unverified New report that has yet to be verified t/bug 🐛
Projects

Comments

@tomeverin
Copy link
Contributor

tomeverin commented Apr 11, 2020

Description

When updating xaml via hot reload, any CSS styling that is a result of linked style sheets (embedded .css resource) is lost.

Steps to Reproduce

  1. Any xamarin forms project
  2. Style some visual elements by linking to an embedded .css
  3. Link it into XAML at ContentPage.Resources with:
    <StyleSheet Source="/Embedded/globalstyle.css" />

Expected Behavior

On hot reload, the linked styles are still applied

Actual Behavior

Linked style sheet styles are no longer applied until next deploy and run

Basic Information

  • Version with issue: 4.5.0.495
  • IDE: Visual Studio Community 16.5.3

VS bug #1101668

@tomeverin tomeverin added s/unverified New report that has yet to be verified t/bug 🐛 labels Apr 11, 2020
@pauldipietro pauldipietro added this to New in Triage Apr 11, 2020
@jsuarezruiz jsuarezruiz added external-hotreload Non-Forms bugs that affect Hot Reload a/CSS labels Apr 13, 2020
@samhouts samhouts moved this from New to External in Triage Apr 17, 2020
@ChrisMissal
Copy link

Does anybody know if there any current workarounds for this?

@chucker
Copy link
Contributor

chucker commented May 28, 2021

Does anybody know if there any current workarounds for this?

Sort of.

Let's say you use an app-wide stylesheet and currently apply it in the App.xaml as follows:

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="YourProjectName.App" PageAppearing="Application_PageAppearing">
    <Application.Resources>
        <StyleSheet Source="AppStylesheet.css" x:Key="Stylesheet" />
    </Application.Resources>
</Application>

Remove that resource. Instead, in your App.xaml.cs's App() constructor, underneath MainPage = new MainPage() (or similar), add:

            new System.Threading.Timer(_ =>
            {
                Dispatcher.BeginInvokeOnMainThread(() =>
                {
                    var prop = this.Resources.GetType().GetProperty("StyleSheets", BindingFlags.NonPublic | BindingFlags.Instance);
                    prop.SetValue(this.Resources, null);

                    using (var sr = new StreamReader("../../../../../../YourProjectName/AppStylesheet.css"))
                        this.Resources.Add(Xamarin.Forms.StyleSheets.StyleSheet.FromReader(sr));
                });
            }, null, 1_000, 1_000);

(I'll leave figuring out the correct relative path to the style sheet as an exercise to the reader.)

What this does, once a second, is two things:

  • it loads the style sheet and adds it as a resource
  • critically, before that, it resets the resource dictionary's internal StyleSheets collection to null

If you now open that style sheet in the IDE and, say, change something like a background-color property, you'll see an effect within at most a second. (Tested on macOS and iOS targets.) Other properties like font-size I've found to not work reliably, but I think this is a problem not specific to this bug.

The above works because, if StyleSheets is null, that initializes the internals of style sheet management. There are hints that this was supposed to be more dynamic; e.g. there's a collection changed event handler (but it doesn't seem to get attached to anything).

chucker added a commit to chucker/AltNetworkUtility that referenced this issue May 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/CSS external-hotreload Non-Forms bugs that affect Hot Reload s/unverified New report that has yet to be verified t/bug 🐛
Projects
Triage
  
External
Development

No branches or pull requests

4 participants