1

I used to have an instance of a custom class define in app.xaml.cs so I could access it anywhere in my application. How ever I have now changed that so that an instance of my class is made within my Application Resources.

App.xaml

<Application x:Class="Duplicate_Deleter.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Duplicate_Deleter"> <Application.Resources> <local:runtimeObject x:Key="runtimeVariables" /> </Application.Resources> </Application> 

App.xaml.cs This is the class.

using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Threading.Tasks; using System.Windows; namespace Duplicate_Deleter /// <summary> /// Global values for use during application runtime /// </summary> public class runtimeObject { //Can the application be closed? private bool _inProgress = false; public bool inProgress { get { return _inProgress; } set { _inProgress = value; } } //Selected folder to search in private string _fromFolder = "testing string"; public string fromFolder { get { return _fromFolder; } set { _fromFolder = value; } } } } 

My problem is now, I need to be able to access this instance of the class within my code behind in my commands namespace. Below you can see one of the commands, the App.runtime used to work when the instance was within App.xaml.cs.

Classes > Commands.cs

public static void CloseWindow_CanExecute(object sender, CanExecuteRoutedEventArgs e) { if (App.runtime.inProgress == true) { e.CanExecute = false; } else { e.CanExecute = true; } } 

How do I now reference my class instance from within my commands?

1 Answer 1

2

You can use TryFindResource anywhere in code:

public static void CloseWindow_CanExecute(object sender, CanExecuteRoutedEventArgs e) { // Find the resource, then cast it to a runtimeObject var runtime = (runtimeObject)Application.Current.TryFindResource("runtimeVariables"); if (runtime.InProgress == true) { e.CanExecute = false; } else { e.CanExecute = true; } } 

If the resource is not found it will return null. You could add null checking to avoid an InvalidCastException.

Sign up to request clarification or add additional context in comments.

6 Comments

That's weird, i'm getting an error with "TryFindResource", error: The name 'TryFindResource' does not exist in the current context
Hmm that's weird, how about FindResource instead?
I figured it out - it's because you're calling it in a class outside of the main window. Use Application.Current.TryFindResource(..), updating my answer
Awesome thanks! One more related question, how would I use code behind to update a property in my XAML class instance?
You're welcome! In the code I'm showing above you are getting the object from your XAML class instance. Just use it as any other object as you wish
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.