0

I've been developing an windows desktop application in Visual Studio 2015, and I've been trying to figure out how to switch between different view files. Essentially when the app begins I can choose which view it is supposed to display first (i.e. MainPage.xaml), however, I have yet to find any resources that describe how to switch between the different views programmatically, say at a button click.

I'm coming from developing an app for IPhone, and in XCode switching views programmatically seems to be the intended way of doing it. My question is how do I switch the views programmatically using C#? And also, is Visual Studio different in that I should be doing this someway else?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
tbecker
  • 5
  • 4
  • Do you have some C# / WPF experience ? Does MVVM sounds remotely familiar ? Are you using any frameworks ? (trying to figure out how to answer this question in a way that will make sense to you) – Noctis Aug 22 '15 at 08:08
  • I don't have experience in C#, I haven't heard of MVVM but I've done some MVC, and no I'm not using any frameworks – tbecker Aug 22 '15 at 08:11
  • Then the answer will be: with great difficulty :) ... Just kidding, the "simplest" way will require some steep learning curve. If no one will answer, i'll try to sit down later and write a comprehensible one for you. – Noctis Aug 22 '15 at 08:14

3 Answers3

2

Seems like you didn't get your answer. If you'd like some MVVM startup points, have a look at the following:

Alternatively, you can also have a look at my article Code Project: The Big MVVM Template.
If you'll read the article, you'll see many of the benefits of the MVVM approach (and why you'd want a framework that will help).
If you just get the code sample and run it, you can see an application running and be able to look at the parts that might interest you.

There's also this answer with lots of other mvvm related resources.

Community
  • 1
  • 1
Noctis
  • 11,507
  • 3
  • 43
  • 82
  • All of these resources are extremely helpful, and they have definitely given me a better idea of MVVM in general. But, more to my original question, how do I physically change a view, or should I keep to one view upon which I just collapse grids (Kind of along the lines of what Contango was suggesting? – tbecker Aug 25 '15 at 23:16
  • Or rather how would I switch the displayed xaml from EX1.xaml to EX2.xaml each with its own viewmodel? – tbecker Aug 25 '15 at 23:25
  • When you say programattically switch the view, what I think you mean is that you want some even to trigger the view to change (say a button click). If you map that to an ICommand in your view model, then in the ViewModel code do something like `MyViewModel2 myViewModel = new MyViewModel2(); EX2 myWindow = new EX2(myViewModel); myWindow.ShowDialog();` This is assuming you pass the viewmodel in to the window constructor, which you can set up in the EX2.xaml.cs file. – Paul Gibson Jan 12 '19 at 00:17
0

First, get to grips with MVVM. There are some good tutorials online for this.

In my experience, instead of switching the View programmatically, it's much more performant to create the views and hide/show them on demand with Visibility.Show or Visibility.Collapsed. This will result in snappy, fast applications.

As noted in the comments, MVVM is a steep learning curve, but it's worth it: an application written in MVVM is very maintainable and testable.

Update

As requested, I will recommend some tutorials on MVVM. You could read a book, but that won't really work as well as watching a tutorial video and following along with it using Visual Studio. There is something magical about copying what the tutor does as they code up the sample, you really start to understand it.

I would recommend PluralSight or, perhaps, Lynda. These are not free, but the quality is excellect and the material is comprehensive. For the record, I have no affiliation with these companies.

Community
  • 1
  • 1
Contango
  • 76,540
  • 58
  • 260
  • 305
  • 1
    Would you mind linking to some online tutorials, then I'll accept this answer. – tbecker Aug 22 '15 at 19:41
  • Sure, just added some recommendations. – Contango Aug 24 '15 at 17:53
  • There is something , but then again, sometimes you just sit there watching the person talk ... :) . I've seen some nice MVVM tutorials by Laurent bo[something], the guy who wrote MVVM Light. – Noctis Aug 25 '15 at 02:29
0

Assuming you used a project template, it should have generated a base class called App.xaml as an application starting point, and something like MainWindow.xaml as the base start page.


For WPF/XAML

App.Current.MainWindow = <class that implements System.Windows.Window>

If you are using Xamarin.Forms XAML, than it changes to

App.Current.MainPage = <class that implements Xamarin.Forms.Page>
Tezra
  • 8,463
  • 3
  • 31
  • 68