7

Learning .NET MAUI – Part 6

 1 year ago
source link: https://jesseliberty.com/2022/06/29/learning-net-maui-part-6/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Learning .NET MAUI – Part 6

I’m going to start off where we were at the end of Part 5, but this time instead of creating two labels (for State and Zip) I’m going to create one label with MultiBinding:

  <Label VerticalOptions="Center">
     <Label.Text>
         <MultiBinding StringFormat="{}{0} | {1}">
             <Binding Path="state" />
             <Binding Path="zip" />
         </MultiBinding>
     </Label.Text>
 </Label>

This is more efficient and also allows me to put VerticalOptions=”Center” into the label tag and have it affect all the contained labels. This is a nice alternative to having to use the HorizontalStackLayout

NB: You can continue to use the Xamarin.Forms <StackLayout> element, but HorizontalStackLayout and VerticalStackLayout have been optimized and thus are preferred.

Note also that ScrollView is built into CollectionView and so there is no need to wrap this in a scroll view. To see this at work, copy the last entry (CT) a few times so that we have a list, and then shrink the CollectionView by setting its HeightRequest to, for example, 50. You’ll see your states in a small, but scrollable window.

Run On Multiple Platforms

Until now we’ve been running in Android, but all I have to do is go to the drop down next to the “Pixel 5” at the top of Visual Studio, drop it down and choose Windows to run it natively on the Windows computer

ChangeSimulator.jpg?resize=480%2C62&ssl=1

Live Preview

One terrifically useful feature is Live Preview. You open this screen by clicking Debug=>Windows=>Xamarin Live Preview. When you do a window opens up and displays your page as it will be output. More important, however, is that you can add, for example, ruler lines (vertical and/or horizontal) and set their color and toggle them on and off. This is great for making pixel-perfect alignment.

Note that Live Preview also supports hot reload. There is also a button “Show element during selection” that lights up the Xaml element when you select it on the Live Preview Window. That is super-useful.

LivePreview-769x800.jpg?resize=546%2C568&ssl=1

Live Visual Tree

Closely related to Live Preview is the Live Visual Tree. This provides an interactive outline of every element in your project. You can collapse and open sections and you can light up the XAML element directly from the tree.

LiveVisualTree-588x800.jpg?resize=588%2C800&ssl=1

Notice the numbers to the right of each element; that tells you how many sub-elements that element has.

More soon…

The code for this blog post is available here

Related

Learning .NET MAUI – Part 4June 23, 2022In "Essentials"

Learning .NET MAUI – Part 3June 21, 2022In ".NET MAUI"

Learning .NET Maui Part 1June 10, 2022In "Essentials"

This entry was posted in Essentials and tagged .NET MAUI, Learning Maui, MAUI. Bookmark the permalink.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK