Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Application.Current == null in a WinForms application?

Tags:

c#

winforms

Why does Application.Current come out to null in a WinForms application? How and when is it supposed to be set?

I am doing:

 static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.Run(new MainForm());
        }
    }
like image 950
Denis Avatar asked Mar 09 '16 21:03

Denis


People also ask

Will WinForms be deprecated?

WinForms is based on the user32/GDI technology that has existed since the dawn of modern Windows. It is not going to go anywhere, in all senses of the phrase: it won't get new features; it won't get support dropped.

How do I run a WinForms application in the browser?

You can right-click on the icon and then click on the 'Open Web Browser'. A Web browser window will open and your application will be running inside.

What is WinForms good for?

Windows Forms is a UI framework for building Windows desktop apps. It provides one of the most productive ways to create desktop apps based on the visual designer provided in Visual Studio. Functionality such as drag-and-drop placement of visual controls makes it easy to build desktop apps.


2 Answers

Application.Current is Specific for WPF Application. Therefore when you are using WPF controls in WinForms Application you need to initialize instance of WPF Application. Do this in your WinForms Application.

if ( null == System.Windows.Application.Current )
{
   new System.Windows.Application();
} 
like image 159
jdaval Avatar answered Oct 13 '22 08:10

jdaval


Well IMHO opinion the other SO answer is not really the way to go for windows forms, although maybe not incorrect.

Normally you would use ISynchronizeInvoke for such a feature in WinForms. Every container control implements this interface.

You'll need to BeginInvoke() method to marshall the call back to the proper thread.

Based on your previous question the code would become:

public class SomeObject : INotifyPropertyChanged
{
    private readonly ISynchronizeInvoke invoker;
    public SomeObject(ISynchronizeInvoke invoker)
    {
        this.invoker = invoker;
    }

    public decimal AlertLevel
    {
        get { return alertLevel; }
        set
        {
            if (alertLevel == value) return;
            alertLevel = value;
            OnPropertyChanged("AlertLevel");
        }
    }

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            this.invoker.BeginInvoke((Action)(() =>
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName))), null);

        }
    }
}

Where you pass the owning Form class to the constructor of SomeObject. The PropertyChanged will now raised on the UI thread of the owning form class.

like image 40
Ric .Net Avatar answered Oct 13 '22 07:10

Ric .Net