0

I use MVVM framework and got this tutorial on net: https://code.msdn.microsoft.com/windowsdesktop/How-to-use-MVVM-Pattern-0e2f4571 and http://www.c-sharpcorner.com/UploadFile/raj1979/simple-mvvm-pattern-in-wpf/

Now, my problem is:

I can't display the mainpage.xaml even there is no semantic error. Here's my code on app.xaml.cs:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    BasicWPF.View.MainPage window = new MainPage();
    UserViewModel VM = new UserViewModel();
    window.DataContext = VM;
    window.Show();
}

Can anyone help me? Thanks for any help! :)

Thanks to everyone who helped.

[SOLVED]

Change the startupuri in app.xaml to where the page you want to load. In my case

1: I change it:

StartupUri="View/MainPage.xaml"

2: In app.xaml.cs, I typed in this code:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    BasicWPF.View.MainPage window = new MainPage();
    UserViewModel VM = new UserViewModel();
    window.DataContext = VM;
}

from my previous code, delete this code: window.show(); because it startup the page twice coming from app.xaml and app.xaml.cs. To prevent that, delete: window.show();

Thank you again! :)

Daniel
  • 10,864
  • 22
  • 84
  • 115

3 Answers3

2

Set the starting page in app.xaml, not the app.xaml.cs file - in the Application tag, if there is no property StartupUri - add one and set its value to your page name, this way the page will be automatically shown as soon as your application is started. It should look something like this:

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainPage.xaml">

I am considering that you are doing it this way because you wish to set the DataContext of the page, but there is a better way to set the DataContext of your page and it is by setting it directly into your XAML code. Here is an example:

<Page x:Class="WpfApplication1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525" >
    <Page.DataContext>
        <local:UserViewModel/>
    </Page.DataContext>

xmlns:local is a prefix mapping to the namespace you have set for it. Having this you are able to access the types contained in the namespace using the prefix - local:UserViewModel.

NValchev
  • 2,855
  • 2
  • 15
  • 17
  • Your second suggestion actually works, BUT, it still shows the mainwindow.xaml – Sharkstooth26 Apr 22 '15 at 06:28
  • When I do the first and second example on my code, it runs but it got an error stating that system.exception.io occurs. – Sharkstooth26 Apr 22 '15 at 06:29
  • look here http://stackoverflow.com/questions/6518603/wpf-ioexception-cannot-locate-resource and particularly *change the start-up URI to the fully qualified one* I don't know what is your page fully qualified name but I guess from you post it is *BasicWPF.View.MainPage*, so probably you should type *BasicWPF/View/MainPage.xaml* – NValchev Apr 22 '15 at 06:31
  • BUT, main window still pop up – Sharkstooth26 Apr 22 '15 at 06:40
  • Is there another property StartuUri = "MainWindow.xaml" in the app.xaml file? If there is one remove it, if not, look at your app.xaml.cs file and see if it is shown from there – NValchev Apr 22 '15 at 06:45
0

what you can do is instead of setting data context in app.xaml.cs, just hook up the loaded event of main window and add the following code.

   public MainWindow()
    {
        InitializeComponent();
        this.Loaded += MainWindow_Loaded;
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        UserViewModel VM = new UserViewModel();
        this.DataContext = VM;
    }  

This should work. Dont forget to remove the codes from App.xaml.cs. Thanks

Joseph
  • 1,054
  • 1
  • 11
  • 25
0

SO in your case you have a main window inside that main window you need to load the page. What you can do is add a frame to your mainwindow like

 <Frame x:Name="myFrame"/>

then inside the mainwindow loaded event add the below code

  void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        UserViewModel VM = new UserViewModel();
        this.DataContext = VM;
        myFrame.Content = new MainPage();      
    }

This is like we are adding a frame and loading your view to that frame.

Joseph
  • 1,054
  • 1
  • 11
  • 25