8

I have to use the method FindResource("key"). In my MainWindow class, it works.

I've to use it in another class, but I can't refer to it with a new instance of the MainWindow class, because this gives me some problem (not relevant now).

So, I have declared a static method in my MainWindow class, but, thus I can't use "this" in a static method, I have written:

public static string getFromDict(string key){
    View.MainWindow v = new View.MainWindow();
    return v.getResource(key);
}

private string getResource(string key) {
    return this.FindResource(key).ToString();
}

This is still giving me problems, because, as you can see, I create a new instance of MainWindow also here.

So, from another class, how can I use the findResource method? (the resource I want to read are some dicts in xml, included in the project: I already read them correctly in other code).

Piero Alberto
  • 3,823
  • 6
  • 56
  • 108

1 Answers1

14

You don't need to call MainWindow's FindResource if there is resource dictionary.
You can call FindResource as follow,

using System.Windows;

Application.Current.FindResource("YOUR-RESOURCE-KEY");
Won Hyoung Lee
  • 383
  • 2
  • 14
  • I will get married next year but... I think I love you! :D ahahah... sorry the dumb question, I'm a newbie and I was getting crazy...thank you for your perfect tip! :) – Piero Alberto Oct 10 '14 at 15:47