- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
A simple class that uses our custom type converter in VB.NET
Listing 2.17 A simple class that uses our custom type converter QR Code JIS X 0510 Creator In Visual Basic .NET Using Barcode creator for .NET framework Control to generate, create Denso QR Bar Code image in Visual Studio .NET applications. www.OnBarcode.comRecognizing QR-Code In VB.NET Using Barcode reader for .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.compublic class TestClass : Control { [TypeConverter(typeof(BorderTypeConverter))] public Border Border { get { return (Border)GetValue(BorderProperty); } set { SetValue(BorderProperty, value); } } DataMatrix Drawer In Visual Basic .NET Using Barcode printer for .NET Control to generate, create Data Matrix ECC200 image in VS .NET applications. www.OnBarcode.comUCC.EAN - 128 Maker In VB.NET Using Barcode creation for .NET Control to generate, create UCC-128 image in .NET framework applications. www.OnBarcode.comTypeConverterAttribute
Create EAN / UCC - 13 In VB.NET Using Barcode drawer for .NET framework Control to generate, create EAN-13 Supplement 5 image in Visual Studio .NET applications. www.OnBarcode.comPDF-417 2d Barcode Creation In Visual Basic .NET Using Barcode generator for .NET framework Control to generate, create PDF 417 image in Visual Studio .NET applications. www.OnBarcode.compublic static readonly DependencyProperty BorderProperty = DependencyProperty.Register("Border", typeof(Border), typeof(TestClass), null); } Linear Encoder In Visual Basic .NET Using Barcode generation for Visual Studio .NET Control to generate, create Linear image in .NET framework applications. www.OnBarcode.comUSD - 8 Generator In VB.NET Using Barcode drawer for .NET framework Control to generate, create USD8 image in .NET framework applications. www.OnBarcode.comDownload from Wow! eBook <www.wowebook.com>
Decoding QR Code 2d Barcode In VS .NET Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET framework applications. www.OnBarcode.comQR Code Maker In Java Using Barcode creation for Android Control to generate, create QR Code JIS X 0510 image in Android applications. www.OnBarcode.comCore XAML
Paint UCC - 12 In .NET Framework Using Barcode generator for Reporting Service Control to generate, create EAN / UCC - 14 image in Reporting Service applications. www.OnBarcode.comBarcode Creator In .NET Framework Using Barcode maker for ASP.NET Control to generate, create Barcode image in ASP.NET applications. www.OnBarcode.comThe TypeConverterAttribute that specifies the type converter to use for this specific property in this class is shown in listing 2.17. The attribute is applied to the public property because that s what s used by XAML. The converter is declared on the single property so it ll apply only there and not to all instances of the Border type. It s also important to note that the border isn t actually used for anything other than illustrating how to use a type converter. Finally, listing 2.18 shows the type converter implicitly in use in XAML. Create GS1 128 In None Using Barcode encoder for Excel Control to generate, create UCC-128 image in Microsoft Excel applications. www.OnBarcode.comGS1 DataBar Expanded Creator In VS .NET Using Barcode generation for Visual Studio .NET Control to generate, create DataBar image in .NET framework applications. www.OnBarcode.comListing 2.18 XAML showing the custom Border type converter in use
PDF-417 2d Barcode Generation In None Using Barcode maker for Excel Control to generate, create PDF 417 image in Office Excel applications. www.OnBarcode.comGenerating UPC Symbol In Visual Studio .NET Using Barcode generation for Reporting Service Control to generate, create UPC Code image in Reporting Service applications. www.OnBarcode.com<UserControl x:Class="TypeConverterExample.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TypeConverterExample"> <Grid x:Name="LayoutRoot"> <local:TestClass Border="Red 5" /> </Grid> </UserControl> Reading QR Code In .NET Framework Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET applications. www.OnBarcode.comPainting Barcode In Java Using Barcode encoder for Java Control to generate, create Barcode image in Java applications. www.OnBarcode.comType converter in use
Scanning QR-Code In None Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications. www.OnBarcode.comBarcode Encoder In Java Using Barcode drawer for BIRT Control to generate, create Barcode image in BIRT applications. www.OnBarcode.comBecause we used the XamlReader.Load method, we could easily use any valid color string such as "LemonCream" or "#C830019F". Bonus points if you caught the Star Wars reference in listing 2.18. Colors in XAML
You may have given the color string #C830019F a double-take if you re used to sixdigit HTML hex colors. Colors in Silverlight are typically expressed as eight-digit hex numbers, the first pair representing the alpha component and the remaining three pairs the red, green, and blue components in that order. In the color #C830019F, the values are Alpha: 0xC8, Red: 0x30, Green: 0x01, and Blue: 0x9F. The alpha component is optional, so you may use an HTML-style hex color if you wish. For consistency across the application, I recommend you specify the alpha value and use all eight digits without any shortcuts. Type converters are a great way to extend the flexibility of XAML to include types you create yourself or new representations of existing types. We ve used them in projects to provide serialization support for legacy format strings stored in databases and to extend the known representations of existing types. Now that we understand the basics of XAML and have seen a simple example of dynamically loading XAML to parse a color string, let s take that a bit further and look at runtime loading or more complex content. Loading XAML at runtime
In listing 2.16, we saw a brief example of loading XAML at runtime using XamlReader.Load. Let s expand on that to do more than just some basic color conversion. Download from Wow! eBook <www.wowebook.com>
Loading XAML at runtime
You can use dynamically loaded XAML to create entire sections of the object tree at runtime. This could be useful for rendering user-generated content such as shapes drawn on a screen and saved in a database or for creating highly dynamic controls. The process of loading XAML at runtime is incredibly easy. You only need to rely on the XamlReader class, which belongs to the System.Windows.Markup namespace. This class empowers you to parse XAML and convert it into an in-memory object. This object can be created by a statically visible method called Load. This method takes a string of XAML and converts it to the appropriate object. Then you can insert this object into another UIElement. Listing 2.19 shows this entire process in action. Listing 2.19 Loading and parsing XAML at runtime
Result: XAML: <UserControl x:Class="XamlReaderExample.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"> <Grid x:Name="LayoutRoot"> </Grid> </UserControl> public MainPage() { InitializeComponent(); Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { var element = CreateRectangle(); LayoutRoot.Children.Add(element); } private Rectangle CreateRectangle() { StringBuilder xaml = new StringBuilder(); string ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"; xaml.Append("<Rectangle "); xaml.Append(string.Format("xmlns='{0}'", ns)); xaml.Append(" Margin='5 10 5 15'"); Add to tree
Namespace declaration
Download from Wow! eBook <www.wowebook.com>
Core XAML
xaml.Append(" Fill='Orange'"); xaml.Append(" Stroke='Black' />"); var rectangle = (Rectangle) XamlReader.Load(xaml.ToString()); return rectangle; } XamlReader.Load
This example dynamically creates a rectangle and adds it to the object tree. The code in CreateRectangle simply builds up a string with XAML similar to what we d have inside a regular .xaml file. Note that we need to specify the namespaces used for any segment of XAML we ll pass into XamlReader.Load. The code that adds the generated XAML to the object tree can be seen inside the loaded event. You can of course do more with the element than just add it to the LayoutRoot. Listing 2.20 illustrates how we can take the XAML and integrate it with the managed code representations of XAML constructs to create multiple instances of the rectangle.
|
|