User Control Initialization in Windows 8
I’ve been working on Building a Custom Charting Controls for Windows 8 and today I’ll talk about does InitializeComponents Work in Windows 8 User Controls,
when you build a Custom control for Windows 8 by Default InitalizeComponents will not be available so you need to build the method on your own to load controls from it
The Initialize Components Method needs to do the following
1. Load your Xaml file and Attach it to the Solution
2. Load your Controls into your Controls
Below is a Sample of how you can construct the InitalizeComponents Method
Code Snippet
- public void InitializeComponents()
- {
- if (_contentLoaded)
- return;
- _contentLoaded = true;
- Application.LoadComponent(this, new System.Uri("ms-appx:///DataVirtualization.Toolkit/Legends/LegendItem.xaml"),
- Windows.UI.Xaml.Controls.Primitives.ComponentResourceLocation.Application);
- rec = (Rectangle)this.FindName("rec");
- text = (TextBlock)this.FindName("text");
- }
as you can See I loaded the xaml file first using Application.LoadComponent
then used the method FindName in usercontrol to load the relative controls to my class, I hope that this post was helpful and thanks