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
Creating Mobile Apps with Xamarin.Forms Preview Edition 2 by Charles Petzold.epub
Creating Mobile Apps with Xamarin.Forms Preview Edition 2 by Charles Petzold.pdf
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.
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(6556)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6445)
Kotlin in Action by Dmitry Jemerov(5094)
Odoo 15 Development Essentials - Fifth Edition by Daniel Reis & Greg Mader(3516)
Odoo 15 Development Essentials by Daniel Reis(2837)
React Native - Building Mobile Apps with JavaScript by Novick Vladimir(2551)
Learning Angular - Second Edition by Christoffer Noring(2381)
Pride and Prejudice by Jane Austen(2370)
Mobile Forensics Cookbook by Igor Mikhaylov(2040)
Computers For Seniors For Dummies by Nancy C. Muir(2023)
Bulletproof Android: Practical Advice for Building Secure Apps (Developer's Library) by Godfrey Nolan(1893)
Android Development with Kotlin by Marcin Moskala & Igor Wojda(1819)
Building Android UIs with Custom Views by Raimon Ràfols Montané(1812)
1936941139 (N) by Bob Rosenthal(1758)
Building Progressive Web Apps: Bringing the Power of Native to the Browser by Ater Tal(1748)
Hands-On Internet of Things with MQTT by Tim Pulver(1732)
Android App Development by Franceschi Hervé J.;(1729)
Ember.js in Action by Joachim Haagen Skeie(1711)
Hands-On Design Patterns with React Native by Mateusz Grzesiukiewicz(1670)
