How to Run Aspose.CAD in Multi-platform App UI(MAUI)

Prerequisites

  • Visual Studio 2022.
  • NET 7 SDK is used in the example.

MAUI

.NET Multi-platform App UI (.NET MAUI) is a cross-platform framework for creating native mobile and desktop apps with C# and XAML. Using .NET MAUI, you can develop apps that can run on Android, iOS, macOS, and Windows from a single shared code-base.

.NET MAUI is open-source and is the evolution of Xamarin.Forms, extended from mobile to desktop scenarios, with UI controls rebuilt from the ground up for performance and extensibility. If you’ve previously used Xamarin.Forms to build cross-platform user interfaces, you’ll notice many similarities with .NET MAUI. However, there are also some differences. Using .NET MAUI, you can create multi-platform apps using a single project, but you can add platform-specific source code and resources if necessary. One of the key aims of .NET MAUI is to enable you to implement as much of your app logic and UI layout as possible in a single code-base.

Installation MAUI

  1. To create .NET MAUI apps, you’ll need the latest version of Visual Studio 2022
  2. Either install Visual Studio, or modify your existing installation, and install the .NET Multi-platform App UI development workload with its default optional installation options:
    Visual installer

Create project

  1. Launch Visual Studio. In the start window, click Create a new project to create a new project:
    Create project
  2. In the Create a new project window, select MAUI in the All project types drop-down, select the .NET MAUI App template, and click the Next button:
    Select project type
  3. In the Configure your new project window, name your project, choose a suitable location for it, and click the Next button:
    Select project path
  4. In the Additional information window, choose the version of .NET that you’d like to target and click the Create button:
    Target Framework
  5. Wait for the project to be created and its dependencies to be restored
  6. Install the latest Aspose.CAD version from NuGet:
    NuGet
  7. Replace the code in MainPage.xaml
    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="MauiApp1.MainPage">
    
        <ScrollView>
            <VerticalStackLayout
                Spacing="25"
                Padding="30,0"
                VerticalOptions="Center">
    
                 <Label 
                Text="Welcome to .NET MAUI!"
                VerticalOptions="Center" 
                HorizontalOptions="Center" />
    
                <Image
                    x:Name="Base64DecodedImage"
                    Source="dotnet_bot.png"
                    SemanticProperties.Description="Cute dot net bot waving hi to you!"
                    HeightRequest="600"
                    HorizontalOptions="Center" />
    
                <Label
                    Text="Hello, World!"
                    SemanticProperties.HeadingLevel="Level1"
                    FontSize="32"
                    HorizontalOptions="Center" />
    
                <Label
                    Text="Welcome to .NET Multi-platform App UI"
                    SemanticProperties.HeadingLevel="Level2"
                    SemanticProperties.Description="Welcome to dot net Multi platform App U I"
                    FontSize="18"
                    HorizontalOptions="Center" />
    
                <Button
                    x:Name="FileSelectrBtn"
                    Text="Select file"
                    SemanticProperties.Hint="Counts the number of times you click"
                    Clicked="OnSelectFileClicked"
                    HorizontalOptions="Center" />
    
                <Button
                    x:Name="ClearBtn"
                    Text="Clear"
                    SemanticProperties.Hint="Counts the number of times you click"
                    Clicked="OnCleanClicked"
                    HorizontalOptions="Center" />
    
            </VerticalStackLayout>
        </ScrollView>
    </ContentPage>
  8. Add new methods in MainPage.xaml.cs
    private void OnCleanClicked(object sender, EventArgs e)
    {
        Base64DecodedImage.Source = "dotnet_bot.png";
    }
    private async void OnSelectFileClicked(object sender, EventArgs e)
    {
        var result = await FilePicker.PickAsync(new PickOptions()
        {
            PickerTitle = "any"
        });
        if (result == null)
        {
            return;
        }
        var stream = await result.OpenReadAsync();
        var img = Aspose.CAD.Image.Load(stream);
        var ms = new MemoryStream();
        await img.SaveAsync(ms, new PngOptions());
        Base64DecodedImage.Source = ImageSource.FromStream(() => ms);
    }

Debug Windows Machine

  1. In the Visual Studio toolbar, use the Debug Target drop-down to select Framework and then the net7.0-windows entry:
    Debug Target
  2. In the Visual Studio toolbar, press the Windows Machine button to build and run the app:
    Start Debugging on Windows Machine
  3. In the running app, press the “Select file” button several times and observe that the count of the number of button clicks is incremented:
    Home Page
  4. Select the required file and click open:
    Select file
  5. After processing the file, an image of the converted file should appear on the screen:
    The result of debugging windows

Install Android Emulator

  1. In the Visual Studio toolbar, use the Debug Target drop-down to select Android Emulators and then the Android Emulator entry
  2. In the Visual Studio toolbar, press the Android Emulator button:
    Start install Emulator button
  3. In the Android SDK - License Agreement window, press the Accept button:
    Android SDK-License Agreement
  4. In the Android SDK - License Agreement window, press the Accept button:
    Android SDK ARM -License Agreement
  5. In the User Account Control dialog, press the Yes button:
    SDK Manager
  6. In the License Acceptance window, press the Accept button:
    SDK ARManagerM -License Agreement
  7. In the Visual Studio toolbar, press the Android Emulator button:
    Start install Emulator button
  8. In the User Account Control dialog, press the Yes button:
    Android Device Manager
  9. In the New Device window, press the Create button:
    New Android Device
  10. Close the Android Device Manager window

Debug with Android Emulator

  1. In the Visual Studio toolbar, press the Pixel 5 - API 33 (Android 13.0 - API 33) button to build and run the app:
    Select android Emulator
  2. In the running app in the Android emulator, press the “Select file” button several times and observe that the count of the number of button clicks is incremented:
    Android Emulator Home Page
  3. Select the required file and click select:
    Select file in andoid
  4. After processing the file, an image of the converted file should appear on the screen:
    The result of debugging android

More Examples

For more samples of how you can use Aspose.CAD in Docker, see the examples.

See Also.