powerapps

How to use a Global Variable within an Object


I'm trying to use variables to make it easier to switch parameters when I need to in Power Apps. PowerApps doesn't use style sheets, so I read that if I use the OnStart feature on the App and add in a variable list there, it should be able to read that throughout the app ( in which it does ). The problem is that it doesn't actually add the result when I use it.

So I click the App, then select OnStart = fx... and in the formula, I add:

Set(IPRCSS, {
 FontFamily: Font.'Segoe UI',
 PrimaryColor: RGBA(133, 15, 136, 1),
 SecondaryColor: RGBA(94,63, 182,1)
});

Now when I go to a component, or even just a screen, I'll click the object (in this case a Text field) and then I'll add the variable in the "Color" portion ( IPRCSS.PrimaryColor ). The app reads it but it doesn't actually change the color. When I play it, it still doesn't work. How do I get the variable to actually input it into the field?


Solution

  • If you use App.OnStart to define the variables that your app will be using, you need to explicitly "run" the App.OnStart from the tree view for it to execute for the first time (see below). If you save/close/reopen the app you shouldn't need to do that.

    Running App.OnStart

    Having said that, if your settings are constants that are not changed throughout the execution of the app, a better way would be to use named formulas. Those are referenced declaratively from the control properties, and have a better performance than variables.

    For example, if you define the expression for the App.Formulas to

    IPRCSS = {
      FontFamily: Font.'Segoe UI',
      PrimaryColor: RGBA(133, 15, 176, 1),
      SecondaryColor: RGBA(94,63, 182,1)
    };
    

    You should be able to reference IPRCSS.PrimaryColor, IPRCSS.FontFamily, ... from your controls and it should immediately work.