可能重复:
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();
}
}