Dynamically loading resource dictionary files to a wpf application gives an error

前端 未结 3 1119
天命终不由人
天命终不由人 2020-12-03 12:08

I am trying to add a xaml resource file dynamically using the statement,

Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Sou         


        
3条回答
  •  -上瘾入骨i
    2020-12-03 12:26

    To load a content file, you can call the GetContentStream method of the Application class, passing a pack URI that identifies the desired content file.

    Checkout

    http://msdn.microsoft.com/en-us/library/aa970494.aspx#Content_Files

    EDIT

    I did it successfully like this

        Uri uri = new Uri("Resources/MyDict.xaml", UriKind.Relative);
        StreamResourceInfo info = Application.GetContentStream(uri);
        System.Windows.Markup.XamlReader reader = new System.Windows.Markup.XamlReader();
        ResourceDictionary myResourceDictionary = 
                                       (ResourceDictionary)reader.LoadAsync(info.Stream);
        Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
    

提交回复
热议问题