C# 为什么不是';主窗口关闭后是否自动处理和收集WPF窗口?
我一直在想这个问题很久了,我正在寻找最好的解决办法。要复制此问题,只需:C# 为什么不是';主窗口关闭后是否自动处理和收集WPF窗口?,c#,wpf,visual-studio,garbage-collection,C#,Wpf,Visual Studio,Garbage Collection,我一直在想这个问题很久了,我正在寻找最好的解决办法。要复制此问题,只需: 在新WPF应用程序中添加新窗口 添加以下代码: public MainWindow() { InitializeComponent(); //add code Window1 w1 = new Window1(); //following is in my actual project DataContext = new AllUserQueryiesViewModel(new Co
public MainWindow()
{
InitializeComponent();
//add code
Window1 w1 = new Window1();
//following is in my actual project
DataContext = new AllUserQueryiesViewModel(new CommonQueryTestSubWindow());
}
public class AllVM
{
public AllVM(IOneVMContainer container)
{
OneVMs = new ObservableCollection<OneVM>()
{
new OneVM(container),
new OneVM(container)
};
}
public ObservableCollection<OneVM> OneVMs { get; set; }
public OneVM SelectedOneVM { get; set; }
public void OpenOne()
{
SelectedOneVM.Open();
}
}
public class OneVM
{
IOneVMContainer container;
public OneVM(IOneVMContainer container)
{
this.container = container.NewInstance();
}
//I want to open and close the view from viewmodel
public void Open()
{
container.GetContentCtl().Content = this;
container.Show();
}
public void Close()
{
container.Close();
}
}
//my usercontrol to hold AllVM
public AllUC()
{
InitializeComponent();
DataContext = new AllVM(new OneContainerWindow());
}
//implement by other team members
public partial class OneContainerWindow : Window,IOneVMContainer
{
public OneContainerWindow()
{
InitializeComponent();
}
public IOneVMContainer NewInstance()
{
return new OneContainerWindow();
}
public ContentControl GetContentCtl()
{
return contentCtl1;
}
}
使用上述视图模型:
public class AllVM
{
public AllVM(IOneVMContainer container)
{
OneVMs = new ObservableCollection<OneVM>()
{
new OneVM(container),
new OneVM(container)
};
}
public ObservableCollection<OneVM> OneVMs { get; set; }
public OneVM SelectedOneVM { get; set; }
public void OpenOne()
{
SelectedOneVM.Open();
}
}
public class OneVM
{
IOneVMContainer container;
public OneVM(IOneVMContainer container)
{
this.container = container.NewInstance();
}
//I want to open and close the view from viewmodel
public void Open()
{
container.GetContentCtl().Content = this;
container.Show();
}
public void Close()
{
container.Close();
}
}
//my usercontrol to hold AllVM
public AllUC()
{
InitializeComponent();
DataContext = new AllVM(new OneContainerWindow());
}
//implement by other team members
public partial class OneContainerWindow : Window,IOneVMContainer
{
public OneContainerWindow()
{
InitializeComponent();
}
public IOneVMContainer NewInstance()
{
return new OneContainerWindow();
}
public ContentControl GetContentCtl()
{
return contentCtl1;
}
}
因此,我想知道是否不可避免地在ViewModel中存储一个窗口,这导致了主要问题。我知道您一定有更好的解决方案,请帮助。首先不要在构造函数中实例化新窗口 现在,对于您的问题,您需要设置窗口的:
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Window w = new Window() { Owner = this };
}
这样子窗口应该被正确地处理。只要应用程序仍然有活动窗口实例,WPF dispatcher循环就会继续运行。您的Window1对象。它不可见,因为您从未调用过它的Show()方法,而且您自己也无法关闭窗口,因为您丢失了对窗口的引用,这一事实并不会改变结果 不清楚您为什么会考虑这样做,但一种解决方法是在主窗口关闭时强制dispatcher循环退出:
protected override void OnClosed(EventArgs e) {
base.OnClosed(e);
Application.Current.Dispatcher.InvokeShutdown();
}
还有很多其他方法,比如将“w1”作为类的字段,以便可以调用其Close()方法。或者让它成为一个拥有的窗口,使它与主窗口同时关闭。或者显示它,以便用户能够控制应用程序中的窗口。你选吧。这不是MVVM。DataContext是用来保存一个视图模型,而不是一个视图(窗口)。。。设置
DataContext
。我建议您在尝试编写更多代码之前,尤其是在这里询问更多问题之前,花一些时间阅读MSDN上的页面。@Clemens上面的代码不是MVVM(只是为了复制,我的实际DataContext继承了ViewModelBase),但我在项目中使用的是MVVM。现在我已经删除了问题中的MVVM单词,问题仍然是一个问题。这可能是一个使用或不使用MVVM的问题。该问题与是否为DataContext
属性无关。添加一个成员,如private Window=new Window()代码>将显示相同的行为。我想说的是,窗口必须以某种方式关闭。@Clemens好的,我已经按照您的建议更新了我的代码。虽然从未调用过Window.Show(),但dispatcher循环是否仍在运行?为什么会这样?这样做没有多大意义me@LeiYang:您需要将子窗口的所有者设置为主窗口。你的任务是弄清楚你的结构是如何形成的。但基本上你可以做以下肮脏的把戏:someWindow.Owner=Application.Current.MainWindow代码>