Chapter 7 Notes
Title: XAML vs. Code
Summary
- XAML is a better way to write up the "view" part of MVVM.
- XAML is used to write the tree-structured visual user interface
Concepts Not in programs
Converter Classes
Page 134
Below the Color class has a TypeConverter attribute added. Attributes in this context means that the defined attributed class is added to the decorated class. That means that you may have additional properties because of the decoration. You might say that Color now has a TypeConverter object that is instantiated with a Type - in this case the: typeof(ColorTypeConverter)
This, of course, means that Color has additional functionality! We'll learn more about this later!
Property-[Element|Attribute] Syntax:
Page 136
This section spells out how to translate some of the code we are used to writing into XAML. Specifically it identifies how XML elements and attributes map to objects and contents.
<Frame ...> ... </Frame> is a object element
<Frame.Content> ... </Frame.Content> is a property element
In <Frame VerticalOptions="Center" ...> ... </Frame>, VerticalOptions is a property attribute.
XAML Compiler
Page 145
You can force your output to compile the XAML instead of using it directly. This saves time and space, but is new and therefore suspect. Add the following line to your Assembly.cs file in the properties folder.
1 [assembly: XamlCompilation(XamlCompilationOptions.Compile)]
Programs
CodePlusXaml
Page 140
Concepts:
- Shows how to create a XAML page in an existing document
- Shows code behind
- Shows XML namespaces required.
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" NOTE: the x: is by convention, so you should use it that way, but it is not strictly required that the name be x.
- Shows how to define the class that the objects defined in this fill will belong to:
x:Class="CodePlusXaml.CodePlusXamlPage"
- Shows where generated code can be found and how to expose it.
In Visual Studio, in the Solution Explorer, select the CodePlusXaml project, find the icon at the top with the tooltip Show All Files, and toggle that on.
- .xaml.g.cs where the g is for generated.
- See obj/Debug folder for generated files for XAML
- A simple way to identify the Content - its available, just make sure to cast it correctly.
ScaryColorList
Page 146.
Concepts:
Shows how to do OnPlatform in XAML
- Shows how to nest object elements, property attributes and property elements properly.
- Shows code behind concepts
introduces the concept of a default property (p. 151) and the ConntentPrpoertyAttribute.
Classes:
OnPlatform class was used (as a variation of the Device.OnPlatform(...))
- ... otherwise no new classes demonstrated.
TextVariations
Page 152
- Shows various ways of formatting text in XAML.
- if you prefer to use multiple lines, you should left justify the whole paragraph in the XAML file surrounded by quotation marks. (See page 154).