Click here to Skip to main content
15,918,889 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a .xaml file and I want to add it to my existing windows forms project. Can I do that? How can I do that?

(When I try to do it, it gave me compile errors)


Kushan Randima
Posted
Comments
Sergey Alexandrovich Kryukov 9-May-12 10:45am    
What was the error? And what were you planning to do with this file? :-)
--SA

XAML format is for use with WPF/Silverlight.
Windows Forms uses only .cs files for the definition of UI elements.

So, the answer is no : you cannot add an XAML file in a WinForms project as is. You have to create a new System.Windows.Forms.Form that will mimic the look of your XAML file.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-May-12 10:54am    
Strictly speaking, you can (but it would be pointless unless one does something special). Please see my answer.

"Cannot... as is" is an important note. The idea explained in last sentence ("will mimic") is absolutely correct. (The only question is: why doing it? In principle, I could imagine some applications, for example, portability with Mono, where WPF is not implemented). Anyway, my 5 for this answer.

--SA
phil.o 9-May-12 11:00am    
Thanks SA for these precisions :)
5ed your answer as it is much more accurate than mine (which would deserve a 4 under comparison).
Sergey Alexandrovich Kryukov 9-May-12 15:53pm    
Thank you very much, Phil.
--SA
silambu varad 6-Feb-17 4:48am    
please reply this question
You can add a .XAML file to your project exactly as any other file, but prepare for the frustration: it won't add a WPF Window to your Forms application. Do you understand that the nature of WPF application is very different; and it has almost nothing to do with Windows API?

Interoperation between Forms and WPF is possible, but quite complex. Please see:
http://msdn.microsoft.com/en-us/library/ms751797.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.elementhost.aspx[^],
http://msdn.microsoft.com/en-us/library/ms742215.aspx[^].

In principle, XAML is not directly related to WPF. This is just an XML schema which could be used for many different purposes. For example, you can use XAML as a vector image format. In this case, you can include it in your Forms (or any other) project as a resource and use for some graphical purposes. Somehow — you did not share with us your purposes. You could create some engine to work with XAML graphics or something.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900