简体   繁体   中英

How to use C# with WPF

I want to create a UI for my application.

What really confuses me is how to actually do it. I know that Microsoft has introduced WPF.

I have also seen some examples, but what I am not getting is whether or not WPF a seperate language? How can I use C# with WPF?

Lets say I want the user to click on a browse button, select a file and display it's content on the UI. I want to do it using C# while WPF providing the UI, is this possible?

Any good resources for a newbie like me?

EDIT What i didnt understand, will i use WPF for my UI or windows form for my UI while my actual code is in C# ?

I think i am being lost here ? WPF seems something else that supports c#. is that true ? i thought WPF was only for UI while the actual code would be in C# or VB.

I have taken wpf unleashed but it explains wPF and not how to use C# with it. Atleast in the starting chapters ?

I know i am being dumb here but i am really confused

Yes it is definitely possible. Take a look at this article about Mode-View-ViewModel (MVVM) from Microsoft, which is a good introduction to using Xaml (the markup language for WPF) with C#.

Since you are familar with WinForms I will explain it like this.

WinForms provides the GUI and it is similar to what WPF does.

WPF however mainly uses XAML, a markup language based on xml to design the visual elements. It is a presentation foundation on its own that could be used without XAML but it is certainly a big part of it.

When creating an application it will be either WPF or WinForms you can't combine the two (well easily anyway I know there are a few ways to get around it)

You can interact with WPF the same way you interact with WinForms elements.

You can create a Window. On there you can place a grid, where you can then place controls such as a button or textbox. Then in the code behind it is exactly the same as referencing a control in winforms. for example in the page_load function doing

txtInput.Text = "A String";

So what should you choose? (Please note I am about to give you a few things to compare I realize this hardly describes both technologies to its fullest)

WPF

Pros - Great for visually appealing designs - You have XAML based control over your visual elements. Meaning you can change the way your form looks by writing xaml instead of doing all through the visual studio IDE pressing buttons.

Cons - Bit of a performance hog. It has come along way with .net 3.5 sp1 but still chews up quite a few resources - Not as many controls built for it as WinForms, mainly because its a newer technology. - Can become complex as syntax for binding information to controls doesn't include intellisense.

WinForms

Pros - Familiar and well used technology so your development will be faster - Better performance - More controls built for it

Cons - More effort to build a visually appealing design - Its not new and exciting so to speak. I know we all like to learn new things :)

WPF is part of .NET framework, so it's not related with specific programming language. Please read MSDN's "Introduction to WPF" - http://msdn.microsoft.com/en-us/library/aa970268.aspx

It is not a seperate language. WPF employs XAML, a derivative of XML, to define and link various UI elements. As in web development, both layouts and specific themes are well suited to markup, but XAML is not required for either. Indeed, all elements of WPF may be coded in a .NET language (C#, VB.NET). The XAML code can ultimately be compiled into a managed assembly in the same way all .NET languages are.

You can implement your requirement in easily WPF.

To get a hands-on in WPF start with this article, http://10rem.net/blog/2010/02/09/getting-started-with-wpf--hello-world-in-multiple-flavors

Happy coding..

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