应用程序显示mainpage.xaml的多个实例



可能重复:
MainWindow构造函数被调用两次

我正在按照MVVM模式开发一个WPF应用程序,如下链接所示。http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

我从示例中复制了一些文件(即viewmodel base、Relaycommang等)该应用程序运行良好,只是在启动时显示了Mainwindow.xaml的两个实例

我试了很多办法想解决,但都找不到。

 public partial class App : Application
{
    static App()
    {
        // This code is used to test the app when using other cultures.
        //
        //System.Threading.Thread.CurrentThread.CurrentCulture =
        //    System.Threading.Thread.CurrentThread.CurrentUICulture =
        //        new System.Globalization.CultureInfo("it-IT");

        // Ensure the current culture passed into bindings is the OS culture.
        // By default, WPF uses en-US as the culture, regardless of the system settings.
        //
        FrameworkElement.LanguageProperty.OverrideMetadata(
          typeof(FrameworkElement),
          new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
    }
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        MainWindow window = new MainWindow();
        // Create the ViewModel to which 
        // the main window binds.
        string path = "Data/customers.xml";
        var viewModel = new MainWindowViewModel(path);
        // When the ViewModel asks to be closed, 
        // close the window.
        EventHandler handler = null;
        handler = delegate
        {
            viewModel.RequestClose -= handler;
            window.Close();
        };
        viewModel.RequestClose += handler;
        // Allow all controls in the window to 
        // bind to the ViewModel by setting the 
        // DataContext, which propagates down 
        // the element tree.
        window.DataContext = viewModel;
        window.Show();
    }
}

检查App.xaml文件。您的视图可以在那里实例化,也可以在代码中实例化。否则,在没有更多信息的情况下,很难判断问题出在哪里。

最新更新