Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Cannot re-initialize ResourceDictionary instance, When ResourceDictionay referenced from xaml

$
0
0

I have a wpf assembly project that, I am using as a global sytle library (i.e. Windows.Styles.dll). So far it contains 2 files CommonStyles.xaml the root class is a resourceDictionary and MyCustomControl.xaml a usercontrol class which is referenced from an assembly during application start up.

When I reference the CommonStyles.xaml file (i.e. from another assembly), I get an exception when a UserContol that references the style assembly is displayed (Cannot reinitialize resourceDictionay).

<UserControl.Resources>

  <ResourceDictionary>

       <ResourceDictionary.MergedDictionaries>

                        <ResourceDictionary Source="pack://Application:,,,/Windows.Styles;component/CommonStyles.xaml">

       </ResourceDictionary.MergedDictionaries>

 </ResourceDictionary>

</UserControl.Resources>

However when I reference the CommonStyles.xaml file from another assembly in the codebehind of a UserContol no exception occurs.

public partial class CustomView : UserControl

{

   private ResourceDictionary _commonStylesDictionary;

    public CustomView ()

   {

       InitializeComponent();

       _commonStylesDictionary = new ResourceDictionary();

       _commonStylesDictionary.Source = new Uri(/Windows.Styles;component/CommonStyles.xaml, UriKind.RelativeOrAbsolute);

        ApplyStyle();

   }

  public void ApplyStyles()

  {

     var InputItemsControlStyle = commonStylesDictionary["InputItemsControlStyle"] as Style;

      InputItemsControl.Style = InputItemsControlStyle;

  } 

}

Why is that, why does this only work from code behind? 



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>