I have some 20-30 line pieces of code running when various buttons are pressed. Is there a way to write this code once, and then call it in different places? Having to make sure I make changes everywhere is making my app pretty vulnerable on even small changes.
However, the pattern of organizing a function into a single place so it can be used over and over is possible. There are some considerations, though.
A common example is a 'Loading Screen's with a Button per type of load. Then I can Navigate to the 'Loading Screen' with a context variable, like "LoadAccounts" which the onVisible property of the screen can use to select the button.
This way, if my function to load accounts is complicated, I can still get to it for multiple places, and even go so far as to pass a screen variable so the button can automatically navigate the user back.
This has another advantage of giving you a blocking pattern to allow the app to process data and letting the user know you're doing X, Y, Z. Bonus if you have a loading.io spinner or something.
NOTE: the function Select() is used to click the button, but it's the same as a user clicking. If you had three buttons (each with complicated functions), you should consider having the last like in each select the next button (if they have to be sequential), otherwise three select functions in a row is the same as the user (very) quickly and starting the three functions in parallel.
Hi @brucemZA ：
Do you want to encapsulate a long piece of code as a custom function and then you can use this function by calling the function name?
The most direct answer is: This is an idea under review.
If you want to implement this feature, please vote for this idea:
In addition, there are three alternatives:
You can call Flow in PowerApps, and Flow can return specific results to PowerApps. In other words, you can delegate the data processing process to flow, and you only need to fill in the name of the flow and the necessary parameters for each call.
The following connection is a case I made (1\using a button in the app to trigger the flow. 2\the flow stores the picture to onedrive and then gets the sharing connection. 3\The flow returns the sharing connection to powerapps)
I think this link will help you a lot:
2\Createing a custom connection.
It's not a simple process today. The main process to add a custom function is to create a custom API, where the function would be executed in the server, and in your app you'd add a connection to that API.
3\Using custom Components.I'v made a test for your reference:
Components are reusable building blocks for canvas apps so that app makers can create custom controls to use inside an app, or across apps using a component library. Components can use advanced features such as custom properties and enable complex capabilities.
In other words, you can encapsulate the data processing process into a custom Component, and then implement code reuse by using this Component.
I assume that the formula I want to customize is number1*number2.
Step1: add a compentent
In this compentent
Step2: add two cunstom proeprty(Input1/Input2)
Step3: add a cunstom proeprty(Output1)
Step4: add a label control(Label1_2)
Step5: set this component's Output1 property to:
Back to my screen and add a custom component(Component1_4)
step6\Add two textinput controls(TextInput3/TextInput3_1)
step7\set the Component1_4
step7\Add a label and set it's Text preoeprty to:
Featuring samples like Return to the Workplace and Emergency Response Applications
We're excited to announce our first cross-community 'Can You Solve These?' challenge!
Reopen responsibly, monitor intelligently, and protect continuously with solutions for a safer work environment.
We are excited for the next Super User season.
Features releasing from October 2020 through March 2021