I'm new to Xamarin and I don't understand too much things.
I've created an android app with the preinstalled left menu (hamburger menu) including navigation view. My content_page.xml is linked to MainActivity.cs and I run my code there. Now I want to create another page. So I add a layout element (like content_page) called second_content_page.xml and I link it to a new activity called SecondActivity.cs. Code behind works and I got no problems as far.
The problem is the second content page which doesn't show the menu items (there are lots of layouts about it). So I've tried to copy the code from MainAcitivity's OnCreate method:
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_main);
//ToolBar
AndroidX.AppCompat.Widget.Toolbar toolbar = FindViewById<AndroidX.AppCompat.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close);
drawer.AddDrawerListener(toggle);
toggle.SyncState();
NavigationView navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
navigationView.SetNavigationItemSelectedListener(this);
But I run into many errors related to the toolbar that (as VS says) is loaded two times, so I thought this way to load the same menu was incorrect.
My question is: how to switch pages in the correct way using xml pages and not xaml?
It seems that you can use FlyoutPage, it contains flyout page and detail page and the menu will always display if you what.
Here is a guide about it:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/flyoutpage
And here is an sample link:
https://github.com/xamarin/xamarin-forms-samples/tree/main/Navigation/FlyoutPage
Related
I have a Xamarin Forms application, which has 2 buttons on the toolbar. Currently I'm using FreshMVVM (and it's navigation). I want to load pages without putting them into the navigation. I do not want to have the 'back' button on the toolbar, also I do not want the user return the last page.
Here's how I push a page currently:
CoreMethods.PushPageModel<FirstChoicePageModel>();
I tried to push as a modal, but that way the toolbar buttons does not work until I press back. Should I make new navigation containers and switch to them if I push the buttons?
It's been a little while since I've used FreshMVVM, but if I recall correctly, you have 2 options:
If you want to completly reset the nav stack, you can do CoreMethods.PushPageModelWithNewNavigation<FirstChoicePageModel>();
If you want to keep the nav stack, you can set NavigationPage.SetHasBackButton(this, false); in the code behind of the view.
For both of these options, it may necessary to intercept the back button on Android. In the code behind there is an overrideable method:
protected override bool OnBackButtonPressed()
{
return true; // prevent Xamarin.Forms from processing back button
}
If you want to show a page, but you doesn't want to navigate (change the current page), you can always use Popups.
See this link https://github.com/rotorgames/Rg.Plugins.Popup
I'm trying to disable my bottom navigation view in my activity but it didn't work. But when i try to hide it and show it, its worked fine. But to disable and enable it, it doesn't work. Btw I used android.support.design.widget.BottomNavigationView
This is the code i tried
BottomNavigationView bottomNavView = FindViewById<BottomNavigationView>(Resource.Id.bottom_navigation);
bottomNavView.Enable = true;
its not working and i don't know why :/
I am using NavigationView but I saw the same methods on BottomNavigationView (I declared it on my code). To disable the item you can do this:
navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);//My navigationview
IMenuItem asd = (IMenuItem)navigationView.Menu.GetItem(2);
asd.SetEnabled(false);
If you want to remove items you can do this:
navigationView.Menu.RemoveItem(Resource.Id.nav_Cambiar_TPK);
navigationView.Menu.RemoveItem(Resource.Id.nav_nuevo);
I know you want to see code, but the easiest way to show this problem is to create a new project after installing the new Windows Template Studio Extension. Note: if you create a project with a Navigation menu another way, the problem still exists.
I created a new Windows Template Studio Project named "Test".
For "Project Type": choose Navigation Pane (default).
For "Framework": choose Code Behind (default).
After clicking "Next"
For "Pages (9) - Add Multiple" : choose Tabbed, keep default name "Tabbed".
Click the "Create" button.
Run the project and navigate to the "Tabbed" page.
Use the Right and Left Arrow keys: nothing happens (Item 1 doesn't change to Item 2).
If you click on a PivotItem Header, or on the blank page first the arrow keys will then work.
Note if you use the "Tab" key it will select and underline the first PivotItem Header and the arrow keys will also work.
The Microsoft News app is an example of a Pivot page and Navigation menu working correctly.
This is most likely because when you first navigate to the Tabbed page, the Pivot control is not on focus. So you can try setting it whenever it's first loaded.
public MainPage()
{
InitializeComponent();
MyPivot.Loaded += (s, e) => MyPivot.Focus(FocusState.Programmatic);
}
I am working on Xamarin forms shared project with a master detail page. When I run the app the master detail page is the first item that loads yet it shows the back button which once clicked opens the navigation (master) drawer. This makes no navigation sense whatsoever! I have tried to hide the back button but have not been able to do this. Has anyone come across this and succeeded in doing this? I would prefer a programmatical solution rather than a xaml one.
I have tried adding the below code:
NavigationPage.SetHasBackButton(this, false);
But this doesn't work. I want to keep the toolbar as I will be adding my own menu items here but want the back button gone.
Try this:
NavigationPage.SetBackButtonTitle(this, string.Empty);
NavigationPage.SetHasBackButton(this, false);
I have developed an application using TileControl
and on Clicking the Tiles, it is navigating to forms.
The forms contains GridControl and when I am performing double click event on gridview,it is navigating to another form and displaying a result.
Now the Problem is, when i am clicking the back button of Tile Menu,it is directly showing the main menu,instead of form having GridControl.
I want to show the GridControl first and then the Main Menu.
Please help me with a solution.
I far as I can see you are using the DocumentManager and it's WindowsUIView.
To make it possible to navigate back from the current screen(with item detail) to upper level(with grid) you should make the current content container aware to it's parent container via ContentConteiner.Parent property.
Thus your containers hierarchy should looks like this:
// mainTileContainer(MainMenu)
// -> gridItemsPage(GridControl)
// -> itemDetailPage(DetailForm)
//...
mainTileContainer.ActivationTarget = gridItemsPage;
gridItemsPage.Parent = mainTileContainer;
itemDetailPage.Parent = gridItemsPage;
Related links:
Content Containers
Hierarchy and Screens
How To: Create Content Containers Hierarchy