title: Windows Store Apps : Loading styles from a customcontrol class library via reflection, OnApplyTemplate is not called?
body: <p>I am trying to load a custom control library via reflection in windows 8 Metro C# App, the library is loaded but the styles specified in generic.xaml are not loaded, eventually I tried to load the generic.xaml by making it as an Embedded resource and ,then extracted the Generic.xaml to a location and specified the location of it as uri of a ResourceDictionary object, but it throws an error </p>
  
  <pre><code>"Failed to create a 'System.Type' from the text local:CustomControl1"
  </code></pre>
  
  <p>I cannot create a nuget package or extension SDK as unfortunately that is not my requirement, 
  Below sample code I wrote to copy the generic.xaml and load it in a resource dictionary</p>
  
  <pre><code>public sealed class CustomControl1 : Control
  {
      public CustomControl1()
      {
          this.DefaultStyleKey = typeof(CustomControl1);
          Assembly CurrentAssembly = typeof(CustomControl1).GetTypeInfo().Assembly;
          var names = CurrentAssembly.GetManifestResourceNames();
          var stream = CurrentAssembly.GetManifestResourceStream(names.First());
          //generic.xaml is an embedded resource in the current assembly
          if (stream != null)
          {
              //created new generic.xaml here
              var file = ApplicationData.Current.LocalFolder.CreateFileAsync("Generic.xaml", CreationCollisionOption.ReplaceExisting).Completed = (o, a) =&gt;
              {
                  var storageFile = o.GetResults();
  
                  var s = storageFile.OpenStreamForWriteAsync().Result;
  
                  var length = (int)stream.Length;
                  byte[] bytes = new byte[length];
                  int output = stream.Read(bytes, 0, length);
  
                  s.Write(bytes, 0, length);
                  s.Flush();
                  s.Dispose();
  
                  var asyncResult = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =&gt;
                  {
                      var resourceDict = new ResourceDictionary();
                      var uri = new Uri("ms-appdata:///local/" + storageFile.Name);
                      resourceDict.Source = uri;
                  });
              };
          }
      }
  
  
      // OnApplyTemplate is not called without loading the style from generic.xaml
      protected override void OnApplyTemplate()
      {
          base.OnApplyTemplate();
      }
  }
  </code></pre>
  
  <p>The below code I wrote in the custom control library's constructor, so that the control template can be set without generic.xaml</p>
  
  <p>Here since the attribute TargeType="local:CustomControl1" is not present the control gets loaded properly, here since I loaded the style in the constructor, the OnApplyTemplate gets called</p>
  
  <pre><code>StringBuilder sb = new StringBuilder();
  sb.Append(@"&lt;ControlTemplate  
  xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""   
  xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""  
  xmlns:d=""http://schemas.microsoft.com/expression/blend/2008""
  xmlns:mc=""http://schemas.openxmlformats.org/markup-compatibility/2006""&gt;");
  sb.Append(@"&lt;Border Background=""{TemplateBinding 
  Background}""                                                             
  BorderBrush=""{TemplateBinding BorderBrush}""
  BorderThickness=""{TemplateBinding BorderThickness}""&gt;
     &lt;Grid&gt;
        &lt;Button x:Name=""Tbx1"" Content=""Hello World"" Foreground=""HotPink""   
         HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch""/&gt;
    &lt;/Grid&gt;
  &lt;/Border&gt;");
  sb.Append(@"&lt;/ControlTemplate&gt;");
  this.Template = (ControlTemplate)XamlReader.Load(sb.ToString());
  </code></pre>
  
  <p>but the problem is loading all the styles using XamlReader is not a good idea, unless we are out of Options. As there may be various dependent styles which too have to be loaded.</p>