I was studying the documentation found in the microsoft page because i'm willing to learn xamarin.forms. Whenever a new topic is introduced i try to write a small application that puts to test what i've just learned. While experimenting with xaml markup extensions i found out that one of the examples shown in the microsoft documentation doesn't work for me, not even when i copy it from the website and paste it in my vs project.
The C# code is this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace XamarinLab
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
}
public class HslColorExtension : IMarkupExtension<Color>
{
public double H { set; get; }
public double S { set; get; }
public double L { set; get; }
public double A { set; get; } = 1.0;
public Color ProvideValue(IServiceProvider serviceProvider)
{
return Color.FromHsla(H, S, L, A);
}
object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
{
return (this as IMarkupExtension<Color>).ProvideValue(serviceProvider);
}
}
}
and the xaml code is
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:XamarinLab"
x:Class="XamarinLab.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<Style TargetType="BoxView">
<Setter Property="WidthRequest" Value="80" />
<Setter Property="HeightRequest" Value="80" />
<Setter Property="HorizontalOptions" Value="Center" />
<Setter Property="VerticalOptions" Value="CenterAndExpand" />
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<BoxView>
<BoxView.Color>
<local:HslColorExtension H="0" S="1" L="0.5" A="1" />
</BoxView.Color>
</BoxView>
<BoxView>
<BoxView.Color>
<local:HslColor H="0.33" S="1" L="0.5" />
</BoxView.Color>
</BoxView>
<BoxView Color="{local:HslColorExtension H=0.67, S=1, L=0.5}" />
<BoxView Color="{local:HslColor H=0, S=0, L=0.5}" />
<BoxView Color="{local:HslColor A=0.5}" />
</StackLayout>
</ContentPage>
when i try to run the app the following exception pops up
markup extension not found for local:HslColorExtension
The example i copied is found at : https://learn.microsoft.com/it-it/xamarin/xamarin-forms/xaml/markup-extensions/creating
It's the very first example, thanks in advance for any help.
You need to create a new File Class, called HslColorExtension
in your project, rather than creating a class in the MainPage file, where it won't recognize the namespace or the class created.
Just move that part to a new File and you should be all set up.
You can check the Offical Sample, to see the structure, the files and how to setup.