How do I use Xamarin Shell in C# code?
I am having terrible difficulty getting anywhere with the new Shell because I am wanting to do it in C# code if possible and every single example seems to use XAML.
Can anyone point me at an example piece of code that uses the Shell?
- Patrick
Developer technologies .NET Xamarin
-
JarvanZhang • 23,971 Reputation points
2021-03-30T12:41:10.23+00:00 Hi, PatrickRyder. To create a basic shell application in code, first initialize the
FlyoutItem
orTabbar
and add the pages. Then add the flyout items toShell.Items
. Please check the code.public class CustomShell : Shell { public CustomShell() { var flyoutItem1 = new FlyoutItem { Title = "Item_1" }; flyoutItem.Items.Add(new ShellContent { Content = new Page1(), Title = "page1" }); flyoutItem.Items.Add(new ShellContent { Content = new Page2(), Title = "page2" }); var flyoutItem2 = new FlyoutItem { Title = "Item_2" }; ... this.Items.Add(flyoutItem1); this.Items.Add(flyoutItem2); } }
-
EasyGoingPat • 261 Reputation points
2021-03-31T07:16:31.23+00:00 Thank you for the answer.
Unfortunately, the code doesn't compile for me. There is no flyoutItem reference.
I am wanting to use a Tab Bar as my main screen. Could you post code to set-up a basic one of those?
Also, at the moment, I am doing something very similar to your code:
public class AppShell : Shell { public AppShell() { ShellContent content = new ShellContent() { ContentTemplate = new DataTemplate( typeof(HomeView) ) }; Items.Add( content ); } }
When I do this, I get the following warning:
[TableView] Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information... (TRUNCATED)
Do you have any idea what might be causing this?
-
JarvanZhang • 23,971 Reputation points
2021-03-31T09:08:09.267+00:00 What is the 'HomeView'? Is it a contentPage? I tested the code on my side, it works fine.
public class CustomShell : Shell { public CustomShell() { ShellContent content1 = new ShellContent() { ContentTemplate = new DataTemplate(typeof(MainPage)) }; Items.Add(content); } }
-
EasyGoingPat • 261 Reputation points
2021-03-31T10:55:07.907+00:00 My inheritance is HomeView - ViewBase - ContentPage.
Are you saying you run my code and don't see the same warning that I am seeing?
-
JarvanZhang • 23,971 Reputation points
2021-03-31T11:39:41.547+00:00 Yes, it works fine on my side. This may be a illustrates warning, did that effect the deploy? What's the version of the VS and XCode? Try to update both to lastest stable version and test again.
-
EasyGoingPat • 261 Reputation points
2021-04-01T08:37:04.697+00:00 VS is 16.9.3. XCode is 14.4.
It seems this warning has been seen by a number of people: https://stackoverflow.com/questions/57897288/uitableviewalertforlayoutoutsideviewhierarchy-error-warning-once-only-ios-13-g
Some say it is harmless but I'm not comfortable with leaving a warning I don't even understand the cause of. And my Xamarin/iOS knowledge is not up to investigating this more deeply, certainly not in a practical amount of time.
It also worries me that you can run my code and see no problem.
So I guess I am stuck with trying to get the old stack-based navigation to do what I need.
- Patrick
-
JarvanZhang • 23,971 Reputation points
2021-04-01T13:56:50.38+00:00 Hi, PatrickRyder. Did you face the warning in other projects? And will the warning occur if you create a shell template application directly?
-
EasyGoingPat • 261 Reputation points
2021-04-19T07:16:56.91+00:00 I have just created a brand new Xamarin iOS project, using the Shell Navigation Tabbed template and I do see exactly the same warning.
So maybe the people who say this is harmless are correct. I would still like to know why it happens though. And I have the problem that we have a policy of zero warnings in production code.
Kind wishes - Patrick
-
EasyGoingPat • 261 Reputation points
2021-04-20T06:47:50.367+00:00 -
JarvanZhang • 23,971 Reputation points
2021-04-20T14:53:27.33+00:00 Hi, PatrickRyder. There is nothing wrong with the code and the project is deployed well, the illustrates warning is due to Visual Studio or the iOS native.
-
EasyGoingPat • 261 Reputation points
2021-04-21T06:37:15.053+00:00 Hi, @JarvanZhang
That's reassuring to know. If I did want to report this as a bug, though, how would I do this, as the feedback in Visual Studio brings me here?
- Patrick
-
JarvanZhang • 23,971 Reputation points
2021-04-22T08:32:49.823+00:00 Hi @EasyGoingPat , sorry for the miss-leading comment, we can reproduce the warning from Shell Navigation Tabbed template, the warning showed up in the output window when app run, it should be a runtime warning thrown from native iOS. This can be a bug from the template, it's suggested to report this to the Xamarin.Forms github repo and check if there's any workaround to get rid of this.
Sign in to comment