Introduction to Xamarin Forms Shell by Chris Miller || Lightup Conference
14K views
Nov 16, 2023
We'll cover a new way of creating Xamarin.Forms apps using Shell, which was introduced in Xamarin.Forms 4.0 . Shell provides an easy way to scaffold your application and provides URI navigation from page to page. Some experience with Xamarin.Forms is recommended. Conference Website: https://www.2020twenty.net/lightup #lightup #2020twenty
View Video Transcript
0:00
Thank you
0:00
All right. Thank you and welcome everyone to introduction to Zamorinformed Shell
0:07
I'm Chris Miller. I'm a software architect for Tyler Technologies. I can be reached on the usual social media things
0:16
Twitter as another lab. I blog at Rajapet.com. I'm on GitHub as another lab
0:22
And a lot of what we're going to be seeing today will be taken from my course that's on plural site using
0:27
Zamaranform Shell. So let's begin. All right, thank you all for donating
0:34
You haven't donated yet. Here is the QR code, just aim your mobile device at it and scan it in
0:42
I'd like to take a chance to thank our sponsors. Without this, we couldn't be putting this on today
0:48
We also have links for speaker and event feedback. Also scan your phone at each one
0:55
And I'm not sure where to get them after we're to the slide, but I'm sure they're going to be on the site somewhere else
1:03
So what is Shell? It's a new way of declaring page navigation within Zamaran forums
1:10
It was introduced in Zammer forms 4.0 last year. It's open source as part of Zamon forums and lives in GitHub
1:17
This is really handy because you can report issues and you can join in on the conversation for new features
1:23
It uses a hierarchical layout to lay out your pages, which makes it really easy to set up your page navigation
1:29
It provides a mechanism for your eye navigation to go from one page to another
1:35
It has a built-in search handler to make searching from lists to detail pages very easy
1:43
So why shell? Well, there was a need to be able to quickly define page hierarchy for an application
1:50
They've always had the master detail template, but was not enough. You can do a lot with it, but it takes a lot to get there
1:58
So Shell was designed around the fast rendering system that's now part of Zaman Forms
2:02
It's designed to go faster and take up less resources. So what makes up Shell
2:08
Well, we start off with a simple container called Shell. And in it, you can have an optional file menu, also known as the hamburger menu
2:16
You can have top tabs. You can have bottom tabs. You have shell content, which are the actual pages that will go into the tabs
2:24
And you can have menu items. Here's a sample flyout menu for a made-up application
2:33
It has three sections. Each section can be customized. We have a header section at the top
2:39
which you can use for branding. The middle section, which is the item pages
2:44
and then the bottom section, which is used for menu items. So the fly-out header is typically used
2:53
for branding and displaying information. You know, what your app is, your version number
2:58
and so forth. And you would use ZAML to define the layout
3:01
and the controls used. Here's a sample flyout header. You can see it's just a grid
3:09
It has, sorry, I went too little too fast. Go back. It has a grid and just a couple of controls on it
3:16
We have an image and then we overlay some text on it. Anything you can do in ZAML
3:20
you can pretty much put in your header. The middle section is your shell item template
3:26
and that controls the display of your pages to be navigated. There are bindable properties for icon and title and this is usually defined in the zamel So what we have here is a sample shell item template
3:42
The first column defines the look of the icon. The second defines the look of the text
3:48
And this is where you can, you have all the room for stylizing anything you want for your
3:52
make your menu look. If you're supporting a right to left language like Arabic or Hebrew
3:59
the layout will not be localized. localized. The menu will come in from the right side of screens to the left side, but the icons will still be on the left of the text. If you need to swap the columns for left to rights support, there's a couple ways of handling this. You can set the column list and the grid column attributes in code, or you could data bind them to a view model that checks the flow direction at runtime
4:23
And here is sample data template to define the pages that are navigated when the user selects an item
4:30
So for each page, you have a title, you have a flight icon, and you have the content template
4:37
Using a data template, the page is not instantiated until the first time it's called
4:41
This allows a shell to start up quickly. Here's a slight variation. We're using fly-out display options set to as multiple items
4:52
And when you do that, your pages are grouped together. So in the hamburger menu, there'll be a top and vertical bar above it and below it
4:58
and then they'll be displayed with bottom tab. to indicate which page
5:02
Here's your first page and your second page. When you play, sorry
5:11
when you place multiple shell content classes inside a tab class, you get top tabs
5:16
where each reach title would be the name of the tab. And when you click on each tab
5:21
will go to each individual page. When you have multiple tabs, you're creating bottom tabs
5:28
and these will appear at the bottom of the page. the bottom of the page. And this is how to do both top and bottom tabs
5:37
The controls defined by the tab class will be the bottom tabs
5:43
The controls defined by the shell content class will be the top tabs that appear inside each of the bottom tabs
5:59
similar to the shell item template. There's just a slight difference for the name of the binding
6:02
properties for both the icon and the text. So here, instead of binding to flight icon
6:10
we bind to icon. And our text is bound to the text property
6:19
With the menu item, you can use an on-click event to define the action for the menu selection
6:25
Or you can use data binding, sorry, and bind a command to the item
6:29
Routes are registered, let's talk a little bit about URI navigation. Routes are registered in the shell template and via the register route method
6:42
You chain the routes to form a navigation hierarchy. The same page can appear in multiple routes, but the URI must be a unique path
6:49
And the URIs usually have three components. You have the route path, the page, and then a query parameter
6:56
So here's an example. Wack, Wack route, Wack page, question mark, some parameter equals some value
7:03
This makes implementing a search feature very easy. So to implement to actually implement the search the UI support is built in We just need the subclass to search handler class And within the search handler class
7:18
we over-guired the on-quiry change and the on-item selected events. So inside the search handle class, in on-quiry change
7:27
it gets past a value that the user typed in. With an in-our-code, we will set item sources
7:34
to an enumerable t-list of the items that match the search. list of the items that match the search criteria
7:39
When you exit on query change, Shell will display a selection list that uses that item source list
7:45
When you click on an item from that selection list, on item selected will be fired
7:49
This allows you to take the selection and call page with that information
7:55
Here's a sample on query changed event from a search handler class
7:59
It gets passed in the value that the user typed in as new value. First thing I like to do is check to make sure
8:05
that they haven't typed in spaces or null characters or anything non-printable
8:09
If it's not null, if it's null or white space, we said item sources to null and then we just return back out
8:14
Otherwise, I usually convert it to lowercase because I usually put do case insensitive searches
8:20
And then I just get a reference to my data store. And then I'm just going to do a search on that on that data store
8:27
And I'm just going to compare house type and address to whatever the user
8:30
passes in. And then we'll come back with an innumerable list they get past item sources
8:36
And we get a selection list. And if the user clicks on anything in selection list
8:44
on item selected gets fired. Excuse me. So the first thing we do is we set item source to null
8:50
because we're done with the list, we don't need it anymore. For iOS and Samar Inform, we have to implement a slight little
9:00
we have to invent a delay as a workaround. There is a bug. It's still in, it's run out version 4.7
9:06
of shell and it's still there. Without this little delay, the item selected event will just fire so quickly
9:15
nothing will happen. And then what we do is we use the URI navigation
9:22
and call go to sync from the shell and go to slash houses
9:26
slash house ID and we're passing in the ID value of the house that we found
9:34
And before we get into how we've process that I'll show you how you add the search handler to the page
9:40
It comes to Shell, and you just add the search handler class and Shell will build in the UI for you
9:45
So right above the content, you would put in shell. That search handler and then namespace your search handler
9:52
And then you can just set placeholder to whatever text you want appear in the search field
9:56
Set show results to true. I've never been able to figure out what that does, but you do need to
10:02
you need that in there. And display member name. That will be the field
10:06
that actually gets displayed in the search list. You can customize it
10:12
So if you add in a item template, you now have a data template
10:17
and then you can define what you want to appear in the search list
10:21
So in this case, for each row, we're going to have a grid. We're going to bind, we're going to have an image bound to it
10:26
and we're going to have the address field. Now I can show you how we handle the query parameter
10:34
We use the query parameter attribute to bind the parameter to the view
10:40
I sorry the query property attribute to bind the parameter to the view And then we add a setter method to the parameter to load up an instance of the view model So attribute we saying ID is going to match ID
10:56
And then what we're going to do is we're going to take that value
11:01
search our data store, and come back with a matching house model
11:05
Once you find that, I have a method in my view model where I can pass in the house
11:11
and they'll come back with the view model, with a view model and we set that to our binding context
11:15
And that's all we need to do. Now we're do a couple quick demos
11:22
And let me bring up my quick version of it. All right, so what we have here is our App Shell
11:34
And we're going to create a series of menu items in the hamburger menu
11:40
Burger menu to show the different ways you can do the layout. We're going to have a couple single pages right here
11:47
We'll do this multiple item. We'll do the top tabs and we'll do the bottom tabs and then finally top and bottom
11:54
So I'm going to bring this up on Android. All right. I did have extra material prepared in case there were no questions
12:06
So let's just keep on going. So one. So when would you use shell
12:11
If your app uses a standard layout where you basically have multiple pages where there's a toolbar at the bomb top flat menu
12:19
Shell has all that for you and makes it really easy. If you're doing a new development like a news name informs app
12:26
using Shell is probably the easiest way to get up quickly. In fact, going forward, that will be the default template
12:33
And more importantly, if you want the app to have the same look and feel on
12:37
Android and iOS. Shell makes that really easy. They handle everything for you for that
12:44
And there is a way to add some style. You're not limited to what comes in basic templates
12:51
You can get your own little styling. We saw that you have customizable templates for the fly out
12:57
header for the shell, the menu, and the search results. You can control the fonts, you can control the colors
13:04
layout, all of that stuff. You can even go. You can even go deeper under the hood and use customer
13:09
renderers. If there was something you wanted to show up in that menu and it's just not supported, you can override it and put it
13:17
put in your own. Hopefully you wouldn't have to do that because it just becomes more work to support. But that's available to you right out
13:24
the box. Also the font icon support that was added in Zamora and
13:29
forms four makes it really easy to add scalable icons. So all the
13:34
images in your fly out menus, you didn't. Instead of having to have different bit maps for different sizes
13:40
you can just use the icon fonts and you just have one. There are multiple sources for icon fonts
13:46
I like the ones from material design icons. Font awesome is also well is also really good
13:52
If you have a lot of your own vector images and you have a design
13:57
department, you can actually create your own font and put everything in there and you have one simple resource
14:02
Now way they scale and you can color them and it just makes things much easier
14:05
And there's also a new get package, Zamonforms visual material that will make the iOS
14:12
renderers look a little more like Android's material design. Now we're back to questions