In winforms – there's a message pump waiting for an event to happen – when that happens – the appropriate event handler in C# is called.
In WPF there's also XAML. When is that executed? Does the C# code call it or does it call the C# code? In other words: Does an event trigger C# code to run, or does it trigger XAML to be executed?
It seems (please correct me if I'm wrong) that WPF is not really different in the flow of things from winforms. The message pump will call C# event handlers, and the initialization of the form will be done in an InitializeComponent method.
The difference is just that the InitializeComponent method of a WPF form will include parsing an XAML file, so essentially, the developer is describing the initial appearance of the form using XAML instead of C#.
(Of course "C#" can be interchanged here with "VB".)
Here's some info about the wpf application and it's "lifecycle". http://msdn.microsoft.com/en-us/library/ms743714.aspx
And here's some info on InitializeComponent and the role it plays tying into Xaml parsing. What does InitializeComponent() do, and how does it work in WPF?
I'll see if I can find a more official post about the Xaml parsing.
From http://msdn.microsoft.com/en-us/library/aa970678.aspx
"
The XAML file is parsed by the markup compiler.
A compiled representation is created for that XAML and copied to the obj\Release folder.
A CodeDOM representation of a new partial class is created and copied to the obj\Release folder.
In addition, a language-specific code file is generated for every XAML file. For example, for a Page1.xaml page in a Visual Basic project, a Page1.g.vb is generated; for a Page1.xaml page in a C# project, a Page1.g.cs is generated. The ".g" in the file name indicates the file is generated code that has a partial class declaration for the top-level element of the markup file (such as Page or Window). The class is declared with the partial modifier in C# (Extends in Visual Basic) to indicate there is another declaration for the class elsewhere, usually in the code-behind file Page1.xaml.cs.
"
A XAML-Parser parses it and creates the respective CLR objects from it, that is about it.
Related
I'm a WPF developer and have to perform some task in WinForms (I'm a total newbie of WinForms).
Unlike in WPF, I've noticed that the InitializeComponent method in WinForms is generated by the Windows Form Designer, and obviously I can't modify the code written in this method manually as it might just be overridden upon code regeneration.
Let's say I wish to change the following line in the InitializeComponent of WinForms
From:
this.myBtn.ImageAlign = System.Drawing.ContentAlignment.BottomCenter;
To:
this.myBtn.ImageAlign = System.Drawing.ContentAlignment.MiddleCenter;
What is the correct way of doing so? I haven't found any custom code in the codebase that enforces this BottomCenter value? Where is this value coming from? Is it a default value in WinForms?
I've searched the web and encountered similar questions, such as:
https://social.msdn.microsoft.com/Forums/en-US/92ab4ea6-c7c8-44cb-91e7-e91c4f77accd/how-to-modify-windows-form-designer-generated-code-in-initializecomponent-method-programmitically?forum=winforms
in which it is suggested to implement a custom CodeDomSerializer on the control. Is it the right approach for doing such a simple thing? I just want to change a simple ImageAlign property.
I'm maintaining a large c# XAML-based UI project which uses several embedded common or at least reused XAML controls. As in, like,
<shared:DirectionsView DataContext="{Binding DirectionsViewModel}"/>
The problem comes when I try to define hotkeys for new buttons in one UI that happen to already be in use in some included control. I can see the embedded control directly in the XAML editor, but if I want to add ALT+V as a hotkey somewhere, searching the XAML I'm editing for "_v" isn't going to find something in the DirectionsView that's hotkeyed that way.
Is there some file, maybe generated during the build process, that is the "final XAML" or otherwise represents the form with all inclusions in it? That way I'd at least have something to probe.
Is there some file, maybe generated during the build process, that is the "final XAML" or otherwise represents the form with all inclusions in it?
No, there isn't. The visual tree is composed at runtime when the XAML processor parses the BAML (the compiled XAML) and actually creates instances of the runtime classes such as your DirectionsView class.
So you will have to search through the different source files and compose your own "final" element tree "manually" I am afraid.
I am trying to develop multi-document interface for C# application (that is suprisingly hard, in Delphi it was and still is the simple task for more than two decades!!!) and it seems to me that I should use AvalonDoc framework for this https://avalondock.codeplex.com/
The essence is - all the dynamic documents will be the parts (fragments) of one (main) C# Window and there will be one XAML file - that is require by Avalon.
My intention is to create separate XAML and code-behind file for each document, is it possible to create fragments in Visual Studio 2015? E.g. such code pieces that does not inherit from Window and that can be dynamically inserted into window (e.g. as AvalonDock LayoutDocument's).
I know that VS has notion of controls and components but I am not sure - is it right practice to create entire documents (e.g. invoices, stores) as single components/controls?
Yes you can create UserControls -
They belong to the list of standard template files in Visual Studio.
A UserControl is :
1 A Xaml file for describing content.
You can design it graphically or with code editing.
2 C# or Vb.net file for codebehind = event handlers, data members, extra methods , ...
Both files make a single class during compilation thanks to the partial keyword.
Once the Usercontrol is compiled you can drag and drop it onto the surface of a Window like a standard control (e.g. button).
Usercontrols can also be instanciated through C#/Vb.Net code.
Let me know if I answer correctly - if I am complettely wrong - I Delete, or I complete if needed
I know I can build a WPF application with FSharp.ViewModule or FSharp.Desktop.UI. In this case I'm trying to build the GUI part in C# and access it from F#. I can start the C# application from F#, and it shows the MainWindow however I cannot access any control on it from code. How would I refer to the Button (Name:button) on this form? MainWindow or App doesn't show it in my initial setup.
The C# View is just a MainWindow with a Button on it.
The F# code is this:
open System
open WpfView
[<STAThread>]
do
let app = App()
let win = MainWindow()
app.Run(win) |> ignore
The controls on your WPF are 'internal'. That is they are only visible to code in the same assembly.
see: Internal access modifier
Your F# and C# code are in separate assemblies so your F# code cannot access the internal members of classes in your C# code.
The controls are automatically generated by the xaml compiler so you can't change them directly however a simple fix would be to create public access methods and/or properties in your C# class that access the internal properties.
Additionally, while you may have reasons to structure your code the way you have, it might be better to have the main entry point to the application in the C# assembly along with the WPF code and have that reference your logic in the F# assembly. That way you can use data binding to bind to models you have written in F#.
By default, the controls in a WPF form are declared as internal and are not visible outside the assembly of the form. You can change that in the XAML of the WPF form by specifying the x:FieldModifier attribute like this:
<Button x:Name="button" Content="Button" x:FieldModifier="public" />
However, it would be a better design to give the form your own properties to access the data in the form and not directly deal with the controls from outside.
I've been trying to find how does the lifecycle of an application with a GUI written in XAML looks like. This blog post really confused me. The quote:
To fully understand the areas of opportunity for improving startup time, it's important to understand the workflow of a launching application.
1. The App constructor is called in App.xaml.cs.
2. XAML in App.xaml is parsed.
3. Application_Launching is called in App.xaml.cs.
4. The Page constructor of your MainPage is called.
5. XAML in your MainPage is parsed.
6. OnNavigatedTo is called in your MainPage.
I though that XAML is compiled to BAML and embedded to dll. Runtime just translates BAML to .Net objects.
Is it different for WP7 Silverlight? Does Runtime parse it like a browser parses HTML?
What is the point of C# files generated from XAML? (e.g. Main.xaml.g.cs) Is it different for C++ and XAML apps?
.g.cs files are auto-generated and contain information related to the general layout of a XAML page. Here is a pretty good description.
The runtime indeed parses the XAML that is embedded in the assembly - unlike WPF, it is not embedded as BAML.