Showing posts with label .net maui community toolkit. Show all posts

Introduction Hey! Ever wanted to create cool apps for different devices like phones and computers? Well, .NET MAUI makes it super...

.NET MAUI - Video Player using Community Toolkit .NET MAUI - Video Player using Community Toolkit

A blog about android developement

.net maui community toolkit

Introduction

Hey! Ever wanted to create cool apps for different devices like phones and computers? Well, .NET MAUI makes it super easy. It's like a superhero for app creators! Now, imagine adding fun things like videos to your apps. That's where the Community Toolkit comes in – a toolbox that lots of developers work on together. Today, we're exploring the Video Player control, a cool feature powered by .NET MAUI and its friendly Community Toolkit. Get ready for an adventure in making your apps more exciting with videos!


Quick Links:


Project Setup:

  • Launch Visual Studio 2022, and in the start window click Create a new project to create a new project.
  • 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:
  • In the configure your new project window, name your project, choose a suitable location for it, and click the Next button:
  • In the Additional information window, click the Create button:
  • Once the project is created, we can able to see the Android, iOS, Windows and other running options in the toolbar. Press the emulator or run button to build and run the app

Install Plugin:

  • Open Terminal or Command Prompt: Open a terminal or command prompt window. You can usually find this on your computer by searching for "Command Prompt" (Windows) or "Terminal" (macOS/Linux).
  • Navigate to Your Project Folder: Use the cd command to navigate to your .NET MAUI project folder. For example:
  • Install Community Toolkit: Run the following command to install the CommunityToolkit.Maui package:
    dotnet add package Microsoft.Maui.CommunityToolkit
  • Restore Packages: After the installation, run the following command to restore the packages:
    dotnet restore

Implementation

  • First, we need to open "MauiProgram.cs" and include the following namespace and line to allow the app to use the Chart Library.
    using CommunityToolkit.Maui;
    .UseMauiCommunityToolkit()
    .UseMauiCommunityToolkitMediaElement()
  • Open MainPage.xaml file and add the following namespace. (the page will be replaced according to you).
    xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
  • Then, remove the default content and add an instance of the Media Element class to the page.
    <toolkit:MediaElement Source="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
    ShouldShowPlaybackControls="True"
    BackgroundColor="AliceBlue"
    x:Name="mediaElement"/>
  • We can add other controls for custom video controls like play/pause and volume controls like below.
    <HorizontalStackLayout BindingContext="{x:Reference mediaElement}"
    					      HorizontalOptions="Center"
    					      Spacing="10">
    	
    	<Button Text="Play"
    		HorizontalOptions="CenterAndExpand"
    		Clicked="OnPlayPauseButtonClicked">
    	</Button>
    	<Button Text="Stop"
    		HorizontalOptions="CenterAndExpand"
    		Clicked="OnStopButtonClicked">
    	</Button>
    </HorizontalStackLayout>
    <Slider Maximum="1.0"
    		   Minimum="0.0"
    		   Value="{Binding Volume}"
    		   ValueChanged="Slider_ValueChanged"  
    		   Rotation="270"
    		   WidthRequest="100" />
  • Open Code behind and add the following which will be useful to controls media elements using custom controls.
    void OnPlayPauseButtonClicked(object sender, EventArgs args)
    {
    	if (mediaElement.CurrentState == MediaElementState.Stopped ||
    		mediaElement.CurrentState == MediaElementState.Paused)
    	{
    		mediaElement.Play();
    	}
    	else if (mediaElement.CurrentState == MediaElementState.Playing)
    	{
    		mediaElement.Pause();
    	}
    }
    
    void OnStopButtonClicked(object sender, EventArgs args)
    {
    	mediaElement.Stop();
    }
    
    private void Slider_ValueChanged(object sender, ValueChangedEventArgs e)
    {
    	mediaElement.Volume = e.NewValue;
    }

Download Code:

You can download the code from GitHub. If you have any doubts, feel free to post a comment. If you liked this article, and it is useful to you, do like, share the article & star the repository on GitHub.

In this video, we will see the recap of .NET MAUI articles post on 2023. Supercharge your .NET MAUI projects by referring this articles. ...

.NET MAUI - 2023 Recap .NET MAUI - 2023 Recap

A blog about android developement

.net maui community toolkit

In this video, we will see the recap of .NET MAUI articles post on 2023. Supercharge your .NET MAUI projects by referring this articles.

DEV EXPRESS CHARTS IN .NET MAUI

December 04, 2023

Read More

Lottie animations in .NET MAUI

November 19, 2023

Read More

Transforming Labels into Hyperlinks with .NET MAUI

November 12, 2023

Read More

Mastering MVVM: A Deep Dive into .NET MAUI with MVVM Toolkit

October 28, 2023

Read More

Dev Express - Data Grid Control for .NET MAUI (Lifetime - Free plugin)

October 15, 2023

Read More

Data Grid Control for .NET MAUI (Free plugin to Sort, Filter & Show Data)

October 04, 2023

Read More

.NET MAUI - Swipe View

September 17, 2023

Read More

Flyout Page in .NET MAUI

July 21, 2023

Read More

.NET MAUI Barcode Scanner using IRONBARCODE

July 02, 2023

Read More

Dynamic Status Bar in .NET MAUI

June 18, 2023

Read More

Localisation in .NET MAUI

June 12, 2023

Read More

File Picker in .NET MAUI

May 29, 2023

Read More

Toast in .NET MAUI

May 21, 2023

Read More

Avatar View in .NET MAUI Community Toolkit

May 07, 2023

Read More

Signature Pad using .NET MAUI Community Toolkit

April 16, 2023

Read More

.Net MAUI - QR Code Generator

January 05, 2023

Read More

.Net MAUI - Zxing Barcode Scanner

January 10, 2023

Read More

Introduction Welcome to our newest blog post, where we explore the vibrant realm of MVVM (Model-View-ViewModel) architecture usin...

Mastering MVVM: A Deep Dive into .NET MAUI with MVVM Toolkit Mastering MVVM: A Deep Dive into .NET MAUI with MVVM Toolkit

A blog about android developement

.net maui community toolkit

Introduction

Welcome to our newest blog post, where we explore the vibrant realm of MVVM (Model-View-ViewModel) architecture using the cutting-edge MVVM Toolkit in .NET MAUI. In this comprehensive guide, we will unravel the intricacies of MVVM and demonstrate how the MVVM Toolkit in .NET MAUI empowers developers to create robust, responsive, and easily maintainable cross-platform mobile applications. Join us on this enlightening journey as we unravel the secrets of mastering MVVM in the context of .NET MAUI.


Quick Links:


Project Setup:

  • Launch Visual Studio 2022, and in the start window click Create a new project to create a new project.
  • 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:
  • In the configure your new project window, name your project, choose a suitable location for it, and click the Next button:
  • In the Additional information window, click the Create button:
  • Once the project is created, we can able to see the Android, iOS, Windows and other running options in the toolbar. Press the emulator or run button to build and run the app

Install Plugin:

In this steps, we will see the steps to install "MVVM Toolkit" in .NET MAUI project:
  • Access NuGet Package Manager: In Visual Studio, right-click on your .NET MAUI project in the Solution Explorer. From the context menu, select "Manage NuGet Packages."
  • Search for "CommunityToolkit.Mvvm": In the NuGet Package Manager, click on the "Browse" tab. In the search bar, type "CommunityToolkit.Mvvm" and hit Enter. The package should appear in the search results.
  • Select and Install the Package: Once you find "CommunityToolkit.Mvvm" in the search results, click on it to select it. Ensure you choose the appropriate version compatible with your .NET MAUI project. Click on the "Install" button to begin the installation process.
  • Accept License Agreement: During the installation, you may be prompted to accept the license agreement. Review the terms and conditions and click on the "Accept" button to proceed.
  • Wait for Installation to Complete: Visual Studio will download and install the package along with its dependencies. This process may take a few moments, depending on your internet connection speed.
  • Verify Installation: After the installation is complete, verify that there are no error messages in the Output window. This indicates a successful installation of the package.

Implementation

View Model

  • In this step, we create a ViewModel that inherits from the ObservableObject class. This inheritance is pivotal because ObservableObject implements the INotifyPropertyChanged interface. By doing so, we gain the ability to trigger the PropertyChanged event, a vital mechanism enabling the notification of property value changes to subscribers, primarily the UI. This synchronization is fundamental for effective data binding, ensuring seamless coordination between the user interface and the underlying ViewModel.

    For example:
    public partial class ItemEntryPageModel : ObservableObject
    {
        [ObservableProperty]
        private int _id;
        [ObservableProperty]
    	private string _name;
    	[ObservableProperty]
    	private string _description;
    
        [ICommand]
    	public async void Save()
    	{
            await Application.Current.MainPage.DisplayAlert("MAUI MVVM Sample", "Item Saved Successfully", "OK");
        }
    }
  • When we use the [ObservableProperty] attribute, properties can send automatic alerts when they change. This is important for connecting data and updating the user interface (UI) when properties change. When you apply the [ObservableProperty] attribute to a property, it does a lot of necessary coding work behind the scenes. It sets up the code needed to tell other parts of the program when a property changes. This attribute saves time because you don't have to write all this code manually. For Example: "_description" & "_name" produces "Description" & "Name" respectively.
  • The [ICommand] implementation is a way to connect methods or actions in the app with what the user sees on the screen. For instance, it can create a command called SaveCommand that's linked to the Save method. This linking is crucial for making sure that when a user does something, like clicking a button, the right action happens in the app.

View

  • Finally, we will design the View, where we create the user interface using XAML. Here, we connect the UI elements to the ViewModel properties using data binding expressions.
  • This connection enables the View to show and modify task data in real-time, ensuring a dynamic and responsive user experience.
  • <?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="MauiMVVM.Views.ItemEntryPage"
                 Title="Item Entry">
        <StackLayout Margin="20"
                     Spacing="10">
            <VerticalStackLayout>
                <Label Text="Name:"
                       FontSize="16"/>
                <Entry Text="{Binding Name}" 
                       Placeholder="Item Name"/>
            </VerticalStackLayout>
    
            <VerticalStackLayout>
                <Label Text="Description:"
                       FontSize="16"/>
                <Entry Text="{Binding Description}" 
                       Placeholder="Item Description"/>
            </VerticalStackLayout>
    
    
            <Button x:Name="btn_save" 
                    Text="Save"
                    Command="{Binding SaveCommand}"/>
        </StackLayout>
    </ContentPage>
  • In .NET MAUI, data binding makes sure that data stays in sync between the ViewModel and the View without manual intervention.
  • For instance, we can link a task's title in the ViewModel to a label in the View. When the title changes, it instantly updates in the UI.
  • This automatic synchronization simplifies UI updates and eliminates the need for handling events manually.

Wire-up View and View Model

  • The created view and view model should be added in MauiProgram.cs like below.
    var builder = MauiApp.CreateBuilder();
    		builder
    			.UseMauiApp<App>()
    			.ConfigureFonts(fonts =>
    			{
    				fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
    				fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
    			});
    
    #if DEBUG
    		builder.Logging.AddDebug();
    #endif
            builder.Services.AddTransient<ItemEntryPage>();
            builder.Services.AddTransient<ItemEntryPageModel>();
    
            return builder.Build();

Full Code:

Demo

Download Code:

You can download the code from GitHub. If you have any doubts, feel free to post a comment. If you liked this article, and it is useful to you, do like, share the article & star the repository on GitHub.

References

To learn more about Data binding and MVVM

Introduction .NET MAUI, a cross-platform framework, empowers developers to build native mobile and desktop applications using C# and...

Flyout Page in .NET MAUI Flyout Page in .NET MAUI

A blog about android developement

.net maui community toolkit

Introduction

.NET MAUI, a cross-platform framework, empowers developers to build native mobile and desktop applications using C# and XAML. It enables the creation of apps that seamlessly operate on Android, iOS, macOS, and Windows, all from a unified codebase. This open-source platform is an advancement of Xamarin Forms, expanding its reach to desktop scenarios while enhancing UI controls for improved performance and extensibility.


In this article, we will see how we can implement Flyout Page in .NET MAUI project.

Quick Links:


Project Setup:

  • Launch Visual Studio 2022, and in the start window click Create a new project to create a new project.
  • 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:
  • In the configure your new project window, name your project, choose a suitable location for it, and click the Next button:
  • In the Additional information window, click the Create button:
  • Once the project is created, we can able to see the Android, iOS, Windows and other running options in the toolbar. Press the emulator or run button to build and run the app

Implementation

To create a flyout page in a Xamarin.Forms application, you can follow these steps:

  • Add a new page as AppFlyoutPage (XAML Flyout Page) and add the following code.
    <FlyoutPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                xmlns:local="clr-namespace:MauiFlyoutPage"
                x:Class="MauiFlyoutPage.AppFlyoutPage">
        <FlyoutPage.Flyout>
            <local:MenuPage x:Name="flyoutPage" />
        </FlyoutPage.Flyout>
        <FlyoutPage.Detail>
            <NavigationPage>
                <x:Arguments>
                    <local:FirstPage />
                </x:Arguments>
            </NavigationPage>
        </FlyoutPage.Detail>
    </FlyoutPage>
  • Then create a content page "MenuPage" which will be used as master page and add the following code.
    <?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="MauiFlyoutPage.MenuPage"
                 Title="MenuPage">
        <VerticalStackLayout VerticalOptions="Center"
                             HorizontalOptions="Center">
            <Button 
                x:Name="btn"
                x:FieldModifier="public"
                Text="Open Second Page"
                VerticalOptions="Center" 
                HorizontalOptions="Center"/>
        </VerticalStackLayout>
    </ContentPage>
  • Open code behind and add the following code.
    namespace MauiFlyoutPage;
    
    public partial class AppFlyoutPage : FlyoutPage
    {
    	public AppFlyoutPage()
    	{
    		InitializeComponent();
            flyoutPage.btn.Clicked += OpenSecondPageClicked;
        }
    
        private void OpenSecondPageClicked(object sender, EventArgs e)
        {
            if (!((IFlyoutPageController)this).ShouldShowSplitMode)
                IsPresented = false;
            Detail = new NavigationPage(new SecondPage());
        }
    }
  • Here, "flyoutPage.btn" present in the master screen and accessed from the flyout screen using XAML field modifier."
  • In the same way, created two pages as "FirstPage" and "SecondPage" to navigate between the screens in the flyout menu.
  • Now, when we running the application, can able see a flyout menu on the left side and the detail content on the right side. Tapping on the button in the flyout master will update the detail content accordingly.

Full Code:

Demo

Download Code:

You can download the code from GitHub. If you have any doubts, feel free to post a comment. If you liked this article, and it is useful to you, do like, share the article & star the repository on GitHub.

References

https://learn.microsoft.com/en-us/dotnet/maui/user-interface/pages/flyoutpage

Introduction .NET MAUI, a cross-platform framework, empowers developers to build native mobile and desktop applications using C# and...

Dynamic Status Bar in .NET MAUI Dynamic Status Bar in .NET MAUI

A blog about android developement

.net maui community toolkit

Introduction

.NET MAUI, a cross-platform framework, empowers developers to build native mobile and desktop applications using C# and XAML. It enables the creation of apps that seamlessly operate on Android, iOS, macOS, and Windows, all from a unified codebase. This open-source platform is an advancement of Xamarin Forms, expanding its reach to desktop scenarios while enhancing UI controls for improved performance and extensibility.


In this article, we will see how we can implement Dynamic Status Bar in .NET MAUI project.

Quick Links:


Project Setup:

  • Launch Visual Studio 2022, and in the start window click Create a new project to create a new project.
  • 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:
  • In the configure your new project window, name your project, choose a suitable location for it, and click the Next button:
  • In the Additional information window, click the Create button:
  • Once the project is created, we can able to see the Android, iOS, Windows and other running options in the toolbar. Press the emulator or run button to build and run the app

Implementation

Screen Design:

As a first point, we need to implement the screen design as per our requirement. In this tutorial, we will use 3 buttons like in the following code block.

<?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="MauiStatusBarBehaviour.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">

            <Button
                Text="Blue"
                SemanticProperties.Hint="Counts the number of times you click"
                Clicked="OnBlueClicked"
                HorizontalOptions="Center" />

            <Button
                Text="Green"
                SemanticProperties.Hint="Counts the number of times you click"
                Clicked="OnGreenClicked"
                HorizontalOptions="Center" />

            <Button
                Text="Default"
                SemanticProperties.Hint="Counts the number of times you click"
                Clicked="OnDefaultClicked"
                HorizontalOptions="Center" />

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

Methods:

To implement this, we can implement in the following methodlogies.

  • Applying Behaviour in XAML.
  • Applying Behaviour in Code behind.
  • Applying without behaviour in Code behind.

In this article, we are going to see "Applying without behaviour in Code behind.".

  • Just install the .NET MAUI community toolkit to your project.
    Install-Package CommunityToolkit.Maui -Version 3.1.0
  • Add it to the MauiProgram.cs
    var builder = MauiApp.CreateBuilder();
    builder
    .UseMauiApp<App>()
    .UseMauiCommunityToolkit()
    .ConfigureFonts(fonts => {
        fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
        fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
    });
  • Add event in code behind for each button click and apply the below code.
    CommunityToolkit.Maui.Core.Platform.StatusBar.SetColor(statusBarColor);
    CommunityToolkit.Maui.Core.Platform.StatusBar.SetStyle(StatusBarStyle.LightContent);

Full Code:

Demo

Download Code:

You can download the code from GitHub. If you have any doubts, feel free to post a comment. If you liked this article, and it is useful to you, do like, share the article & star the repository on GitHub.

References

https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/behaviors/statusbar-behavior

Introduction .NET MAUI, a cross-platform framework, empowers developers to build native mobile and desktop applications using C#...

File Picker in .NET MAUI File Picker in .NET MAUI

A blog about android developement

.net maui community toolkit

Introduction

.NET MAUI, a cross-platform framework, empowers developers to build native mobile and desktop applications using C# and XAML. It enables the creation of apps that seamlessly operate on Android, iOS, macOS, and Windows, all from a unified codebase. This open-source platform is an advancement of Xamarin Forms, expanding its reach to desktop scenarios while enhancing UI controls for improved performance and extensibility.


In this article, we will see how we can implement a File Picker in the .NET MAUI project.


Quick Links:


Project Setup:

  • Launch Visual Studio 2022, and in the start window click Create a new project to create a new project.
  • 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:
  • In the configure your new project window, name your project, choose a suitable location for it, and click the Next button:
  • In the Additional information window, click the Create button:
  • Once the project is created, we can able to see the Android, iOS, Windows and other running options in the toolbar. Press the emulator or run button to build and run the app

Implementation:

As a first point, we need to implement the screen design as per our requirement. In this tutorial, we will use 3 controls - button, image, and label like in the following code block.


<?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="MauiFilePicker.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30"
            VerticalOptions="Start">

            <Button
                x:Name="ImageSelect"
                Text="Select Barcode"
                SemanticProperties.Hint="Select Image"
                Clicked="SelectBarcode"
                HorizontalOptions="Center" />
            <Label x:Name="outputText"
                   Padding="10"/>
            <Image
                x:Name="barcodeImage"
                SemanticProperties.Description="Selected Barcode"
                HeightRequest="200"
                HorizontalOptions="Center" />
        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

Here,

  • Button - used to select images from device.
  • Image - used to display the selected image.
  • Label - used to display the file path once the image selected through file picker.

Functional part

  • Add a click event for the button to select the image like below.
    private async void SelectBarcode(object sender, EventArgs e)
    {
    	var images = await FilePicker.Default.PickAsync(new PickOptions
    	{
    		PickerTitle = "Pick Barcode/QR Code Image",
    		FileTypes = FilePickerFileType.Images
    	});
    	var imageSource = images.FullPath.ToString();
    	barcodeImage.Source = imageSource;
    	outputText.Text = imageSource;
    }

    Here,

    • FileTypes: The default file types available for selection in the FilePicker are FilePickerFileType.Images, FilePickerFileType.Png, and FilePickerFileType.Videos. However, if you need to specify custom file types for a specific platform, you can create an instance of the FilePickerFileType class. This allows you to define the desired file types according to your requirements.
    • PickerTitle: The PickOptions.PickerTitle is presented to the user and its behavior varies across different platforms.

    Full Code:

    Demo

    Android:
    android
    Windows:
    windows

    Download Code:

    You can download the code from GitHub. If you have any doubts, feel free to post a comment. If you liked this article, and it is useful to you, do like, share the article & star the repository on GitHub.

    References

    https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/storage/file-picker

.NET MAUI, a powerful platform for developing cross-platform mobile applications, provides a seamless way to incorporate Toast fun...

Toast in .NET MAUI Toast in .NET MAUI

A blog about android developement

.net maui community toolkit

.NET MAUI, a powerful platform for developing cross-platform mobile applications, provides a seamless way to incorporate Toast functionality into your app. With the aid of the appropriate tools and resources, implementing an Toast in .NET MAUI using the .NET MAUI Community Toolkit is a straightforward process. In this tutorial, we will guide you through the necessary steps to successfully integrate Toast into your .NET MAUI application.


Quick Links:


Project Setup:

  • Launch Visual Studio 2022, and in the start window click Create a new project to create a new project.
  • 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:
  • In the configure your new project window, name your project, choose a suitable location for it, and click the Next button:
  • In the Additional information window, click the Create button:
  • Once the project is created, we can able to see the Android, iOS, Windows and other running options in the toolbar. Press the emulator or run button to build and run the app

Implementation:

.NET MAUI Community Toolkit is the key to achieve drawing in our App is to use Community.ToolKit.Maui NuGet package. It is a collection of reusable elements such as animations, behaviors converters, among others, for developing applications for iOS, Android, macOS and WinUI using MAUI.


Install .NET MAUI Community Toolkit

  • Install .NET MAUI Community Toolkit nuGet package on your .NET MAUI application.
  • Now initialize the plugin. Go to your MauiProgram.cs file. In the CreateMauiApp method, place in the .UseMauiApp<App>() line and just below it add the following line of code.
var builder = MauiApp.CreateBuilder();
builder
	.UseMauiApp<App>()
	.UseMauiCommunityToolkit()
	.ConfigureFonts(fonts =>
	{
		fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
		fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
	});
    CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            await Toast.Make("Hi, How are you?",
                      ToastDuration.Long,
                      16)
                .Show(cancellationTokenSource.Token);
  • The methods Make() and Show() are essential for displaying Toast messages on the screen. Each of these methods possesses specific attributes that facilitate the configuration of the Toast. Let's delve into a closer examination of these attributes.
  • Make:

    The Make() method is responsible for creating the Toast and requires the following parameters:
    • Text: This is a mandatory parameter represents the message that will be displayed in the Toast.
    • Duration: This parameter specifies the exact duration for which the Toast message will be shown on the screen before fading away. It is an optional parameter and it's default ToastDuration type is ToastDuration.Short:
    • TextSize: This parameter is an optional paramter to set the text size of the Toast. Default value of this parameter is 14 and it's type is double.

    Show:

    This method is responsible for displaying the requested Toast message on the screen.

    Full Code:

    Demo

    Download Code:

    You can download the code from GitHub. If you have any doubts, feel free to post a comment. If you liked this article, and it is useful to you, do like, share the article & star the repository on GitHub.

    The idea behind this tutorial is from "AskXammy"

.NET MAUI is a powerful platform for building cross-platform mobile applications, and with the right tools and resources, it's eas...

Avatar View in .NET MAUI Community Toolkit Avatar View in .NET MAUI Community Toolkit

A blog about android developement

.net maui community toolkit

.NET MAUI is a powerful platform for building cross-platform mobile applications, and with the right tools and resources, it's easy to implement Avatar View to your app. In this tutorial, we'll walk you through the steps for adding Avatar View in .NET MAUI using .NET MAUI Community Toolkit.


Quick Links:


Project Setup:

  • Launch Visual Studio 2022, and in the start window click Create a new project to create a new project.
  • 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:
  • In the configure your new project window, name your project, choose a suitable location for it, and click the Next button:
  • In the Additional information window, click the Create button:
  • Once the project is created, we can able to see the Android, iOS, Windows and other running options in the toolbar. Press the emulator or run button to build and run the app

Implementation:

.NET MAUI Community Toolkit is the key to achieve drawing in our App is to use Community.ToolKit.Maui NuGet package. It is a collection of reusable elements such as animations, behaviors converters, among others, for developing applications for iOS, Android, macOS and WinUI using MAUI.


Install .NET MAUI Community Toolkit

  • Install .NET MAUI Community Toolkit nuGet package on your .NET MAUI application.
  • Now initialize the plugin. Go to your MauiProgram.cs file. In the CreateMauiApp method, place in the .UseMauiApp<App>() line and just below it add the following line of code.
var builder = MauiApp.CreateBuilder();
builder
	.UseMauiApp<App>()
	.UseMauiCommunityToolkit()
	.ConfigureFonts(fonts =>
	{
		fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
		fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
	});
  • A graphical representation known as an avatar is connected to a particular user in order to identify them. This is frequently utilised in programmes that we use every day, therefore it's crucial that you have access to the tools that will enable you to accomplish it. This tutorial will teach you how to quickly and easily construct the.NET Maui Community Toolkit AvatarView!
  • Add the following namespace in the xaml file.
    xmlns:toolkit="clr-namespace:CommunityToolkit.Maui.Views;assembly=CommunityToolkit.Maui"
  • Once the namespace added, you have to add the AvatarView tag with the properties that we required like below.
    <toolkit:AvatarView Text="AM"
    					FontSize="30"
                        TextColor="White"
                        BackgroundColor="Green"
                        BorderColor="White"
                        BorderWidth="5" 
                        HeightRequest="150"
                        WidthRequest="150"                                
                        CornerRadius="120"
                        ImageSource="dotnet_bot.png"/>
  • AvatarView’s properties

    • Text: This property help to set the text to the view
    • ImageSource: This property help to set the image to the view
    • CornerRadius: Determines the shape of the control. The properties can be set in the below ways.
    • Sample 1: CornerRadius="120"
    • Sample 2: CornerRadius="120,40,120,40"

    Full Code:

    Demo

    Download Code:

    You can download the code from GitHub. If you have any doubts, feel free to post a comment. If you liked this article, and it is useful to you, do like, share the article & star the repository on GitHub.

.NET MAUI is a powerful platform for building cross-platform mobile applications, and with the right tools and resources, it's...

Signature Pad using .NET MAUI Community Toolkit Signature Pad using .NET MAUI Community Toolkit

A blog about android developement

.net maui community toolkit

.NET MAUI is a powerful platform for building cross-platform mobile applications, and with the right tools and resources, it's easy to add signature pad functionality to your app. In this tutorial, we'll walk you through the steps of signature pad in .NET MAUI using .NET MAUI Community Toolkit.


Quick Links:


Project Setup:

  • Launch Visual Studio 2022, and in the start window click Create a new project to create a new project.
  • 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:
  • In the configure your new project window, name your project, choose a suitable location for it, and click the Next button:
  • In the Additional information window, click the Create button:
  • Once the project is created, we can able to see the Android, iOS, Windows and other running options in the toolbar. Press the emulator or run button to build and run the app

Implementation:

.NET MAUI Community Toolkit is the key to achieve drawing in our App is to use Community.ToolKit.Maui NuGet package. It is a collection of reusable elements such as animations, behaviors converters, among others, for developing applications for iOS, Android, macOS and WinUI using MAUI.


Install .NET MAUI Community Toolkit

  • Install .NET MAUI Community Toolkit nuGet package on your .NET MAUI application.
  • Now initialize the plugin. Go to your MauiProgram.cs file. In the CreateMauiApp method, place in the .UseMauiApp<App>() line and just below it add the following line of code.
var builder = MauiApp.CreateBuilder();
builder
	.UseMauiApp<App>()
	.UseMauiCommunityToolkit()
	.ConfigureFonts(fonts =>
	{
		fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
		fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
	});
  • DrawView is a class provided by Community.Toolkit.Maui which is responsible for providing a surface that allows drawing lines through touch or mouse interaction.
  • Add the following namespace in the xaml file.
    xmlns:mct="clr-namespace:CommunityToolkit.Maui.Views;assembly=CommunityToolkit.Maui"
  • Once the namespace added, you have to add the DrawingView tag with the properties that we required like below.
    <mct:DrawingView x:Name="DrawBoard"
                        LineColor="Black"
                        LineWidth="5" 
                        HorizontalOptions="FillAndExpand"
                        VerticalOptions="FillAndExpand"
                        HeightRequest="250"
                        IsMultiLineModeEnabled="True"
                        DrawingLineCompleted="DrawBoard_DrawingLineCompleted"
                        BackgroundColor="AliceBlue"/>
  • DrawingView’s properties

    • WidthRequest and HeightRequest: These properties help to set the width and height respectively.
      Keep in mind that you must add both properties to your DrawingView in order for it to be displayed correctly in your app.
    • LineColor: It’s the color of the drawing line.
    • LineWidth: It’s the width of the drawing line.
    • IsMultiLineModeEnabled: By default, the DrawingView allows only one stroke to be drawn at a time. The IsMultiLineModeEnabled property allows you to change this and draw multiple lines at once. It receives Bool values. To activate it you need to set the value to True.

    Cleaning the DrawView:

    • If you want to clear the DrawView, add the following
      DrawBoard.Lines.Clear();
    • The clear function can be called using the button click event and can be called like below
      <Button Text="Clear Board" 
                          Clicked="Button_Clicked"/>
      void Button_Clicked(System.Object sender, System.EventArgs e)
      {
      	DrawBoard.Lines.Clear();
      }

    Previewing the signature or drawing from DrawView:

    • Add an Image control to your XAML.
    • <Image x:Name="ImageView"
             WidthRequest="200"
             HeightRequest="200"/>
  • Add an event for DrawingLineCompleted in the code behind.
  • private void DrawBoard_DrawingLineCompleted(System.Object sender, CommunityToolkit.Maui.Core.DrawingLineCompletedEventArgs e)
    {
    	ImageView.Dispatcher.Dispatch(async() =>
    	{
    		var stream = await DrawBoard.GetImageStream(300, 300);
    		ImageView.Source = ImageSource.FromStream(() => stream);
    	});
    }
Full Code:

Demo

Download Code:

You can download the code from GitHub. If you have any doubts, feel free to post a comment. If you liked this article, and it is useful to you, do like, share the article & star the repository on GitHub.