0

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

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

我从示例中复制了一些文件(即视图模型库、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();
    }

}
4

1 回答 1

0

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

于 2012-09-04T12:20:32.443 回答