Reputation: 11871
I have a MasterDetailPage and I have sidebar menu? with MasterPageItem. Now I am trying to dismiss the presented MasterPageItem. How would I do this?
Here is my MasterDetailPage:
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyApp;assembly=MyApp"
x:Class="MyApp.MainPage">
<MasterDetailPage.Master>
<local:MasterPage x:Name="masterPage" />
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage>
<x:Arguments>
<local:HomePage />
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
and the OnItemSelected method:
void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = e.SelectedItem as MasterPageItem;
if (item != null)
{
if(item.Title == "Logout")
{
SecureStorage.RemoveAll();
MessagingCenter.Send<Paindown.App>((Paindown.App)Xamarin.Forms.Application.Current, "UpdateMenu");
IsPresented = false;
}
else
{
Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
masterPage.listView.SelectedItem = null;
IsPresented = false;
}
}
}
Here is how I am defining a page in my sidebar menu in MasterPage:
menu.Add(new MasterPageItem
{
Title = "Login",
TargetType = typeof(LoginPage)
});
My question is from LoginPage, how do I go back to HomePage?
Upvotes: 0
Views: 183
Reputation: 12723
You could use MessageCenter send message from LoginPage to MenuPage, then can make the MenuPage set the selected item be HomePage.
For example, send message from LoginPage:
void Button_Clicked(object sender, EventArgs e)
{
//MenuItemType.HomePage is a int value to represent the HomePage
MessagingCenter.Send<object, int>(this, "Home", (int)MenuItemType.HomePage);
}
Then in MenuPage can set the current page be HomePage:
public partial class MenuPage : ContentPage
{
public MenuPage()
{
...
//ListViewMenu is the name of ListView in Slider menu
//menuItems is the ViewModel which contains the HomePage
MessagingCenter.Subscribe<object,int>(this, "Home", (senders,arg) =>
{
ListViewMenu.SelectedItem = menuItems[arg];
});
...
}
}
=================================Update================================
If want a amimation for this, you need to open the slider menu and close it manually.
Before that, we could create a public static Instance
of MainPage(MasterDetailPage). Then we can use it in MenuPage to use IsPresented
property.
MainPage:
public partial class MainPage : MasterDetailPage
{
...
public static MainPage Instance { set; get; }
...
public MainPage()
{
...
Instance = this;
..
}
}
Then MenuPage will modify the previous method of MessagingCenter.Subscribe
as follows:
...
MessagingCenter.Subscribe<object,int>(this, "Home",async (senders,arg) =>
{
// open slider menu
MainPage.Instance.IsPresented = true;
// set selected item
ListViewMenu.SelectedItem = menuItems[arg];
// wait 0.5 second
await Task.Delay(500);
//close slider menu
MainPage.Instance.IsPresented = false;
});
...
Upvotes: 1