Navigating from TabbedPage to ContentPage - xamarin

I have a TabbedPage with three tabs which is my main page. In one of the tabs I have a ListView and on clicking on the Item in ListView I want to navigate to a new Page.
I can navigate to the new page but the page is displayed within the tab. I do not want the page to be within the tab and should take be opened a new page taking the whole screen.
This is my listView itemSelected method within one of the tabs.
How can I make the DocketDetail page take the entire screen.
I have tried making it the 'MainPage' which
App.Current.MainPage = new NavigationPage(new DocketDetail());
But doing this I could not go back to the TabbedPage.
Please suggest
lvLiveDockets.ItemSelected += (object sender, SelectedItemChangedEventArgs e) =>
{
if (e.SelectedItem == null)
{
DisplayAlert("Item DeSelected", e.SelectedItem.ToString(), "Ok");
return; //ItemSelected is called on deselection, which results in SelectedItem being set to null
}
else
{
DisplayAlert("Item Selected", e.SelectedItem.ToString(), "Ok");
this.Navigation.PushAsync(new DocketDetail());
}
//((ListView)sender).SelectedItem = null; //uncomment line if you want to disable the visual selection state.
};
My App.Xaml.cs
MainPage = new TabbedPage
{
Children =
{
new NavigationPage(new PendingDockets())
{
Title = "Pending Dockets",
Icon = Device.OnPlatform<string>("tab_about.png",null,null)
},
new NavigationPage(new LiveDockets())
{
Title = "Live Dockets",
Icon = Device.OnPlatform<string>("tab_about.png",null,null)
},
new NavigationPage(new ArchiveDockets())
{
Title = "Archive Dockets",
Icon = Device.OnPlatform<string>("tab_about.png",null,null)
},
}
};`

Your App.cs could be changed to this:
MainPage = new NavigationPage(new TabbedPage()
{
Children =
{
new PendingDockets()
{
Title = "Pending Dockets",
Icon = Device.OnPlatform<string>("tab_about.png",null,null)
},
new LiveDockets()
{
Title = "Live Dockets",
Icon = Device.OnPlatform<string>("tab_about.png",null,null)
},
new ArchiveDockets()
{
Title = "Archive Dockets",
Icon = Device.OnPlatform<string>("tab_about.png",null,null)
},
}
});

You have to put the TabbedPage inside a Navigation Stack (I think as Root page)
Application.Current.MainPage = new NavigationPage(new Tabpage());
when you select yout item in listview, you should push a new page
Navigation.PushAsync(new DocketDetail());
I hope to have understand your problem
lvLiveDockets.ItemSelected += (object sender, SelectedItemChangedEventArgs e) =>
{
if (e.SelectedItem == null)
{
DisplayAlert("Item DeSelected", e.SelectedItem.ToString(), "Ok");
return; //ItemSelected is called on deselection, which results in SelectedItem being set to null
}
else
{
DisplayAlert("Item Selected", e.SelectedItem.ToString(), "Ok");
Application.Current.MainPage = new DocketDetail();
}
//((ListView)sender).SelectedItem = null; //uncomment line if you want to disable the visual selection state.
};

Related

Xamarin: Pop modal form after another modal form was pushed

I have a problem to remove a modal from the stack after another modal was pushed.
I have a content page where the user can select a product. Clicking on the ListView opens a modal form with product details. On this modal form is a button to add the product to a wishlist which opens a new modal form for the wishlist details. I want to close the first modal form after the new modal form was launched.
Here is the sample code:
private async void Btn_addToWishlist_Clicked(object sender, EventArgs e)
{
try
{
var item = sender as Button;
if(Controllers.CurrentProductModel == "Products")
{
AllProducts product = item.BindingContext as AllProducts;
var nextpage = new WishlistSelectorPopupPage
{
BindingContext = product
};
await Navigation.PushModalAsync(nextpage);
}
else if(Controllers.CurrentProductModel == "AllProducts")
{
AllProducts product = item.BindingContext as AllProducts;
var nextpage = new WishlistSelectorPopupPage
{
BindingContext = product
};
await Navigation.PushModalAsync(nextpage);
}
Navigation.RemovePage(this);
}
The last line "Navigation.RemovePage(this)" brings an error because it is a modal page.
How can I adress to remove or pop a specific modal page?
would this help?
var previousPage = Navigation.NavigationStack.LastOrDefault();
await Navigation.PushAsync(new CustomPage());
Navigation.RemovePage(previousPage);
or
var _navigation = Application.Current.MainPage.Navigation;
var lastPage = _navigation.NavigationStack.LastOrDefault();
_navigation.RemovePage(lastPage);

Unable to Navigate pages while keeping the tab bar accessible in xamarin forms

So I have a tab bar and some pages attached to it, and what I'm trying to do is navigate to a different page(not a tab bar page) by clicking a button which is present in one of these tab bar pages, on doing so that page is replacing the tab bar, how would I be able to navigate inside the tab bar itself, I checked a couple of threads and doing a PushAsync should do the trick but its not working
This is a snippet of my NavigateAsync Method
public async Task NavigateAsync(string pageKey, object[] parameter, bool animated = true)
{
var page = GetPage(pageKey, parameter);
await CurrentNavigationPage.Navigation.PushAsync(page, animated);
}
private readonly Stack<NavigationPage> _navigationPageStack = new Stack<NavigationPage>();
private NavigationPage CurrentNavigationPage => _navigationPageStack.Peek();
We have a CurrentPage property in TabbedPage. You will need this CurrentPage.Navigation object to push the page inside tab bar itself.
So it turns out in my navigation service, i was using the tabbedpage as a navigationpage to navigate to other pages, but if you get the current page (which should be encapsulated in a navigation while adding them as children to your tabbedpage), then using that if you navigate it creates a new page inside the current page instead of the whole tabbedpage
Here's my syntax for storing the currentPage as my navigation element to navigate
public Page SetRootPage(string rootPageKey = null, Page pageName = null)
{
NavigationPage tempPage = null;
if (rootPageKey != null)
{
var rootPage = GetPage(rootPageKey);
if (rootPage is TabbedPage tabbedRootPage)
{
tempPage = tabbedRootPage.CurrentPage as NavigationPage;
}
if (tempPage == null)
{
CurrentNavigationPage = rootPage is NavigationPage ? (NavigationPage)rootPage : new NavigationPage(rootPage);
}
else
{
CurrentNavigationPage = tempPage;
}
return CurrentNavigationPage;
// as NavigationPage;
//_navigationPageStack.Clear();
//var mainPage = new NavigationPage(rootPage);
// _navigationPageStack.Push(mainPage);
}
else
{
if (pageName is TabbedPage tabbedPage)
{
CurrentNavigationPage = tabbedPage.CurrentPage as NavigationPage;
}
else
{
CurrentNavigationPage = pageName as NavigationPage;
}
return CurrentNavigationPage;
}
}

Hide Navigation Bar on MasterDetailPage

I have a MasterDetailPage that contains the following constructor:
public MainPage()
{
NavigationPage.SetHasNavigationBar(this, false);
NavigationPage.SetHasBackButton(this, false);
InitializeComponent();
MessagingCenter.Subscribe<JobsPage>(this, "OpenMenu", (sender) => {
IsPresented = true;
});
MasterPage.ListView.ItemSelected += ListView_ItemSelected;
this.MasterBehavior = MasterBehavior.Popover;
App.NavPage = new NavigationPage(new JobsPage() { Title = "Jobs" });
Detail = App.NavPage;
}
As you can see, I've set SetHasNavigationBar and SetHasBackButton to false.
On a different page (a ContentPage, not a MasterDetailPage), I did the same thing in the constructor:
NavigationPage.SetHasNavigationBar(this, false);
NavigationPage.SetHasBackButton(this, false);
InitializeComponent();
On my ContentPage, this works fine, as shown below.
On my MasterDetailPage, however, I'm still seeing the Navigation bar.
How can I fix this?
Not sure how is structured your navigation, but try this:
public MainPage()
{
InitializeComponent();
MessagingCenter.Subscribe<JobsPage>(this, "OpenMenu", (sender) => {
IsPresented = true;
});
MasterPage.ListView.ItemSelected += ListView_ItemSelected;
this.MasterBehavior = MasterBehavior.Popover;
var navPage = new NavigationPage(new JobsPage() { Title = "Jobs" });
NavigationPage.SetHasNavigationBar(navPage, false);
NavigationPage.SetHasBackButton(navPage, false);
App.NavPage = navPage;
Detail = App.NavPage;
}
As far as I know, a Master-DetailPage does not support Navigation Bars. It is the root container where you can place your content pages. These pages can now contain a navigation bar that can redirect you to the root page in the Master-DetailPage.
I faced similar issue and i had a work-around for this.
Set the Detail twice.
// 1st time
var jobPage = new JobsPage() { Title = "Jobs" };
Detail = jobPage;
// 2nd time
var navPage = new NavigationPage(jobPage, false);
Detail = navPage;
Set the 1st in constructor and 2nd in OnAppearing.
Try it.
Try setting those lines in detail page of the MasterDetailPage
In code behind
NavigationPage.SetHasNavigationBar(this, false);
In xaml
NavigationPage.HasNavigationBar="False"

callback of Itemselected doesn't work in Xamarin?

I have a simple two pages.
One has a listview.
So when clicks an item of the listView, go to another page.
But I click default back button on my win phone then I click an item of one again, navigation page doesn't work completely.
Here is my code snippet.
in first page
listview.itemselected += listview_ItemSelected;
private void listview_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem = null){
}
else {
Device.BeginInvokeOnMainThread(() => Navigation.PushAsync(new twopage()));
}
}
Please be aware that I am testing on UWP Project based on Xamarin.Form.
MainPage
Page1
this is my screen short
step 1
public App()
{
InitializeComponent();
var mainpage = new NavigationPage(new MainPage());
MainPage = mainpage;
}
step2
private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var page = new Page1();
Navigation.PushAsync(page);
}

How can I create a drawer / slider menu with Xamarin.Forms?

How do I create an a slider menu using Xamarin.Forms? Is it baked in or something custom?
You create a new class which contains all the definitions for both the Master - i.e. the menu - and the Detail - i.e. the main page. I know, it sounds back-to-front, but for example..
using System;
using Xamarin.Forms;
namespace testXamForms
{
public class HomePage : MasterDetailPage
{
public HomePage()
{
// Set up the Master, i.e. the Menu
Label header = new Label
{
Text = "MENU",
Font = Font.BoldSystemFontOfSize(20),
HorizontalOptions = LayoutOptions.Center
};
// create an array of the Page names
string[] myPageNames = {
“Main”,
“Page 2”,
“Page 3”,
};
// Create ListView for the Master page.
ListView listView = new ListView
{
ItemsSource = myPageNames,
};
// The Master page is actually the Menu page for us
this.Master = new ContentPage
{
Title = "The Title is required.",
Content = new StackLayout
{
Children =
{
header,
listView
},
}
};
// Define a selected handler for the ListView contained in the Master (ie Menu) Page.
listView.ItemSelected += (sender, args) =>
{
// Set the BindingContext of the detail page.
this.Detail.BindingContext = args.SelectedItem;
Console.WriteLine("The args.SelectedItem is
{0}",args.SelectedItem);
// This is where you would put your “go to one of the selected pages”
// Show the detail page.
this.IsPresented = false;
};
// Set up the Detail, i.e the Home or Main page.
Label myHomeHeader = new Label
{
Text = "Home Page",
HorizontalOptions = LayoutOptions.Center
};
string[] homePageItems = { “Alpha”, “Beta”, “Gamma” };
ListView myHomeView = new ListView {
ItemsSource = homePageItems,
};
var myHomePage = new ContentPage();
myHomePage.Content = new StackLayout
{
Children =
{
myHomeHeader,
myHomeView
} ,
};
this.Detail = myHomePage;
}
}
}
It is built in: MasterDetailPage. You'd set the Detail and Master properties of it to whatever kinds of Pages you'd like. I found Hansleman.Forms to be quite enlightening.
My minimum example (as posted here) is as follows:
public class App
{
static MasterDetailPage MDPage;
public static Page GetMainPage()
{
return MDPage = new MasterDetailPage {
Master = new ContentPage {
Title = "Master",
BackgroundColor = Color.Silver,
Icon = Device.OS == TargetPlatform.iOS ? "menu.png" : null,
Content = new StackLayout {
Padding = new Thickness(5, 50),
Children = { Link("A"), Link("B"), Link("C") }
},
},
Detail = new NavigationPage(new ContentPage {
Title = "A",
Content = new Label { Text = "A" }
}),
};
}
static Button Link(string name)
{
var button = new Button {
Text = name,
BackgroundColor = Color.FromRgb(0.9, 0.9, 0.9)
};
button.Clicked += delegate {
MDPage.Detail = new NavigationPage(new ContentPage {
Title = name,
Content = new Label { Text = name }
});
MDPage.IsPresented = false;
};
return button;
}
}
An example solution is hosted on GitHub.
On iOS the result looks like this (left: menu open, right: after clicking on "B"):
Note that you need to add the menu icon as a resource in your iOS project.
If you are looking for simple example of MasterDetailPage please have a look at my sample repo at GitHub. Very nice example is also presented here
Slideoverkit is a great plugin available for Xamarin Forms. There is a github to see free samples and you could find documentation about it here.

Resources