Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use "FindResource" from other classes of the application

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).

like image 667
Piero Alberto Avatar asked Oct 10 '14 15:10

Piero Alberto


1 Answers

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");
like image 134
Won Hyoung Lee Avatar answered Oct 15 '22 17:10

Won Hyoung Lee