WPF and C# project - c#

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

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.

WPF Plug-ins in Windows Forms application

I am used to WPF Development. But recently, i am assigned to Windows Forms projects. Fortunately, application framework supports pluggable architecture and that's why i feel i can develop plugin in WPF.
If we safely assume that framework doesn't need more than a interface to detect a plugin, I have following questions:
First of all, is it a good idea to develop WPF plugin for the given scenario?
Are there any guidelines available which i should follow? Any Examples?
Will it be a good idea to design some abstraction layer specially for WPF plugins rather than depending on interface?
Thanks in advance.
Yeah, we have huge project called "Plugins" which is basically WPF App/Windows loaded via additional app domain.
I think in your case depends if you Windows Forms are in .NET 2 or .NET3.5. If it is .NET 3.5 then you dont need additional app domain to load .NET 3.5 with WPF.
If you are already in .NET 3.5 with your WinForms, then you can use ElementHost to host your WPF inside WinForms.
Apart from few glitches (like not repainting the form sometimes), it went quite smoothly in our case
As to your Qs:
Usually it is better to do everything in WPF, but do u have a choice ? (I dont think so)
Lookup an example on WPF ElementHost
Well, I would use MVVM with WPF so that later on you can chnage the UI bit (maybe Silverlight or Win8 metro) without chnaging much of the business layer logic

WPF or Winforms for a beginner? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
WPF vs. Windows Forms
I am starting to learn C# and wonder, when and why I should choose WPF? I also wonder what WCF is. Are they both GUI related stuff to head into?
If I can make a GUI with Winforms, why should I make a GUI with WCF/WPF?
WPF is a more modern technology than Winforms. It has better layout handling through the XAML code and allows for more advanced GUI patterns such as MVVM (Model View View-Model) built on databinding.
If you start out fresh I would suggest going for WPF directly. Stay out of the databinding and MVVM to start with and just put your code in the code behind file.
WCF is Windows Communication Foundations which is not directly related to GUI programming, although many client applications written in WPF or Winforms uses WCF for communication.
WPF is also a great method of deploying updates to multiple users simultaneousnessly. When you publish an update in WPF it is immediately available to all users and does not require installation to run. The user simply hits the xbap and they begin downloading the newest copy of the application.
On top of what has been already said, I'll just add that learning WPF will give you excellent foundations for Silverlight, in case you want to try something different one day (or - less desktop oriented).
You use WPF to provide rich contents in UI. http://msdn.microsoft.com/en-us/magazine/cc163662.aspx

The benefits and hassles of moving entirely to a WPF Project

I have a project that i started as a WinForms application as that was the format i was confortable with at the time. I have since started dabbling in WPF an introduced some WPF UserControls (mainly grids) into my project and absolutely love them.
The question i have is, is there any real advantage to me changing the UI Project of my solution into a purely WPF project, and get rid of any WinForms?
I am fully aware that each format suits a certain environment, and you wouldnt be able to give a definitive answer without knowing more of the details, but i would like to know peoples opinions, and if anyone has done a silimar thing of converting an existing WinForms App into a WPF frontend, and any observations they made in doing so.
Thanks
Underlying the new features in WPF is a powerful new infrastructure based on DirectX, the hardware-accelerated graphics API that’s commonly used in cutting-edge computer games. This means that you can use rich graphical effects without incurring the performance overhead that you’d suffer with Windows Forms. In fact, you even get advanced features such as support for video files and 3-D content. Using these features (and a good design tool), it’s possible to create eye-popping user interfaces and visual effects that would have been all but impossible with Windows Forms.
WPF enhances features that appeal directly to business developers, including a vastly improved data binding model, a new set of classes for printing content and managing print queues, and a document feature for displaying large amounts of formatted text.
But if you’ve done a substantial amount of work creating a Windows Forms application, you don’t need to migrate it wholesale to WPF to get access to new features such as animation. Instead, you can add WPF content to your existing Windows Forms application, or you can create a WPF application that incorporates your legacy Windows Forms content.
Reference: Pro WPF in C# 2008: Windows Presentation Foundation with .NET 3.5, Second Edition

What is the easiest way to upgrade a large C# winforms app to WPF

I work on a large C# application (approximately 450,000 lines of code), we constantly have problems with desktop heap and GDI handle leaks. WPF solves these issues, but I don't know what is the best way to upgrade (I expect this is going to take a long time). The application has only a few forms but these can contain many different sets of user-controls which are determined programatically.
This is an internal company app so our release cycles are very short (typically 3 week release cycle).
Is there some gradual upgrade path or do we have to take the hit in one massive effort?
You can start by creating a WPF host.
Then you can use the <WindowsFormHost/> control to host your current application. Then, I suggest creating a library of your new controls in WPF. One at a time, you can create the controls (I suggest making them custom controls, not usercontrols). Within the style for each control, you can start with using the <ElementHost/> control to include the "old" windows forms control. Then you can take your time to refactor and recreate each control as complete WPF.
I think it will still take an initial effort to create your control wrappers and design a WPF host for the application. I am not sure the size of the application and or the complexity of the user controls, so I'm not sure how much effort that would be for you. Relatively speaking, it is significantly less effort and much faster to get you application up and running in WPF this way.
I wouldn't just do that and forget about it though, as you may run into issues with controls overlaying each other (Windows forms does not play well with WPF, especially with transparencies and other visuals)
Please update us on the status of this project, or provide more technical information if you would like more specific guidance. Thanks :)
Do you use a lot of User controls for the pieces? WPF can host winform controls, so you could piecewise bring in parts into the main form.
WPF allows you to embed windows forms user controls into a WPF application, which may help you make the transition in smaller steps.
Take a look at the WindowsFormsHost class in the WPF documentation.
I assume that you are not just looing for an ElementHost to put your vast Winforms app. That is anyway not a real porting to WPF.
Consider the answers on this Thread What are the bigger hurdles to overcome migrating from Winforms to WPF?, It will be very helpfull.
There is a very interesting white paper on migrating a .NET 2.0 winform application toward WPF, see Evolving toward a .NET 3.5 application
Paper abstract:
In this paper, I’m going to outline some of the thought processes, decisions and issues we had to face when evolving a Microsoft .NET application from 1.x/2.x to 3.x. I’ll look at how we helped our client to adopt the new technology, and yet still maintained a release schedule acceptable to the business.

Categories