I am wanting to learn C# and it seems everyone is switching from using WinForms to using WPF. WPF applications seems so much more complicated to me because of the use of the .XAML files that are used to building the Forms.
I am just asking before I get really involved, is the XAML files the only way to build WPF applications? Is there an easier method? I know I could just learn to use the WinForms which seems a lot easier since you basically have a Form object that you work with code but like I mentioned I think it would be best to build WPF apps
XAML does make things a lot easier if you know how to use it because it is more readable and declarative, but you can do (pretty much) anything in C# code as well if you like.
e.g.
<Border BorderBrush="Red">
<TextBlock Text="Lorem Ipsum"/>
</Border>
vs.
var border = new Border();
border.BorderBrush = Brushes.Red;
var textBlock = new TextBlock();
textBlock.Text = "Lorem Ipsum";
// The following step is implicit in XAML via the structure
border.Child = textBlock;
Though this can be written more concisely and hierarchically:
new Border
{
BorderBrush = Brushes.Red,
Child = new TextBlock
{
Text = "Lorem Ipsum"
}
};
Generally i would always recommend using XAML, reasons include:
The parser optimizes the tree construction according to WPF's layout system.
Creating DataTemplates in code is not supported. The construction using FrameworkElementFactories has been deprecated in favour of using the XamlParser (and i definitely do not recommend juggling XAML strings in code).
Separation between UI and code.
XAML is certainly not the only way to build WPF applications. After being run through a couple of tools XAML itself is translated into C# / IL which is used to actually build the UI. There is nothing stopping you from writing the exact same code and building a WPF form by hand.
I would caution you though to consider not taking this approach. XAML is certainly the tool of choice for building WPF applications. It will be in the vast majority of web samples. There are far fewer samples of hand coded WPF applications.
Yes, you can code pure WPF but I don't recommend it.
Petzold's book Application = Code + Markup walks you through using pure WPF from code before getting into using XAML. It's readily available second-hand and you can see the sample code there on the website. It's also available online on the Safari ebook site, so you can read for free with a trial account for a couple of weeks. His book is a very serious attempt to teach you WPF without XAML getting in the way as contrasted with the XAML-heavy approach of many other books.
The visual editor in VS2010 was much better than the previous one so you don't necessarily have to understand much XAML to create your interfaces. I also suggest trying the Expression Blend tool to see if you can get use to its "designer approach" to creating the interface.
WPF has a ton of flexibility in where you choose to do things. I've taken a more code-centric approach and use XAML for the layout and styling but do my binding of data and commands in code. That gives a cleaner style of XAML and avoids you having to learn some nuances of how to specify bindings in XAML. For easing the learning curve, I recommend starting with that approach.
I also suggest you start out using the MVVM pattern with a framework such as MVVMLite which will provide a lot of infrastructure for you and help with the separation of GUI and logic.
While you CAN build everything in codebehind, XAML IS the easy way to build WPF apps. Doing things this way also helps with seperating your view from your program logic. Ideally, a WPF development team would consist of at least one programmer/designer who is in charge, so to speak, of the XAML (or visual side) of things. they would be making animations, and other visual elements. and of at least one "normal" developer who is in charge of the programming logic and data model type things. Of course, this is not an ideal world.
No you do not have to but at #H.B. states it makes it easier.
If you use and IDE like Visual Studio, just like WinForms you have a designer plus Expression Blend. If you find XAML overwhelming at first perhaps play with that rather than going straight into XAML.
Related
I am currently learning WPF framework; I have some past (not much though) experience with Winforms. One problem I've had in both is that the menubar does not look native. I've found a workaround in Winforms, but I haven't been able to find anything for WPF. I've not had this problem in other frameworks I've used, particularly Qt.
In many pics I've seen, it looks native enough in Windows 7, but not Windows 10. I included some pics.
How it currently looks:
How it should look:
Thanks in advance!
Edit
While I have not seen the possible duplicate link, I am aware of setting the foreground/background on WPF controls. That link doesn't really answer my question. I don't want to come up with my own style at this point; all I want to do is make controls look native.
If custom styling is the only way, that's fine, but if there is another way, that would be preferable.
Thanks!
I don't think there's a quick fix to get what you want. WPF renders using DirectX, allowing for much more flexibility in styling applications. A WPF app should render exactly the same way on any version of Windows - it will not automatically adopt a native look and feel (that was actually one of the main selling points of the technology in its early days).
While MS made the default styling somewhat close to Windows at the time of release (Vista, I think?), if you want WPF controls to have a particular look you're going to have to style them yourself.
So over the years I have seen quite a few C# and VB applications which have custom UI designs. One of which I can give an example of is the DayZCommander (C#) application which looks like this:
I was wondering this because I wanted to start making login forms which look like this (note this is just a photoshop render):
And just use it for overall form design.
If you can give me some information, or an answer on this topic please do.
Thanks.
First of all you'll have to use WPF to easily achieve these kinda results (It might be possible with WinForms but it will very hard).
You can also use 3rd party controls like MahApps Metro (also available using NuGet). Here's an example of what you can end up with :
You have also tools like Microsoft Expression Blend that will make creating animated UIs/Custom control templates very easy. but sooner or later you'll to dive in XAML to create your own custom control templates but that's beyond the scope of this answer (there are plenty of good tutorials online).
I have seen some applications having such a rich layout that a person starts hating desktop applications like traditional c#.I was wondering how to make applications having GUI like this is it possible to make it in c#?
According to the title, this uses WPF, which can be used with C# and/or XAML. It can actually be used with any .NET language.
The problem is not the programming language. What you need is:
a UI framework that allows rich layouts (in C#, you'd use WPF),
and, even more important, a designer. With "designer", I mean a human, not a tool. If you look at your screenshot, you will notice that the colors of the background image match those of the buttons, that the buttons match the content circle, that the header of the content circle ("Getting Started") matches the header of the window.
So, really, this is not a matter of programming language. What you need is a design. Implementing it is the easy part (at least with technologies such as WPF).
Look at the window title... "Xceed DataGrid for WPF Recource Center". I might be going out on a limb here, but I'm guessing that it was written using WPF and C#.
Only the developers know for sure! Okay, the window title gives it away
Quick guess, they're using Windows Presentation Foundation, which isn't a language, but part of the .NET Framework (starting with 3.0).
Adding some resources here . . .
You have to use WPF for that, and probably with some custom controls.
For WPF have a look here:
Wikipedia
MSDN
Expression Studio
For already made controls you can check:
Xceed
Infragistics
Devexpress
There are a lot more out there even a lot of free ones. All the above are commercial but i think they have a few free samples.
I want to create buttons and displays in WPF that look something like the Visio Flow Chart displays. Are there classes for that?
Here's a series of articles which show you how to create a drag'n'drop flow chart designer in WPF:
http://www.codeproject.com/KB/WPF/WPFDiagramDesigner_Part4.aspx
The problem with that particular implementation (WPF Diagram Designer on Code Project) is that it is not MVVM based and generally doesn't use an architecture that is flexible or extensible to further development (e.g. just wanting to add Undo/Redo support would argue for a rewrite). It does, however, include a path finding algorithm (though in production use I fear it may be a little buggy).
I would recommend looking at the WpfDesigner.* assemblies in the SharpDevelop project (it's open source) and the WPF DrawTools on Code Project (http://www.codeproject.com/Articles/22776/WPF-DrawTools). The architectures of both of these are at least somewhat amenable to further development. Just ensure the licensing is compatible with your use, particularly for SharpDevelop.
I found http://www.codeproject.com/Articles/182683/NetworkView-A-WPF-custom-control-for-visualizing-a quite good. You would need to extend it with some UI templates to get the shapes you want and perhaps change the connector attachment scheme to fixed locations.
There are some projects that could use WPF at work, for the sake of adopting new technologies.
But the problem is, I can't jump start WPF easily. I recognize the learning curve is steeper than Winforms. Though even with Winforms one can start doing UIs and programmatically customize them in a day.
So the problem is:
I don't have any resource to quickly learn the fundamentals of WPF at work. I can't read a book from cover to cover before I do the simplest of things.
Most tutorials on the net, just does things that excludes the fundamentals. i.e. they post partial xaml code I can't even put into my xaml code correctly (VS highlights them in red).
I can't justify using WPF over Winforms where there is no significant gain for prety much all projects I can think of. And the learning curve just makes things worse.
So I can't suggest my manager to use this tech over the traditional Winforms, but I want to.
Is there a guide or a tutorial on the net, or a video that explains the fundamentals of WPF so I can explore everything else on my own, except the more complicated tasks?
Fundamental of WPF video from Mix07.
Windows Client .NET get started section has lots of WPF Videos.
Channel 9 WPF content.
Windows Presentation Unleashed is a very good book.
MSDN content for WPF.
MSDN magazine articles on WPF.
Well, the basics are there. You can drag and drop controls onto a XAML surface in VS.NET 2008 just about as easily as you can with Windows Forms. Sure, there's the binding syntax stuff, but getting started isn't too bad.
But you DEFINITELY need to buy a good book on it. You don't need to have read it from cover to cover in order to get started. A decent book will have you up and running in a chapter or two, and you can take it from there. Just make sure that whatever book you buy, it was published after .NET 3.5 (and SP1 preferably) came out.
Also, understand WHY you want to use WPF. Is it just because it's new? Then you're right, that's not a good enough answer. WPF does have some big benefits though:
A powerful binding expression syntax that actually works
No need to repaint your own windows in response to WM_PAINT messages
_ (corrollary to the previous point) It's much easier to owner-draw and make your own controls
MS is spending a LOT of effort to actually develop it
MS is spending minimal effort on developing WinForms
If you need 2d/3d data visualisation WPF will pay by itself quickly and it's very evolving...
Personally I've got in that boat because all 3d hardware acceleration (opengl) wrapper in c# are dying so the solution for a maintened 3d looks like wpf or xna...
I'd suggest you get either expression blend or vs2010 beta ... vs2008 just don't speak wpf enough to help someone who doesn't know exactly what he's trying to do.