Application.LoadComponent makes the application freeze on shutdown

If I use a Application.LoadComponent()UserControl, page or window to load, my application freezes when I try to close it.

The application closes, but the process continues to work. Easy to spot when debugging.

I tested it under Windows 7 64bit and Vista 32bit. In both cases, I used VS2008 and .NET 3.5.

Reproduction can be created by creating the wpf application as follows:


    public partial class Window1 : Window {
        public Window1() {
            InitializeComponent();
        }
        public void LoadCopy() {
            var uri = new Uri("/WpfApplication1;component/window1.xaml", UriKind.Relative);
            var copy = (Window)Application.LoadComponent(uri);
            MessageBox.Show(copy.Title);
        }
        private void Button_Click(object sender, EventArgs e) {
            LoadCopy();
        }
    }

Does anyone know what could happen? And how to solve it?

+3
source share
3 answers

Try to assign the owner of the created assembly, i.e.

copy.Owner = this;

I could close your example after that.

+1

, , LoadComponent() , (http://msdn.microsoft.com/en-us/library/system.windows.application.mainwindow.aspx), uri, Window1. , , , , , Window1 (A.K.A. )! , , , ( , , ).

LoadComponent() Window1, , uri, StartupUri :

<Application
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 StartupUri="Window1.xaml"> <!-- change this -->
</Application>

Application.ShutdownMode(http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx) OnLastWindowClose:

<Application
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 StartupUri="Window1.xaml"
 ShutdownMode="OnLastWindowClose">
</Application>
+1

I am building your application on Windows 7 32bit in .Net 4.0 and 3.5. I work great for me. I think the problem is configuration related. What configuration do you have? Are you referencing any builds other than default WPF project references?

0
source

Source: https://habr.com/ru/post/1769354/


All Articles