How to get a static reference to a WPF Window?

后端 未结 4 1515
醉酒成梦
醉酒成梦 2021-01-12 05:39

I\'ve tried a huge amount of ways to get a static reference of my window across my program. I need to access all of its members at runtime from different classes, so a stati

4条回答
  •  一个人的身影
    2021-01-12 06:18

    Create a static class that can contain the window object, and then when the window is created, have it pass itself to the static class, from then on the static class can hand out the window object to interested parties even though the window object itself is not static. Something like this. There is no need for your form to be static, you just need a static place to hold the form object.

    public class Core
    {
         internal static MyWindowClass m_Wnd = null;
    
         // call this when your non-static form is created
         //
         public static void SetWnd(MyWindowClass wnd)
         {
             m_Wnd = wnd;
         }
    
         public static MyWindow { get { return m_Wnd; } }
    }
    

提交回复
热议问题