Gradient View in Xamarin.Forms using Magic Gradients



Introduction:

In this tutorial, we will learn how to implement Gradient view in Xamarin.Forms with very simple steps. To achieve the gradient view, we are using Magic Gradients plugin for Xamarin Forms. Magic Gradients is a NuGet that provide a possibility to add beautiful and enhanced gradients into Xamarin.Forms applications. It supports all common Xamarin platforms, such as Android, iOS and UWP. It is built upon SkiaSharp to draw a simple multi-color or multi-layer engaged gradients. Without further delay, we will skip into the coding part of the article.

Magic Gradient:

Magic Gradients provides GradientView control which is just a surface for SkiaSharp inside. Inside GradientView you need to set GradientSource which is a default container. Inside it you can place single gradients LinearGradient, RadialGradient or multiple gradients with GradientCollection as a top element.

Coding Part:

Steps:

I have split the coding part into 3 steps as in the following.

  1. Creating new Xamarin.Forms Projects.
  2. Setting up the libraries.
  3. Implementation of the Gradient Views

Step 1: Creating new Xamarin.Forms Projects

  • Create New Project by Selecting New -> Project -> Select Xamarin Cross Platform App and Click OK.
  • Then Select Android and iOS Platforms as shown below with Code Sharing Strategy as PCL or .Net Standard and Click OK.

Step 2: Setting up the libraries

  • We need to install the NuGet package for Magic Gradients by using any of the below methods.
    1. Search for MagicGradients in NuGet browser.
    2. Using Package Manager CLI: “Install-Package MagicGradients”.
    3. Using the .Net CLI: “dotnet add package MagicGradients”.
    4. Editing your csproj file by adding the following and restoring the project.
    <PackageReference Include="MagiGradients" Version="1.0.0" />
  • We should install it only for our "Core" project.

Step 3: Implementation of the Gradient Views

  • Open your designer file and add the following namespace for magic gradient view.
    xmlns:magic="clr-namespace:MagicGradients;assembly=MagicGradients"
  • After that we can using Magic Gradients as like in the following code snippet. Inside it you can place single gradients LinearGradient, RadialGradient or multiple gradients with GradientCollection as a top element.
  • Here, we have taken LinearGradient as an example.
    <magic:GradientView VerticalOptions="FillAndExpand">
        <magic:GradientView.GradientSource>
            <magic:LinearGradient Angle="90">
                <magic:GradientStop Color="Orange" Offset="0" />
                <magic:GradientStop Color="#ff0000" Offset="0.6" />
            </magic:LinearGradient>
        </magic:GradientView.GradientSource>
    </magic:GradientView>
  • We can use gradient collection for more than one gradient sources like below.
    <magic:GradientView VerticalOptions="FillAndExpand">
        <magic:GradientView.GradientSource>
            <magic:GradientCollection>
                <magic:LinearGradient Angle="45">
                    <magic:GradientStop Color="Orange" Offset="0" />
                    <magic:GradientStop Color="#ff0000" Offset="0.6" />
                </magic:LinearGradient>
                <magic:LinearGradient Angle="90">
                    <magic:GradientStop Color="#33ff0000" Offset="0.4" />
                    <magic:GradientStop Color="#ff00ff00" Offset="1" />
                </magic:LinearGradient>
            </magic:GradientCollection>
        </magic:GradientView.GradientSource>
    </magic:GradientView>

Output

Download Code

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

Share this

Related Posts

Latest
Previous
Next Post »

2 comments

comments
6 August 2020 at 00:49 delete

Great Article
android based projects

Java Training in Chennai

Project Center in Chennai

Java Training in Chennai

projects for cse

The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

Reply
avatar
31 August 2020 at 20:01 delete

Serves24 is Best refrigerator repair In Hyderabad We Provided Door Step Repair And Service & Provide best in class. Get 6 months repair guarantee. Book Now just @249/-. at most affordable prices
LG refrigerator repair in Hyderabad
refrigerator repair in Hyderabad
fridges repair Serivces
Samsung refrigerator repair in Hyderabad
IFB washing machine repair in Hyderabad
fridges repairs hyderabad

Reply
avatar

Please Comment about the Posts and Blog