Creating Mobile Apps with Xamarin.Forms Preview Edition 2 by Charles Petzold

Creating Mobile Apps with Xamarin.Forms Preview Edition 2 by Charles Petzold

Author:Charles Petzold
Language: eng
Format: mobi, epub, pdf
Publisher: Microsoft Press
Published: 2015-07-21T16:00:00+00:00


A custom markup extension

Let’s create our own markup extension named HslColorExtension. This will allow us to set any property of type Color by specifying values of hue, saturation, and luminosity, but in a manner much simpler than the use of the x:FactoryMethod tag demonstrated in Chapter 8.

Moreover, let’s put this class in a separate Portable Class Library so that you can use it from multiple applications. Such a library can be found with the other source code for this book. It’s in a directory named Libraries that is parallel to the separate chapter directories. The name of this PCL (and the namespace of the classes within it) is Xamarin.FormsBook.Toolkit.

You can use this library yourself in your own applications by setting a reference to it or by including the project in your application solution. You can then add a new XML namespace declaration in your XAML files like so to specify this library:

Click here to view code image

xmlns:toolkit="clr-namespace:Xamarin.FormsBook.Toolkit;assembly=Xamarin.FormsBook.Toolkit"

With this toolkit prefix you can then reference the HslColorExtension class in the same way you use other XAML markup extensions:

Click here to view code image

<BoxView Color="{toolkit:HslColor H=0.67, S=1, L=0.5}" />

Unlike other XAML markup extensions shown so far, this one has multiple properties, and if you’re setting them as arguments with the curly-brace syntax, they must be separated with commas.

Would something like that be useful? Let’s first see how to create such a library for classes that you’d like to share among applications:

In Visual Studio, from the File menu, select New and Project. In the New Project dialog, select Visual C# and Mobile Apps at the left, and Class Library (Xamarin.Forms Portable) from the list. Find a location for the project and give it a name. For the PCL created for this example, the name is Xamarin.FormsBook.Toolkit. Click OK. Along with all the overhead for the project, the template creates a code file named Xamarin.FormsBook.Toolkit.cs containing a class named Xamarin.FormsBook.Toolkit. That’s not a valid class name, so just delete that file.

In Xamarin Studio, from the File menu, select New and Solution. In the New Solution dialog, select C# and Mobile Apps at the left, and Class Library (Xamarin.Forms Portable) from the list. Find a location for it and give it a name (Xamarin.FormsBook.Toolkit for this example). Click OK. The solution template creates several files, including a file named MyPage.cs. Delete that file.

You can now add classes to this project in the normal way:

In Visual Studio, right-click the project name, select Add and New Item. In the Add New Item dialog, if you’re just creating a code-only class, select Visual C# and Code at the left, and select Class from the list. Give it a name (HslColorExtension.cs for this example). Click the Add button.

In Xamarin Studio, in the tool menu for the project, select Add and New File. In the New File dialog, if you’re just creating a code-only class, select General at the left and Empty Class in the list. Give it a name (HslColorExtension.cs for this example). Click the New button.

The HslColorExtension.cs file



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.