Creating and implementing the LabelShadowEffect (Android)

In this section, we will begin creating the LabelShadowEffect class for the Android section of our TrackMyWalks solution, which will essentially contain platform-specific methods that will be used by our LabelShadowEffect class. The advantage of creating a LabelShadowEffect class is that it's much easier to modify or add additional control properties that will be used by those XAML pages that utilize this class.

Let's start creating the LabelShadowEffect class for our TrackMyWalks app by performing the following the steps:

  1. Ensure that the TrackMyWalks solution is open in the Visual Studio for Mac IDE.
  2. In the TrackMyWalks.Android project, right-click on the CustomEffects folder and choose Add | New File... from the pop-up menu.
  3. Create a new Empty Class called LabelShadowEffect in the CustomEffects folder, as you did in the section entitled Creating and Implementing the ButtonShadowEffect (Android) in this chapter.
  4. Ensure that the LabelShadowEffect.cs file, which is located as part of the TrackMyWalks.Android group, is displayed in the code editor and enter the following code snippet:
     //
// LabelShadowEffect.cs
// Creates a custom Label Shadow Effect using
// PlatformEffects (Android)
//
// Created by Steven F. Daniel on 16/07/2018.
// Copyright © 2018 GENIESOFT STUDIOS. All rights reserved.
//
using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportEffect(
typeof(TrackMyWalks.Droid.CustomEffects.LabelShadowEffect),
"LabelShadowEffect")]

namespace TrackMyWalks.Droid.CustomEffects
{
public class LabelShadowEffect : PlatformEffect
{
protected override void OnAttached()
{
try
{
var control = Control as Android.Widget.TextView;
float radius = 5;
float distanceX = 4;
float distanceY = 4;
Android.Graphics.Color color = Color.White.ToAndroid();
control.SetShadowLayer(radius, distanceX, distanceY, color);
}
catch (Exception ex)
{
Console.WriteLine("Cannot set property on attached control.
Error: " + ex.Message);
}
}

protected override void OnDetached()
{
}
}
}

Let's now take a look at what we covered in the preceding code snippet:

  1. We started by including references to the System, Xamarin.Forms, and Xamarin.Forms.Platform.Android namespaces, so that we can access the classes that are defined in these namespaces.
  2. We added the ExportEffect assembly attribute to our LabelShadowEffect class, to register the custom effect with Xamarin.Forms so that we can reference this in our XAML pages. You will have noticed that we don't need to add the ResolutionGroupName assembly attribute, as there can only be one declaration of this for the Android platform, and any attempt to add the assembly attribute again will result in a compilation error occurring.
  3. We ensured that our LabelShadowEffect class inherits from the PlatformEffect class, so that we can access each of the platform-specific method implementations of the PlatFormEffect class.
  4. We created and implemented the OnAttached method, used the Container property to reference the platform-specific Label control, and applied shadowing effects to our button control by updating the properties for our control.
  5. We created the OnDetached method that will be used to perform any cleanup whenever the control is detached from a Xamarin.Forms Label control.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.80.3