Why XAML doesn't need to be compiled? - c#

If I create a button in XAML I can directly view it in internet explorer, however if I create the same button in C# I need to compile the code first. Why does it work that way? How does the browser know what to display, shouldn't it treat the XAML as pure XML.
For example, if I have a pure XAML file, that has no reference to code behind, I can open it in internet explorer and it displays it, I am wondering why it works this way, without any compilation?

This is because very early versions of Silverlight supported XAML files in the raw. It's not considered best practice to deploy applications in this way, but it's probably fine if you just have an animation or vector image you want to render. You can read more in this old article here:
http://www.windows-now.com/blogs/rrelyea/archive/2004/1.aspx

Related

How to load an icon / png from Resources into a WPF control using properties window in VS15

I'm migrating from using Windows Forms to using WPF and I am struggling to load icons for controls such as menu items and buttons. A lot of the solutions I see online are typing xaml code directly, however I was wondering if anyone could show me how it is intended to be done through the designer using the properties window?
I have loaded the image (i have both .ico and .png and can use either if this matters) into Resources. When I navigate to the control properties I can see an icon field. However I can't type anything directly into the text box and the further options only seem to be Custom Expression, System Resource and Create Data Binding. I have explored each option and it's not obvious how I would do this. I would expect to be able to use Local Resource but it's grayed out.
I have set the icon Build Action in Resources to 'Resource'.
I much prefer using the designer property window to typing the xaml directly, especially as I am just starting out. If someone could show me a way this can be done I'd be very appreciative as I've spent far too long just trying to get a simple image to be displayed beside a menuitem.
Thanks.

Difference between using XML for static controls and making controls programmatically?

I have started using WPF and started with some games and visual apps.
In my first App I started a blank project then made a class that inherited from a window which had a canvas that I added and removed images from Dynamically, like follows:
class MainWindow : Window
{
public Canvas canvas=new Canvas();
public MainWindow()
{
this.addChild(canvas);
}
//add an image every second move it and remove it
}
This would be impossible with static XML but someone told me it is a bad idea to do controls dynamically, is it true?
Is it a performance loss?
And is there a simple and efficient method to draw, lets say, 100 images at a 30 fps without lag?
There is no performance loss / the performance loss is negligible compared to the HUGE productivity / code cleanliness gained by doing things the RIGHT way.
And XAML is not static.
There is DataBinding, and if you need to add / remove items dynamically there is the ItemsControl.
There is also the concept of DataTemplates that dynamically render specific UI depending on what Model / ViewModel objects are passed to it.
Do not manipulate nor create UI elements in procedural code. WPF is not winforms.
None of the controls are reallystatic. There is no any runtime difference.
Most likely, by using XML you meant XAML. XAML merely serves as a data source use to actually generate C# code (or the code in VB.NET or other .NET language) to compile is and to use exactly in the same was as you would do it programmatically.
You can use this fact in your work. If you know how to do certain things with XAML but don't know how to do similar thing in C# code, do the following: develop XAML-based project and build it. Then perform the search for *.cs files under the directory where your project file is located. You will find some *.cs files which was not in your source code. Those files were auto-generated with the use of XAML. Look at them.
You will be able to learn how it works behind the hood.
Good luck,

Preview of code-only WPF controls in VS2010 - how?

I hope I am able to illustrate the problem using a lot of images. First of all, I was no real fan of XAML (Silverlight issues, crashes in Preview, and so on...)
Now, with VS2010 the situation has become better. There are still a lot of things I like better in code, but I also want a preview in my VS.
So, take a look at the following control: It is really simple, a todo details list. The first screenshot shows the code of the control, pretty straighforward:
CodebasedControl http://img28.imageshack.us/img28/2263/invoicea49.png
There is no XAML, so obviously no preview. Of course, I could encapsulate it in another control, like shown in the next screenshot:
CodebasedControl http://img11.imageshack.us/img11/9515/invoicea48.png
But, in that case I have an additional file I do not want or need.
So I had the idea to move the init stuff inside the contructor of a XAML control. For simplicity, I used simple elements. But they do not show up in the preview...
CodebasedControl http://img99.imageshack.us/img99/5547/invoicea47.png
CodebasedControl http://img512.imageshack.us/img512/9625/invoicea46.png
Finally, I know I could use the controls in other parts of my app when creating UIs. But I am using layout manager, PRISM and a lot of other stuff, so I just want an easy preview of some specific control I created (without having to have a XAML wrapper file for each control)
Thanks for help, and sorry for the post structure, but I though with images it is better to understand...
Chris
Ok,
I found a way. Basically I am tricking VS by changing the XAML, but keeping the code-behind linked to the file. It the same like the wrapper solution, but without having a dedicated extra class or file. I am using the "xaml-infront" file for preview.
This solution only works with pure code controls, I have to do more research for mixed controls (at least I think so.. but it is enough for me for now).
Please be aware, the code behind is NOT partial anymore. It could be placed anywhere else, what I am doing here is basically only related to file-names and visual studio "readability"..
See screenshots for explanation:
alt text http://img15.imageshack.us/img15/5456/invoicea50.png
Some space for easier reading
alt text http://img186.imageshack.us/img186/1545/invoicea51.png

Folder Explorer in C#

I'm trying to create an easy money folder explorer in c# and, because I'm a noob, I don't have many ideas except using webbrowser, which works (if anyone has a better idea please let me know it).
THE QUESTION: how do I make it start in "view - medium icons" instead of "view - details" ?
Thanks a lot,
Ionut
You might be taking the wrong approach here - have you considered using an ordinary ListView control? You'll have much more control over how your icons, columns, etc., are shown.
MSDN even has a tutorial showing how to use a ListView to mimic Explorer functionality.
you have to use P/Invoke to achieve this. In summary, WebBrowser uses a windows control SysListView to display the files. This control can be accessed and customized via P/Invoke.
For an example of how to do this, go here --> WebBrowser to view local files.
The WebBrowser is very inflexible and slow for browsing files and folders. Consider using controls such as FileView developed spcifically for file and folder browsing.

Is XAML in WPF equivalent of .Designer.cs in Windows Forms apps?

Is XAML in WPF equivalent of .Designer.cs in Windows Forms apps?
Does it just provide compile-time state for the UI?
I am not sure but it looks like you can do things programmatically with XAML at run-time.
If I have a basic UI state where everything is added at run-time, then should I be looking outside the XAML stuff?
It's probably safe to look at XAML that way - although it's not entirely accurate. The XAML is compiled into BAML, and parsed at runtime - where the Windows Forms designer.cs file is just another C# file built by the designer. XAML is never directly translated into C#.
You can do everything done in XAML via code, though. Charles Petzold's WPF book actually takes this approach. It builds entire WPF applications in code before he ever introduces XAML.
In addition to Reed's input, I should mention that:
XAML uses XML syntax to define UI, but Form does not.
In XAML, it is much easier to separate UI from the logic by using code-behind-design. As UI is only XML, and they can be designed totally independent of any code-behind changes.
Minor changes like Button where you can set Content instead of Text, so you can set anything whereas it could be only text.
Data Binding and Dependency properties are way easier and better in WPF comapring to Windows.Form
Very easy and cool way to handle your design, as you can find there are many cool examples on the net.
You can use tools such as Snoop to visualize your 3D design, and can inspect and verify all your controls and events by selecting the UI. You can see what events are handled or not. This is a very cool tool, and you can find similar ones.

Categories

Resources