I have a master detail page, and I want to define menu to be a navigation page, so when I select some item in menu, it will open another menu with animation. I tried to create a master as navigation page, and push async new page when selecting an item - but it doesn't work for some reason.
Is it possible at all, or there is other way?
You can have a look at this : https://www.syntaxismyui.com/xamarin-forms-masterdetail-page-navigation-recipe/
This is also a good one to look into
Xamarin.Forms - Master/detail page and navigation history issue
You have to wrap your detail page inside a navigation page so that you get the navigation properties. Is that the bit you are missing?
public class RootPage : MasterDetailPage
{
public RootPage ()
{
var menuPage = new MenuPage ();
menuPage.Menu.ItemSelected += (sender, e) => NavigateTo (e.SelectedItem as MenuItem);
Master = menuPage;
Detail = new NavigationPage (new ContractsPage ());
}
void NavigateTo (MenuItem menu)
{
Page displayPage = (Page)Activator.CreateInstance (menu.TargetType);
Detail = new NavigationPage (displayPage);
}
}
Related
I'm creating an app using xamarin Forms (multiplatform), I'm using a Navigation page, but I want to change the arrow ("<-") to text ("back")
Do you know how could i do it?
Thanks
(I'm going to use it in an Android App, but I'm creating the app using Xamarin forms)
You could use custom renderer to remove the navigation icon and set it with text. But, when you do that, you need to capture the click of the text and simulate the back event.
Create the interface:
public class CustomNavigationPage : NavigationPage
{
public CustomNavigationPage(Page startupPage) : base(startupPage)
{
}
}
The implementation of Android:
[assembly: ExportRenderer(typeof(CustomNavigationPage),
typeof(NavigationPageRenderer_Droid))]
namespace NavigationPageDemo.Droid
{
public class NavigationPageRenderer_Droid : NavigationPageRenderer
{
public Android.Support.V7.Widget.Toolbar toolbar;
public Activity context;
public NavigationPageRenderer_Droid(Context context) : base(context)
{
}
protected override Task<bool> OnPushAsync(Page view, bool animated)
{
var retVal = base.OnPushAsync(view, animated);
context = (Activity)Forms.Context;
toolbar = context.FindViewById<Android.Support.V7.Widget.Toolbar>(Droid.Resource.Id.toolbar);
if (toolbar != null)
{
//if (toolbar.NavigationIcon != null)
//{
//toolbar.NavigationIcon = Android.Support.V7.Content.Res.AppCompatResources.GetDrawable(context, Resource.Drawable.back);
//toolbar.NavigationIcon = null;
toolbar.NavigationIcon = null;
toolbar.Title = "back";
toolbar.SetOnClickListener(new OnClick());
//}
}
return retVal;
}
protected override Task<bool> OnPopViewAsync(Page page, bool animated)
{
return base.OnPopViewAsync(page, animated);
}
}
public class OnClick : Java.Lang.Object, IOnClickListener
{
void IOnClickListener.OnClick(Android.Views.View v)
{
App.Current.MainPage.Navigation.PopAsync();
}
}
In the custom renderer, use the OnClickListener to capture the click on text.
when you are working with xamarin forms it is suggested make use of common components and make least use of custom renderer.
Now for your requirement you want to create custom navigation bar
so here is how you can do it.
Create BaseContent Page
Create a Control Template inside your base page your can follow this link
Inside your control template using a grid view place your label with text binding (Back),also your can place a label in center to show title of page again u can make use of template binding which u would come to know when u go through the link
Now inherit your main page with your basecontentpage page
add your control template inside your main page
turn off your navigation bar of your main page
and you are done, this would give u more power to add more things like image or toolbar in your navbar
also to dynamically handle your back button u can check the count from navigationstack if its 0 u can show Humburger Icon or if its more than 0 u can show your label using IsVisible True/False
after i finished slide down menu (which is a stacklayout containing buttons) on button click on my Pcl i am trying to hide this menu when the user taps any part of the page out of my menu
i used TapGestureRecongnizer and added it to content but it doesnot work on other children elements
TapGestureRecognizer ContentGesture = new TapGestureRecognizer();
ContentGesture.Tapped +=(s,o)=>{
if (CornerFrame.IsVisible == true)
{ CornerFrame.IsVisible = false; }
};
this.Content.GestureRecognizers.Add(ContentGesture);
Try this
Below code in your stack layout page
public event EventHandler<bool> ItemChanged;
TapGestureRecognizer ContentGesture = new TapGestureRecognizer();
ContentGesture.Tapped +=(s,o)=>{
ItemChanged?.Invoke(this, true);
};
this.Content.GestureRecognizers.Add(ContentGesture);
Below code in your main page where you add CornerFrame in page
CornerFrame.ItemChanged += (object sender, bool arg) =>
{
if (CornerFrame.IsVisible == true)
{ CornerFrame.IsVisible = false; }
};
I am working in xamarin.forms. I am creaing an android application. In my application I have to use menu. So I took Master detail page to show the menus. And its working fine.
But my problem is before showing the Master detail page, I have to open a content page which doesn't contain the menus. So I took a content page and set it. But when I am running the application after content page, Master detail page is not appearing. The code is running successfully but the Master page is not appearing.
Can any one tell me how I can show the Master detail page after showing simple content page?
The answer will depend on if you want to maintain the navigation stack or not. If you want to add the new page to the current Navigation Stack then you need to so something like this in the Content Page:
((NavigationPage)Parent).PushAsync(newPage);
If you want to make the new page the root of the Navigation Stack then you need to do something like this:
((App) Parent).MainPage = newPage;
If this doesn't work, post your code.
Giving you a trick ! suppose you have a login page ,after authentication you will go to RootPage which is a masterDetailPage .
Take a hint from from below code
namespace LoginNavigation
{
public class App : Application, IloginInterface
{
public static App current;
public static bool IsUserLoggedIn { get; set; }
public static double ScreenWidth;
public static double ScreenHeight;
public App () {
current = this;
MainPage = new LoginPageWithStack ();
}
public void Logout() {
MainPage = new LoginPageWithStack ();
}
public void ShowMainPage() {
MainPage = new RootPage ();
}
}
}
rootPage:
namespace LoginNavigation
{
public class RootPage:MasterDetailPage
{
MenuPage menuPage;
public RootPage () {
ToolbarItems.Add(new ToolbarItem("Filter", "ring.png", async () => {
var page = new ContentPage();
var result = await page.DisplayAlert("Title", "Message", "Accept", "Cancel");
Debug.WriteLine("success: {0}", result);
}));
menuPage = new MenuPage ();
menuPage.Menu.ItemSelected += (sender, e) => NavigateTo (e.SelectedItem as MenuItemForMaster);
//Master = new MasterMenu();
Master = menuPage;
Detail = new NavigationPage (new TimeSheet()){
BarBackgroundColor = Color.FromHex("008dce"),BackgroundColor = Color.FromHex("008dce")
};
}
void NavigateTo (MenuItemForMaster menu) {
if (menu == null)
return;
Page displayPage = (Page)Activator.CreateInstance (menu.TargetType);
//Detail = displayPage;
Detail = new NavigationPage (displayPage) { BarBackgroundColor = Color.FromHex("008dce"),BackgroundColor = Color.FromHex("008dce")};
menuPage.Menu.SelectedItem = null;
IsPresented = false;
}
}
}
So the trick is ,get the current instance of App class and manipulate Mainpage property of it .
When we login,I dont want to display top bar in login_page,from second_page after login I would like to display slide menu.Is it possible to display slide from second page in xamarin.forms?
public class RootPage : MasterDetailPage
{
public RootPage()
{
var menuPage = new MenuPage();
menuPage.Menu.ItemSelected += (sender, e) => NavigateTo(e.SelectedItem as MenuItem);
Master = menuPage;
Detail = new NavigationPage(new DashBoardPage());
}
void NavigateTo(MenuItem menu)
{
Page displayPage = (Page)Activator.CreateInstance(menu.TargetType);
Detail = new NavigationPage(displayPage);
IsPresented = false;
}
}
In App.cs i am using loginpage and in loginpage i dont want ot display top menu.after login i want to display.how to display after login page
public static Page GetMainPage()
{
return new LoginPage();
}
Finally i got the solution for this question:
use:
await Navigation.PushModalAsync(new RootPage());
in place of
await Navigation.PushAsync(new RootPage());
I want to display a drop down menu when a user clicks a button. Something like comboBox but instead of the comboBox its a button. How can I do it??
I solved it using PopupMenu. Here is the code for other's reference.
public static Rect GetElementRect(FrameworkElement element)
{
GeneralTransform buttonTransform = element.TransformToVisual(null);
Point point = buttonTransform.TransformPoint(new Point());
return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
var menu = new PopupMenu();
menu.Commands.Add(new UICommand("Label", (command) =>
{
//do work
}));
// We don't want to obscure content, so pass in a rectangle representing the sender of the context menu event.
// We registered command callbacks; no need to handle the menu completion event
var chosenCommand = await menu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));
if (chosenCommand == null) // The command is null if no command was invoked.
{
}
}
Milan,
You'll need to create a custom control or a user control that combines a button and a popup. You could also just implement this in-place with a button and popup. I suggest you look at Callisto's Menu control and start from there to implement your dropdown menu:
Callisto controls (includes a Menu)