Possible Duplicate:
MainWindow constructor getting called twice
I am developing a WPF application following the MVVM pattern as per the below link.
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
I copied some files from the sample (i.e. viewmodel base, Relaycommang etc) The application is working fine except at startup it displays two instance of Mainwindow.xaml.
I tried a lot to resolve but couldn’t trace out.
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();
}
}
Check your App.xaml file. Your view could be instantiated there as well as in code. Otherwise its hard to tell what the problem is whithout more information.