Piero Alberto
Piero Alberto

Reputation: 3943

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

Upvotes: 8

Views: 6736

Answers (1)

Won Hyoung Lee
Won Hyoung Lee

Reputation: 393

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");

Upvotes: 14

Related Questions