简体   繁体   中英

How to switch view/displayed XAML file in Visual Studio

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 (ie 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?

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.

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM