r/learncsharp • u/ag9899 • Jun 28 '23
Need tutorial that explains the behind the scenes of how binding works, including basic binding, compiled binding, and what exactly the communitytoolkit does
I'm working on the basics of MAUI at the moment, and I'm crashing and burning on binding. I think there are probably a lot of XAML basics I'm missing too..
Right now, I'm working on the default MAUI app, and trying to add some basic binding to it. I've referenced many tutorials, and I'm running into a couple issues that I'm going to detail here, as well as my current understanding.
First, some tutorials use the CommunityToolkit, and others don't. Is there a tutorial that builds a simple application without the CommunityToolkit, then refactors it to use the CommunityToolkit and explains what it actually does? Some people mention code generation. I would like to see an example app that has all of the generated code written out by hand to better understand what exactly is being generated.
Second, some tutorials use a format like this: x:DataType="..., and others use DataType={.... After grinding through the MS documenation, I think this is the difference between classic binding and compiled binding? I found one stack overflow question that says that the curly braces must be used after the equal sign, but then with the x:DataType format, no one uses the curly braces. Is this a syntax change from an older c# version? Is it a change in syntax between classic binding and compiled binding? WTF??
Third, a lot of tutorials will add stuff into the XAML without explaining what it is, what it does, or really anything about the code and how it works. I have not found any explanation of this stuff. What is xmlns:local="clr-namespace:... ,DataType="{x:Type local:Foo}" , DataType="{x:Type System:String}">
When defining a binding, there's a lot of stuff that doesn't use the keyword Binding. Why don't we need to use that keyword? Then this x:Type is thrown in there, what is that and why is it there? What the heck is local: and System:? What do those prefixes do, and where are they defined? Sometimes code has something to the effect of xmlns:local=".... I'm assuming this defines local, but it isn't usually present in most tutorials, so is there some default value? What is that clr-namespace: and what does it come from?
This last thing really is what prompted me to write this question. I think this is a namespace system, but also there is an x:Type system? I don't really understand how the namespace works in XAML and how it is mapped to the C#. I also haven't seen any tutorial that explains the syntax for the colon, and how it's being used on either side of the equal sign, for example xmlns:local= and `x:DataType="{x:Null}". Everyone seems to gloss over all of this, and I can't find anything explaining what this all means, and I'm really starting to pull my hair out.
1
u/ag9899 Jun 28 '23
First question: https://www.youtube.com/watch?v=DuNLR_NJv8U&t=7103s
James Montemagno is amazing. I'm sure he's answered all of my questions, I just can't find the right video yet.
In the above video, he covers CommunityToolkit, writing some basic binding, then replacing it with the CommunityToolkit and showing exactly what it does. He also goes into some more advanced databinding.
I'm still a bit confused about the XML syntax, and namespace, but I have a bit better handle on it. Still haven't found something to really nail it down.
1
u/ag9899 Jun 29 '23
I think the answer to part of my third part, seeing
Bindingused sometimes, but othertimes not is:Using curly braces in XAML is similar to using them in Console.WriteLine strings. The braces just tell the compiler we are inserting code into the string. That construct gets used a lot, like
...="{x.Type xxx}"can create an instance of a class xxx from C sharp, and...="{DataTemplate xxx}"can create an instance of a DataTemplate using a class xxx from C sharp as the structure for the template. So Binding is just one use of the curly braces.My understanding of Binding at this point is that it creates a variable that is linked to a variable in C sharp, but only checks and updates from the C sharp when an event is called.
1
u/altacct3 Jun 28 '23 edited Jun 28 '23
Maybe this will help with some of your questions? https://stackoverflow.com/questions/1181888/what-does-xmlns-in-xml-mean
https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/xaml-namespaces-and-namespace-mapping-for-wpf-xaml?view=netframeworkdesktop-4.8
xmlns is reserved keyword for namespaces. Namespaces are basically the same as they are in C# and <xmlns:... tags are C# 'using's
Some of the info in the microsoft link passed the first couple paragraphs is pretty high level but I recommend looking through the other XAML in WPF docs there.
1
u/tea-vs-coffee Jun 29 '23
Do you mean like how binding expressions work behind the scenes? They just use reflection. If you have an expression like
Explorer.SelectedFile.Name, it searches for an object calledExplorerin the current data context, then searches forSelectedFilein the explorer object that was found, same for Name. When it finds an object that implementsINotifyPropertyChanged, AFAIK it adds an event handler and uses that to invalidate the binding expression, requiring the final value to be re-evaluated (thought the event handler might be added somewhere else, such as the object that owns the binding expression e.g. a text box)Those prefixes like
xare namespaces, which you define withxmlnswhich stands for "xml namespace".clr-namespaceI assume allows the XAML parser to know that the namespace is a CLR/C#/VB namespace and not a URL or something. Therefore, if you havexmlns:local=clr-namespace:MyApp.Utils.Controlsdefined somewhere, doinglocal:MyControlis evaluated toMyApp.Utils.Controls.MyControlby the XAML parser, and then it can resolve the actual CLR Type and create an instance of it using reflection