Поделиться через


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

  1. public void InitializeComponents()
  2.         {
  3.             if (_contentLoaded)
  4.                 return;
  5.  
  6.             _contentLoaded = true;
  7.  
  8.             Application.LoadComponent(this, new System.Uri("ms-appx:///DataVirtualization.Toolkit/Legends/LegendItem.xaml"),
  9.                 Windows.UI.Xaml.Controls.Primitives.ComponentResourceLocation.Application);
  10.  
  11.             rec = (Rectangle)this.FindName("rec");
  12.             text = (TextBlock)this.FindName("text");
  13.         }

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