Martyn Ball
Martyn Ball

Reputation: 4895

Reference object in App.xaml.cs in new window

I'm attempting to create an object which will hold any values the application will need globally during its runtime. I thought i'd use App.xaml.cs as this is the heart of the application if I understand correctly as that code is run first and kept in memory.

Getting this error on the .inProgress part of the code at the bottom of this post:

'App' does not contain a definition for 'inProgress' and no extension method 'inProgress' accepting a first argument of type 'App' could be found (are you missing a using directive or an assembly reference?)

App.xaml.cs

public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            //Startup
            Window main = new MainWindow();
            main.Show();

            //Bind Commands
            Classes.MyCommands.BindCommandsToWindow(main);

            //Create runtime objects
            var runtime = new runtimeObject();
        }

        public static explicit operator App(Application v)
        {
            throw new NotImplementedException();
        }
    }
    /// <summary>
    /// Global values for use during application runtime
    /// </summary>
    public class runtimeObject
    {
        private bool _inProgress = false;

        public bool inProgress
        {
            get { return _inProgress; }
            set { _inProgress = value; }
        }
    }

Here i'm trying to access the runtime object so that I can see if the application can be closes, bare in mind this may not be needed, but I would need to do similar tasks like this other than close the window.

Classes > Commands.cs

bool inProgress = (System.Windows.Application.Current as App).inProgress;

Upvotes: 1

Views: 1501

Answers (1)

Sean Beanland
Sean Beanland

Reputation: 1118

It looks like you need to add a property to access the runtime object. Currently you're just creating an instance in OnStartup method. Assign that instance to a property:

public partial class App : Application
{
    public static runtimeObject runtime { get; set; };

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        //Startup
        Window main = new MainWindow();
        main.Show();

        //Bind Commands
        Classes.MyCommands.BindCommandsToWindow(main);

        // Create runtime objects
        // Assign to accessible property.
        runtime = new runtimeObject();            
    }

    public static explicit operator App(Application v)
    {
        throw new NotImplementedException();
    }
}

Then access the property from your commands logic:

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

Upvotes: 1

Related Questions