I am developing a WinUI 3 app that includes TabView. Now I want to create a new window when a tab is dragged out of the current window. But I cannot create another new window because App.m-window
is set to the first created MainWindow
. The code looks like this:
//App.xaml.cs
public partial class App : Application
{
public App(){ this.InitializeComponent();}
public static MainWindow m_window; //This is static so I can call it with "App.m_window"
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
m_window = new(); //m_window is set to the first created window
m_window.Activate();
}
}
//MainWindow.xaml.cs
public sealed partial class MainWindow : Window
{
public TabView CurrentTabview; //Some other properties
public MainWindow(){ this.InitializeComponent();}
}
//Page.xaml.cs This page is navigate by a tabview in MainWindow
public sealed partial class TabPage : Page
{
public TabPage(){
this.InitializeComponent();
TabView Item = App.m_window.CurrentTabview;
// I have to call current "MainWindow" properties by using "App.m_window"
}
}
That worked fine. But because I hope to do something like drag the tab out of the current window and automatically create a new window, I couldn’t call App.m_window
anymore. Or the page from two different window will call the same MainWindow
.
I have tried the solution in How to retrieve the window handle of the current WinUI 3 MainWindow from a page in a frame in a NavigationView control on the MainWindow. But That doesn’t work.
My previous solution of calling MainWindow
was making it a static variable in App
class. So now I tried with creating a variable ThisWindow
in page
and set it to App.m_window
, so ThisWindow
becomes local. Like this, but ThisWindow
is “Null”
//Page.xaml.cs
private MainWindow ThisWindow;
void GetCurrentWindow(){
ThisWindow = App.m_window;
if ( ThisWindow != null)
{
Debug.WriteLine("not null");
}
else { Debug.WriteLine("Null"); }
// Output: Null
}
How can I solve it