locked
How to user var as public variable? RRS feed

  • Question

  • Hi all,

    to use variable stored in MainWindow from some Page I use this method ...

    var mw = Application.Current.Windows
            .Cast<Window>()
            .FirstOrDefault(window => window is MainWindow) as MainWindow;

    But I need to use these line in every function in Page. More simple it would be If there was a one public variable.  Or is there a more simple way of how to do this? ... of how to use variables between window - page?

    Thank you.

    Tuesday, February 19, 2019 6:23 PM

Answers

  • Hi all,

    to use variable stored in MainWindow from some Page I use this method ...

    var mw = Application.Current.Windows
            .Cast<Window>()
            .FirstOrDefault(window => window is MainWindow) as MainWindow;

    But I need to use these line in every function in Page. More simple it would be If there was a one public variable.  Or is there a more simple way of how to do this? ... of how to use variables between window - page?

    Thank you.

    Hi  TakeshiKitano, 

    You can try to define the public static property in App class of your project.

           public static Window usercontrolDPShow
            {
                get
                {
                    return Application.Current.Windows
            .Cast<UsercontrolDPShow>()
            .FirstOrDefault(window => window is UsercontrolDPShow) as UsercontrolDPShow;
    
    
                }
            }

    Then, you can use it in every function.

      MessageBox.Show(App.usercontrolDPShow.Title);

    Best Regards,

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by TakeshiKitano Tuesday, March 5, 2019 9:31 AM
    Wednesday, February 20, 2019 7:56 AM

All replies

  • In case of WPF, try ‘var mw = (MainWindow)Application.Current.MainWindow’.

    • Proposed as answer by Andy ONeill Wednesday, February 20, 2019 2:16 PM
    Tuesday, February 19, 2019 7:01 PM
  • You could post to the WPF forum to see wjat they say.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wpf

    Tuesday, February 19, 2019 9:37 PM
  • Hi TakeshiKitano,

    Thank you for posting here.

    Since this thread is related to WPF, I will move it to WPF forum for suitable support.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wpf

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 20, 2019 5:22 AM
  • Hi all,

    to use variable stored in MainWindow from some Page I use this method ...

    var mw = Application.Current.Windows
            .Cast<Window>()
            .FirstOrDefault(window => window is MainWindow) as MainWindow;

    But I need to use these line in every function in Page. More simple it would be If there was a one public variable.  Or is there a more simple way of how to do this? ... of how to use variables between window - page?

    Thank you.

    Hi  TakeshiKitano, 

    You can try to define the public static property in App class of your project.

           public static Window usercontrolDPShow
            {
                get
                {
                    return Application.Current.Windows
            .Cast<UsercontrolDPShow>()
            .FirstOrDefault(window => window is UsercontrolDPShow) as UsercontrolDPShow;
    
    
                }
            }

    Then, you can use it in every function.

      MessageBox.Show(App.usercontrolDPShow.Title);

    Best Regards,

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by TakeshiKitano Tuesday, March 5, 2019 9:31 AM
    Wednesday, February 20, 2019 7:56 AM
  • In case of WPF, try ‘var mw = (MainWindow)Application.Current.MainWindow’.

    I think you have  design fault really.

    You shouldn't store variables in a window.

    Especially if you will be using them throughout your app.

    It is likely a static class with a static property is more suitable for this purpose.

    Then you could get/set whatever values from any bit of code.

    .

    In terms of the question though.

    MainWindow is a bit confusing.

    It has two meanings.

    In that the default name for your starting window is MainWindow.

    WPF (by default) gives special significance to the first window shown.  This is the main window of the application and Application has a property set to that instance.

    You could remove the MainWindow you're given by default and add a new window called Foo.

    Change app.xaml so it loads up that rather than the default MainWindow.

    That instance of Foo then becomes the main window and you would cast Application.Current.MainWindow to Foo rather than MainWindow.

    If you close Foo then the application will close down. ( Although this can be changed so you need to explicitly close the app down ).

     


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Wednesday, February 20, 2019 2:23 PM