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

时间:2012-09-04 12:13:09

标签: wpf

  

可能重复:
  MainWindow constructor getting called twice

我正按照以下链接开发遵循 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();
    }

}

1 个答案:

答案 0 :(得分:0)

检查您的App.xaml文件。您的视图可以在那里以及代码中实例化。否则很难说出问题是什么,而不是更多的信息。