Hack Forums
[C#] Windows Presentation Foundation - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Windows Presentation Foundation (/showthread.php?tid=5085065)



Windows Presentation Foundation - Sorzus - Dec 7, 2015

Hey,
I've noticed that some of you are interested in WPF, so I want to tell you a little bit about this awesome technology. I'm creating applications using the WPF for 2 years and I earned lots of experience.


What is WPF?
WPF (Windows Presentation Foundation) is a technology from Microsoft to create a User Interface. WinForms and Console are the other ones provided by Microsoft. It's important to note that WPF doesn't replace WinForms and WinForms doesn't replace Console. All of them have something they are the best at.
With WPF, you don't drag and drop controls from the toolbox - you could do that, but it's cruel and destroys the awesomeness of WPF: Please don't do that. In WPF, you create the view using a special language: XAML (Extensible Application Markup Language)!
You can use WPF with C# or VB.Net - it doesn't depend on a programming language.


XAML
As some of you already now, XAML is the language to create something you can see in WPF. XAML is a markup language, which means that it looks similar to HTML (Hypertext Markup Language) and XML (Extensible Markup Language). XAML is pretty easy to understand and if you know a little bit about HTML, you don't need much time.

Here is a small sample. It will create a black banner which is 30 units high and contains a white text "Hello World" (centered). Below, there is a centered button with the text "Just a random button".
Code
<Window  [...]>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid Background="Black">
            <TextBlock HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       Foreground="White"
                       Text="Hello World" />
        </Grid>
        <Button Grid.Row="1"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Content="Just a random button" />
    </Grid>
</Window>

Layout
In WPF, you don't say this button is 25 pixels high. You say that it's 25 units high. This is relative to the screen resolution. With WinForms, a button with 25 pixels will be very, very small on a 4k monitor. In WPF, it will be the same height as on a hd screen.
Also, you don't set an absolute location for the controls. You don't say the button is on (X=100,Y=100) - the button doesn't even have the properties. The layouts are dynamic and you can scale them as you want - there won't be any whitespace if done right.


MVVM
I think that it's almost impossible to be a WPF programmer and never hear about something called MVVM (Model View ViewModel), but what is that? In WinForms, you drop a button on the form and make a double click on it to subscribe to the click-event. In MVVM, you don't use eventhandlers to react to an event. You bind properties to a class called ViewModel. Okay, you may understand nothing, if you want to learn that, just google it. MVVM is pretty hard to understand, but if you understood the concept, it's awesome^2. It is clean, allows you to create laaaarge windows and makes unit testing really easy. Really, you won't want to code using events ever again. It is worth every second you invest in learning that, trust me ;)


Styles
In WPF, a control doesn't have a look. A control is just a behavior. The look comes with the style you set on it. If you change the default styles in the file App.xaml, all controls in your whole application will look different. That's just awesome for styling your application, unlike WinForms you don't have to change every control.


Animations/Storyboards
WPF supports animations. It is pretty easy to animate almost every property of a control like width, height, opacity, RenderTransform (rotation), ... You can set the duration and the start time for the animation. Also, you can animate colors which is just awesome!
WPF renders with Direct X, it has a great performance.



Okay, if you want to learn WPF now, just google a little bit, you'll find lots of posts which recommend resources for learning this awesome technology (look for StackOverflow posts). If you are new to programming, I wouldn't recommend to start with WPF because it is a little bit more complicated than WinForms or Console.


If you have any questions about WPF, feel free to ask me :)


* - Motley - Dec 7, 2015

I don't have time to read this at the moment but it's looks very good and high quality. Thanks for the contribution!


* - Zilog80 - Dec 7, 2015

(Dec 7, 2015 07:46 AM)Alkalinee Wrote:  With WPF, you don't drag and drop controls from the toolbox - you could do that, but it's cruel and destroys the awesomeness of WPF: Please don't do that. In WPF, you create the view using a special language: XAML (Extensible Application Markup Language)!

> 2015 (almost 2016) writting the GUI interface instead make it WYSIWYG. Someone said RAD, A.K.A RAPID application development?

Well, you have Blend for Visual Studio for making the XAML, but you need to spend a LOT of time for learning Blend

MVVM is a software archirectural pattern (there are a LOT of patterns, PRISM, MVC,... not only MVVM) and you can use MVVM in Windows Forms too. (Do you know that there are bindings in Win Forms?)

Do you tried make unit testing or use TDD techniques using MVVM? Do you use mocking using MVVM? Good luck. For testing a f**ing ShowDialog() or a simple MessageBox you need to re-invent the wheel (again someone said RAD?)

I worked with WPF on the past (Visual Studio 2010) and intellisense on XAML was a shit. Maybe new VS versions improve this, I don't know.

WPF = Silverlight = dead

Microsoft Build 2014: “… not planning on any investing in major changes to WPF"

GL


* - Sorzus - Dec 7, 2015

@Zilog80
Thank you for your reply.

Quote:2015 (almost 2016) writting the GUI interface instead make it WYSIWYG

I don't know what you want to say. WPF is really cool for the dynamic layouts. If you take a look at the XAML code I've posted, you will see that you can resize the window as you want: it will always look good. If you drag & drop the controls on a WPF window, you destroy that because it will generate
Code
VerticalAlignment="Top" HorizontalAlignment="Left"
which is an absolute position. If you want to create a drag and drop interface, you can use WinForms.

Quote:MVVM is a software archirectural pattern (there are a LOT of patterns, not only MVVM) and you can use MVVM in Windows Forms too. (Do you know that there are bindings in Win Forms?)

MVVM is the most popular and the most used pattern in WPF. Using MVVM in WinForms is pretty limited (no commands, ListView is almost unusable, ...).

Quote:I worked with WPF on the past (Visual Studio 2010) and intellisense on XAML was a shit.

They did a lot of improvements in VS 2015. Anyway, if you can afford Resharper, you'll get a god-like IntelliSense.


Quote:WPF = Silverlight = dead

I don't know why you say something like that, it's false. WPF and Silverlight are two different things. Silverlight represents a subset of WPF that is delivered through a browser plug-in, much like Flash/Flex. WPF isn't dying at all, lots of new applications are written with WPF.

Quote:In november 2014 the WPF team published an article, The Roadmap for WPF, showing that WPF was still actively developed.
The team is mainly working on important issues like performances, which have been continuously enhanced since the inception of WPF, and tooling fully integrated into Visual Studio.
Maybe the more important enhancement is the full support of touch and high-density display.




* - Armada - Apr 2, 2016

How has this not gotten attention?
- Possibly you dropped the bomb on them and they still trying to figure it out.

This is not only well presented but totally informational.


* - Nicktron - Apr 2, 2016

Really cool reading dude, WPF sounds great indeed. :)


RE: Windows Presentation Foundation - Rrocet - Aug 9, 2018

Hi, Sorzus. Finally I found how to ask you. I really liked the window of licensing in Orcus. I would like to do exactly the same in my project.
I learn WPF for a year now, but I still do not know how to do it!
Search in Google pictures: Visual Studio 2015 Community Installer.
There will be Related pictures with a beautiful installer who has such a dark design. This is very similar to the window licensing of Orcus.
Please, can you help me make the same window? You can reply here or send PM for me.
Maybe this is some kind of additional library? But this is definitely not MahApps Metro....


RE: Windows Presentation Foundation - SaketSaket - Aug 16, 2018

Thanks for the this small tutorial on WPF...


RE: Windows Presentation Foundation - Vyxn - Aug 16, 2018

This was really my plan but I got a job for Java programming so maybe next time? :(

(Aug 16, 2018 12:29 AM)SaketSaket Wrote:  Thanks for the this small tutorial on WPF...

Thanks for the bump as I nearly forget about WPF.