2

Build an Uno Platform app with Azure Mobile Apps

 11 months ago
source link: https://learn.microsoft.com/en-us/azure/developer/mobile-apps/azure-mobile-apps/quickstarts/uno/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Build an Uno Platform app with Azure Mobile Apps

  • Article
  • 06/12/2023

In this article

The Uno Platform lets you create .NET UI applications for Windows, macOS, Linux, iOS, Android, and Web Assembly from a single codebase. This tutorial shows you how to add a cloud-based backend service to an Uno Platform Android mobile app by using Azure Mobile Apps and an Azure mobile app backend. You create both a new mobile app backend and a simple Todo list app that stores app data in Azure.

You must complete this tutorial before all other Uno Platform tutorials about using Azure Mobile Apps.

Although the Uno Platform supports a wide variety of platforms, the Azure Mobile Apps library is only supported on mobile and desktop platforms. The tutorial only covers the Android platform.

Prerequisites

To complete this tutorial, you need:

This tutorial assumes you are using Windows and Visual Studio 2022. We recommend that you walk through the Uno Platform tutorial to become acquainted with the development process for the Uno Platform.

Download the sample app

  1. Open the azure-mobile-apps repository in your browser.

  2. Open the Code drop-down, then select Download ZIP.

    Screenshot of the Code menu on GitHub.
  3. Once the download is complete, open your Downloads folder and locate the azure-mobile-apps-main.zip file.

  4. Right-click the downloaded file, and select Extract All....

    If you prefer, you can use PowerShell to expand the archive:

    PowerShell
    C:\Temp> Expand-Archive azure-mobile-apps-main.zip
    

The samples are located in the samples folder within the extracted files. The sample for the quick start is named TodoApp. You can open the sample in Visual Studio by double-clicking the TodoApp.sln file.

Screenshot of the file explorer for the solution.

Deploy the backend to Azure

If you have already deployed the backend from another quick start, you can use the same backend and skip this step.

To deploy the backend service, we will:

  • Use Azure Resource Manager and the Azure CLI to deploy an Azure App Service and Azure SQL Database to Azure.
  • Use Visual Studio to publish the service code to the newly created Azure App Service.

Create resources on Azure.

  1. Open a terminal and change directory to the folder containing the TodoApp.sln file. This directory also contains azuredeploy.json.

  2. Ensure you've signed in and selected a subscription using the Azure CLI.

  3. Create a new resource group:

    Azure CLI
    az group create -l westus -g quickstart
    

    This command will create the quickstart resource group in the West US region. You can select any region that you wish, providing you can create resources there. Ensure you use the same name and region wherever they're mentioned in this tutorial.

  4. Create the resources using a group deployment:

    Azure CLI
    az deployment group create -g quickstart --template-file azuredeploy.json --parameters sqlPassword=MyPassword1234
    

    Pick a strong password for your SQL Administrator password. You'll need it later on when accessing the database.

  5. Once the deployment is complete, get the output variables as these hold important information you'll need later on:

    Azure CLI
    az deployment group show -g quickstart -n azuredeploy --query properties.outputs
    

    An example output will be:

    Screenshot of command line results.
  6. Make a note of each of the values in the outputs for later use.

Publish the service code.

Open the TodoApp.sln in Visual Studio.

  1. In the right-hand pane, select the Solutions Explorer.

  2. Right-click the TodoAppService.NET6 project, then select Set as Startup Project.

  3. On the top menu, select Build > Publish TodoAppService.NET6.

  4. In the Publish window, select Target: Azure, then press Next.

    Screenshot of the target selection window.
  5. Select Specific target: Azure App Service (Windows), then press Next.

    Screenshot of the specific target selection window.
  6. If necessary, sign in and select an appropriate Subscription name.

  7. Ensure View is set to Resource group.

  8. Expand the quickstart resource group, then select the App Service that was created earlier.

    Screenshot of the app service selection window.
  9. Select Finish.

  10. Once the publish profile creation process has completed, select Close.

  11. Locate the Service Dependencies and select the triple-dots next to the SQL Server Database, then select Connect.

    Screenshot showing the S Q L server configuration selection.
  12. Select Azure SQL Database, then select Next.

  13. Select the quickstart database, then select Next.

    Screenshot of the database selection window.
  14. Fill in the form using the SQL username and password that were in the outputs of the deployment, then select Next.

    Screenshot of the database settings window.
  15. Select Finish.

  16. Select Close when complete.

  17. Select Publish to publish your app to the Azure App Service you created earlier.

    Screenshot showing the publish button.

  18. Once the backend service is published, a browser will be opened. Add /tables/todoitem?ZUMO-API-VERSION=3.0.0 to the URL:

    Screenshot showing the browser output after the service is published.

Configure the sample app

Your client application needs to know the base URL of your backend so that it can communicate with it.

  1. Expand the TodoApp.Data project.

  2. Right-click on the TodoApp.Data project, then select Add > Class....

  3. Enter Constants.cs as the name, then select Add.

    Screenshot of adding the Constants.cs file to the project.
  4. Open the Constants.cs.example file and copy the contents (Ctrl-A, followed by Ctrl-C).

  5. Switch to Constants.cs, highlight all text (Ctrl-A), then paste the contents from the example file (Ctrl-V).

  6. Replace the https://APPSERVICENAME.azurewebsites.net with the backend URL of your service.

    namespace TodoApp.Data
    {
        public static class Constants
        {
            /// <summary>
            /// The base URI for the Datasync service.
            /// </summary>
            public static string ServiceUri = "https://demo-datasync-quickstart.azurewebsites.net";
        }
    }
    

    You can obtain the backend URL of your service from the Publish tab. Ensure you use a https URL.

  7. Save the file. (Ctrl-S).

Build and run the sample app for Android

  1. In the solutions explorer, expand the uno/Platforms folder.

  2. Right-click the TodoApp.Uno.Mobile project and select Set as Startup Project.

  3. In the top bar, select the Any CPU configuration, TodoApp.Uno.Mobile target. Select a suitable Android emulator to run the application:

    Screenshot showing how to select an Android emulator in Visual Studio.

  4. Press F5 to build and run the project.

Once the app has started, you see an empty list with a text box. You can:

  • Enter some text, then press the + icon to add the item.

  • Set or clear the check box to mark any item as done.

  • Press the refresh icon to reload data from the service.

    Screenshot of the Uno Platform app running on Android.

Next steps

Continue the tutorial by adding authentication to the app.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK