我是一名長期的 C# 程式員,但我對 WPF 和 XAML 完全陌生。我可以找到很多教程說“這是如何實作這個特定的事情”而不是“這就是為什么你這樣做來實作這個特定的事情”。我很難理解 XAML 中各種語法的含義。
在這種情況下,屬性中的花括號實際上是什么意思?它們在代碼方面被翻譯成什么?我如何推理它們?它們是如何被解釋的?為什么似乎有多種語法(Binding="{Binding someProperty}"vs Binding="{Binding path=someProperty}")?
我一定遺漏了一些明顯的東西,但我確實花了幾天時間閱讀教程、觀看教程,甚至在極其枯燥且難以理解的
這是如何作業的
當 XAML 編譯器運行時,<Label ... />將被翻譯成代碼,Label在運行時生成類的實體。我們可以從 XAML 設定該實體的屬性。通常,當您使用
這是我們迄今為止學到的:

如果我們添加花括號,現在 XAML 編譯器會以不同的方式對待它。大括號中第一個空格之前的單詞現在被視為標記擴展的名稱。標記擴展是從類繼承的MarkupExtension類。
在這種情況下,花括號中文本的第一部分是Binding。
所以現在我們知道大括號和大括號內字串的第一部分是什么意思,但其余部分呢。為什么有時有等號有時沒有?
After the text Binding there's a space, and the rest of the string is treated as comma-separated inputs to the Markup Extension. How does that work in practice? Well, if the first inputs have no equals sign they'll be passed into the Markup Extension class' constructor as strings.2
In our example, the Binding class would be created with a parameter value of Text. If you have a look at the 
After the class has been instantiated using whatever constructor parameters you supplied, it then treats the remaining equal-sign-separated inputs as key-value pairs. The key is the name of a public property on the Markup Extension and the value is the what that public property is set to.
If you again look at the 
Summary
The curly braces tell the XAML compiler to treat the contents of those curly braces as the name of a class inheriting from MarkupExtension, followed by a space, followed by the constructor parameters (if any), followed by key-value pairs that represent the names of public properties and their values.
The property's value will then be calculated by calling the ProvideValue method on the class extending MarkupExtension.
Notes
1. The Binding class is an exception to this rule, and is literally just called Binding without the "Extension" part.
2. I'm not sure what happens if there's no matching constructor.
Disclaimer: I have absolutely no clue what I'm talking about. Please correct this if it's wrong!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/357663.html
上一篇:異步函式凍結UI執行緒
