How to switch view/displayed XAML file in Visual Studio - c#

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?

Seems like you didn't get your answer.
If you'd like some MVVM startup points, have a look at the following:
The agile warrior: simple mvvm walkthrough
Code project: MVVM basic to advance
Code project: Model-View-ViewModel (MVVM) Explained
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>

Related

Transitioning from Windows Forms to WPF

For a long time now, I have been stuck with Windows Forms development (started with VB6, and has continued through to C# .NET 4.5), and I have pretty much hit the limit of what Windows Forms can do, both using pure .NET, and special effects with Native Code.
I have tried to learn WPF and XAML, but I get stuck right at WPF's new designer. It really seems very difficult to use in comparison to the Windows Forms designer.
I want to know if there are any alternatives to .NET's WPF designer, that are more suited to Windows Forms developers?
I like to blog about beginner articles for WPF, and there are a few in particular that may help you out:
Understanding the change in mindset when switching from WinForms to WPF
What is this "DataContext" you speak of?
A Simple MVVM Example
To summarize, the biggest difference between Winforms and WPF is that in WPF your data layer (the DataContext) is your application, while in Winforms your UI layer is your application.
To look at it another way, with WPF your application consists of the objects you create, and you use Templates and other UI objects to tell WPF how to draw your application components.
That's the opposite of WinForms where you build your application out of UI objects, and then supply them with the data needed.
Because of this, the designer isn't actually used that much since your application components are designed in code, and the designer is only needed to draw a user-friendly interface that reflects your data classes (typically Models and ViewModels)
And personally, I prefer to type all my XAML out by hand since it's faster and doesn't make as much of a mess as the drag/drop WPF designer does, although I do use the Designer on occasion to preview what my UI will look like.
So to your answer your question about if there's other WPF designers suited for WinForms developers, I would suggest that instead of looking for another designer, instead look to learn how to use WPF in the way it's meant to be used. Using WPF like it's WinForms means you miss out on much of what makes it so great :)
Well although, some people don't agree, I would also recomment to not use the VS designer. At least not to create an interface. If you may want to get a first impression of your implementation without starting the application, it's a good viewer at least as long no sophisticated things like Styles and Templates are used. But, IMHO, its drag and drop result should only be used as prototype and therefore be discarded after it's no longer needed.
Here are some reasons which are important for me not to use it.
The VS designer is working with fix margins and alignments (which is usually not necessary, if you're using the layout controls), means you have to touch many controls, if the requirements are changed. If you're deep in XAML and the WPF mechanics you can create an applications which can be modified with small effort, regarding the look and feel.
Since the designer is generating the xaml, the composition is not optimal and the UI may perform badly. I didn't measure it, it's just a feeling.
A much better alternative is MS Blend, although the start is everything else but easy. Its drag and drop result is much better that the result of the VS designer.
But it's a pretty powerful tool, which helps you to use pretty powerful elements to create a state of the art UI. I recommend to visit at least a short workshop to get an idea of its opportunities.
Back to your question, IMHO, and I think many people agree, get yourself a good book e.g. WPF Unleashed and later, if you want to know more about the details, WPF Pro. There are a lot of features which are different to Winforms. You won't get to know them by using any designer. I think that's the best approach.
Please also consider that there are many frameworks and libraries (e.g. MVVM light, WPFToolkit) out there, which are already solving some common problems. So it's not necessary to reinvent the wheel.
I know this is an old question but for the benefit of anyone else looking at this, I think I should redress the balance a bit - reading some of the other answers, I get the feeling that some of the 'don't use the designer' sentiment comes from not using it properly.
This tutorial is quite good to get you going and answers some of the criticisms in the other posts.
For instance, you can switch from the Winforms-like margin-based layout that is the default when you drop a control, to a more WPF-ish style by right-clicking and selecting 'Reset Layout'
This video covers similar ground.
I still prefer the VS2010 designer on balance - VS2013 seems to be a bit buggy when dragging and dropping onto TabItems **, (which my current project uses a lot) - but the VS2013 Document Outline view lets you move things around in that view too, which can be a real plus.
Really, though, to get the most out of WPF and xaml you need to be reasonably fluent in both the designer view and the xaml view and switching between them; if you shy away from the designer, you are missing out on something that can help you a lot.
** Edit - although this seems to have been improved in Update 3 for VS 2013, and in previews of VS14, to date I still get odd behaviour at times.
First of all, in WPF (XAML) in Visual Studio deisgner, you should always use the xaml code to build you UI and do not drag and drop you control! You need to keep your code clean. You can use Expression Blend to help you, it's more graphic oriented with drag and drop, but it's not free.
It's not a big learning curve, but I think you should learn how to do your xaml by hand instead of searching for alternative.
I've been through this process as you did. Afterwards I was teaching everyone in my company WPF. There are a couple of important lessons I have learned and everybody I know who works with WPF.
If you are working with UI controls in the code behind, .... Then you are doing it wrong. There is absolutely no need for you to deal with UI controls in the code behind.
You do not need the visual developer for clicking on it. You are much more productive by only dealing with XAML.
Use Copy/Paste. Do not trust in your typing capabilities. It will save a lot of headaches.
Think of the XAML just as a window that loks over the data. In the code behind you are changing the data. In XAML you are defining how the UI will interpret the data.
Converters are amazing. As soon as you get a key amount of Converters, your productivity will rocket Sky high. They will take over the role of the crazy amount of control eventhandlers that hide or resize, or what ever about UI,
It makes UI development fun. Especially once you find out how it likes to play along with Asyc processes. It really takes away a lot of the headaches that were caused by Winforms.

Creating your own forms designer

I would like start writing an application that will let my user design the interface by choosing some basic controls as Labels, TextBoxes etc. Once done I would like to save the layout in an xml file and be able later on runtime to reconstruct what user choose.
Is there anything similar ready and pre-made?
Can you suggest me the approach?
Any suggestion is welcome!
Cheers
Creating your own designer is a lot like building your own submarine in your basement. Programmers tend to take the quality of the Winforms or WPF designers as a standard of measure. Both are however the result of multi man-year efforts at Microsoft. Reproducing their work from scratch is a daunting task.
Best thing to do is leverage what's already done by them. Possible with the Winforms designer, this magazine article is excellent to help you get started. It is dated but I'm fairly sure it is still relevant, little has changed in Winforms in the past 6 years.

WPF and C# project

Hy.I am currently working on a application in windows forms c#.My application is almost finished but the design is awful,i mean is very simple.I've searched some things about WPF and i think is a very good way to improve my design...But my question is:For using WPF only for my interface..if i will make copy -paste of my code, and i will modify only buttons and colors....will my project continue to work?Or I should started from 0...?
Thank you
In general I would recommend to start from scratch. If you aren't doing a complicated architecture here and have simple event handlers for WinForms you can pretty much stick with the same structure in WPF.
Architecturally speaking it would have of course been better to isolate the business logic from the event handlers. In that regard you might want to take a look at MVVM (and the MVVM Light Toolkit) and Prism.
We have successfully put a WPF shim around one of our very large WinForms apps and hosted the WinForms parts inside of a windows forms host. This allows you to convert one part at a time. This might be a route to take if this project is bigger or you are not sure you will be able to convert the entire application in one shot.
If your win forms app is designed properly you should be able to reuse all but your UI code. In my opinion WPF is far superior to win forms and you should definatly consider using it but it will take some learning to get started, dont do thus if you have a deadline coming up soon.
I also recommend you to move to the MVVM and WPF anyway if your project it is not so big you can use only WPF for interface and copy and paste your code. But from architecture point of view using the MVVM pattern you can make better separation of the interface (View) and you logic (Model) if you will use MVVM in the future will be easier to maintain and change your application

WPF How to design a multi-window Application?

im looking for some tips and hints how i can build up my Application in a good way.
So here are some informations: I have at the moment the Mainwindow designed with a grid which holds 4 frames. its looking like this.
At the top is a menu which let you select the different Pages. I have for the main area about 8 pages for e.g. a settings page and 4 content pages. The Sidebar displays some additional informations which can be choosen by the user and the bottom shows only a page when 1 of the content pages are opened.
So my content pages have ViewModels behind to fill them and im trying to use commands in the near future when im understand all features of them ;). So im trying to use the MVVM way, but im very new at this.
I don't really need a navigation menu like it is given from navigation windows.
So is it the best way for an application like that to use Frames or are there any better solutions?
Frames support the built-in navigation infrastructure of WPF so they're a good choice if you plan to use that. It does have some problems (at least some people think so, including me) but for simple/small applications it will do just fine. Of course they'll also work if you want to roll your own light-weight navigation system, like I usually do. I generally navigate by sending instances of ViewModels around and setting them as the contents of windows or content controls, and letting DataTemplates 'dress' them up as they are defined in the views. For larger projects I'd always recommend one of the awesome frameworks which include navigation systems like Prism or Magellan.
Basically it all depends on your requirements; there are a lot of ways to get it right. It's good that you're trying to use MVVM, try to stick with that and it will generally automatically guide you in a good direction.
I'd suggest using a docking library like AvalonDock to spice your application up a bit... however I have a bitter experience with it as it's not MVVM-friendly. I recently spent the better part of a week wrestling with it to make it usable enough in a MVVM scenario and I'm not sure it can ever be done. There may be other libraries that handle things better, but (full-featured, like in Visual Studio) docking is a complicated matter so maybe don't go there. Sorry, I probably felt the need to vent after the AvalonDock frustrations.

XAML Based Level Editor

I would like to create a level editor for Silverlight with XAML. What resources would be useful for research before starting such a project?
Here is some information to clarify.
I made 1 silverlight game and released it so far.
The I would like to have an overhead style action game. The map would be much larger than the current viewable space.
WPF has a steep learning curve, don't get dis-heartened it's brilliant, but it has so many nooks and corners that it'll take a while to get comfortable. For a start there's a really good blogging community at WPFDisciples all of these are worth reading. Codeplex has some good libaries for 3D, physics engines etc and reverse engineering others code is a good way to learn.
A couple of good books are Windows Presentation Foundation Unleashed and Petzold's Applications = code + markup is one of the few books/places that doesn't preach that everything has to be done in Xaml
An area that takes some getting used to is binding, lots of stuff out there but it can be complex, and it's a impossible to debug until you have the magic knowledge. Binding in WPF isn't just about data, it's about linking all UI elements and behaviours together - it's really important.
Finally you have a choice between Silverlight and WPF, they're not the same. Although Silverlight is a good sub set of WPF, there are certain things you just can't do.
Finally Finally if you have a graphic design, artist background try using Blend, not VS it's a much better tool for those who are visually aware.
Obviously getting to grips with XAML and how to work with it as a concept is important if you haven't already. An overview of it (using WPF as the example) can be found at MSDN here along with the System.Windows.Markup namespace which has the classes you would use for working with it.

Categories