Forum Xamarin Xamarin.Forms

Do static resources such as styles load late/last slowly - or depend on an OS subsystem?

ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

Am a loss and out of my depth on something I'm seeing.

The app in question is set as the Launcher when run in Android. Thus when the tablet powers up the app launches automatically and is being presented pretty much as a single-purpose or embedded device.

On power-up the app launches, splash screen appears... then crashes when the first ContentPage is presented due to static resource style 'LabelStyleReg` not found. In other words, it couldn't find the style defined in the app.xaml and apply it to a label on that first page.

Since the app crashed the launcher is then launched - which is the app. On this second launch, everything is fine. What the user sees is the splash screen, then the splash screen, then the app. If you're not looking closely you don't even see the splash screen drop and start again at the crash; which is good from that perspective.

What I don't grasp is how the app can fail to find the style on the first launch directly after device power-on, but if fine every time thereafter.

It doesn't happen when the app is not the android Launcher. If you boot the device then launch the app its fine. Which makes me feel like there is some android OS mechanism that hasn't loaded yet, that Xamarin.Forms relies on to support these resources... or key/value pairs... or the mono framework itself isn't loaded yet perhaps?

I hate to try hacky things like add a 3 second delay to the boot time etc. I'd rather know the cause and be able to wait for a specific event to happen if that's what I need to do.

Anyone?

Yes the style does exist:

I've annotated the key step in the launch squence of this error log.

Best Answer

  • NMackayNMackay GBInsider, University admin
    Accepted Answer

    @RodrigoJuarez.1796

    Hi,

    Please open a new thread with your issue, don't open old threads as much has changed in Forms since 2017.

Answers

  • BoguslawBlonskiBoguslawBlonski PLMember ✭✭✭
    edited June 2017

    How are You adding resource? LabelStyleMed and RegularFontSize ?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @BoguslawBlonski said:
    How are You adding resource?

    Its a style in the app.xaml

    <Application  blahblah>
       <Application.Resources>
          <ResourceDictionary>
                <Style x:Key="LabelStyleReg"
                       BasedOn="{StaticResource LabelStyleMed}"
                       TargetType="Label">
                    <Setter Property="FontSize" Value="{StaticResource RegularFontSize}" />
                </Style>
       </Application.Resources>
    </Application  blahblah>
    
  • BoguslawBlonskiBoguslawBlonski PLMember ✭✭✭

    How are You adding ?
    BasedOn="{StaticResource LabelStyleMed}"
    Value="{StaticResource RegularFontSize}"

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I can't say it any other way... They are defined in the App.xaml
    If you're looking for an article on how to create app-scoped styles here's a tutorial.

  • BoguslawBlonskiBoguslawBlonski PLMember ✭✭✭

    You could try add it in App.cs via source code

    Device.OnPlatform(
    Android: () =>
    {
    //Application.Current.Resources.Add("RegularFontSize", Device.GetNamedSize(NamedSize.Large, typeof(Label)));
    Application.Current.Resources.Add("RegularFontSize", Statics.FontSizes._100PercentOfMicro);
    } iOS: () =>
    {
    }

  • BoguslawBlonskiBoguslawBlonski PLMember ✭✭✭

    Ok I my eyes you got bug in thing what i asked, if not sorry no other clue.

    https://en.wikipedia.org/wiki/Rubber_duck_debugging

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I don't think you're grasping the issue or the setup. But I appreciate the help.
    There is no need to "add" anything. Its already part of the app.
    App.xaml
    App.xaml.cs
    Are the App class - styles defined in the App.xaml are already part of the App class
    You're referring to App.cs because you don't have a App.Xaml/App.Xaml.cs structure and that fine. Not everyone is XAML-centric. But when you have these things in XAML the structure is not just App.cs but App.xaml and App.xaml.cs

    The problem seems to be that the resources are being loaded slowly by Xamarin.Forms - after the app is up and running - and thus not available fast enough for the first ContentPage to use ..... But only when the app is started immediately upon power on as the Android Launcher.

  • NMackayNMackay GBInsider, University admin
    edited June 2017

    That's a weird one. Are the font sizes the very 1st thing you define in your App.xaml?

    I do colors
    Fonts
    Font sizes

    and eventually styles.

      <prism:PrismApplication.Resources>
        <ResourceDictionary>
    
          <!-- Colors -->
          <Color x:Key="DarkThemeBlue">#196b90</Color>
    
          <!-- Fonts -->
          <OnPlatform x:Key="FontFamilyRegular" x:TypeArguments="x:String">
            <On Platform="iOS">SFCompactDisplay-Regular</On>
            <On Platform="Android">sans-serif</On>
            <On Platform="Windows">Segoe UI</On>
          </OnPlatform>
    
          <!-- Font Sizes -->
          <OnPlatform x:Key="EntryLabelSize" x:TypeArguments="x:Double">
            <On Platform="iOS">
              <OnIdiom x:TypeArguments="x:Double" Phone="12" Tablet="14" />
            </On>
            <On Platform="Android,Windows">
              <OnIdiom x:TypeArguments="x:Double" Phone="14" Tablet="16" />
            </On>
          </OnPlatform>
    
    
          <OnPlatform x:Key="InfoTitleFontSize" x:TypeArguments="x:Double">
            <On Platform="iOS">10</On>
            <On Platform="Android,Windows">12</On>
          </OnPlatform>
    

    I'm just wondering is playing about with the order of your app.xaml will make the problem go away.....

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @NMackay said:
    That's a weird one. Are the font sizes the very 1st thing you define in your App.xaml?

    I do colors
    Fonts
    Font sizes

    and eventually styles.

    My does that sound familiar. Are you sure you're not working here?

  • BoguslawBlonskiBoguslawBlonski PLMember ✭✭✭

    @ClintStLaurent said:
    I don't think you're grasping the issue or the setup. But I appreciate the help.
    There is no need to "add" anything. Its already part of the app.
    App.xaml
    App.xaml.cs
    Are the App class - styles defined in the App.xaml are already part of the App class
    You're referring to App.cs because you don't have a App.Xaml/App.Xaml.cs structure and that fine. Not everyone is XAML-centric. But when you have these things in XAML the structure is not just App.cs but App.xaml and App.xaml.cs

    The problem seems to be that the resources are being loaded slowly by Xamarin.Forms - after the app is up and running - and thus not available fast enough for the first ContentPage to use ..... But only when the app is started immediately upon power on as the Android Launcher.

    Xaml is slow as You said.

    Have You tried doing it from code?

  • RodrigoJuarez.1796RodrigoJuarez.1796 USMember ✭✭
    edited October 2019

    I know this is from a long time ago, but were you able to find some fix to this?
    I have a color in my application resources created like:

    <Color x:Key="PrimaryColor">#760d3a</Color>
    

    If I try to use it as a static resource, it is not working when the app starts but looks good if I make a change using hot reload. If I use it as a dynamic resource, it is working from the start.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    As you said, this is from quite some time back: June 2 years ago
    This problem hasn't come up for us in well over a year. So I have to assume it was a problem with the tooling back then.
    Several new versions of Xamarin forms.
    Xamarin itself is no longer a 3rd party extension but built in and better supported.

    So let me ask you this:Are you fully up to date?
    Does this happen with a new app made from current solution template?

    Sorry I couldn't be more help.

  • NMackayNMackay GBInsider, University admin
    Accepted Answer

    @RodrigoJuarez.1796

    Hi,

    Please open a new thread with your issue, don't open old threads as much has changed in Forms since 2017.

This discussion has been closed.