0

I cannot call the Detail of the MasterDetailForm. Codes are running but not display. How can i do? "PageLog: Click button to open PageReg" I don't have a problem with Hamburgermenu. The problem is, I can't access from different pages.

MainPage.xml.cs

public partial class MainPage : MasterDetailPage
{
    ... // Main functions...
    public void ExportMDP() // Open new content
    {
        Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(PageReg)));
        IsPresented = false;
    }
}

PageLog.xml.cs

public partial class PageLog : ContentPage
{
    ...
    void RegClick(object sender, EventArgs args) // Button Click
    {
        var x = new project.MainPage();
        x.ExportMDP();
    }
}
RexLond
  • 3
  • 2
  • you need to call ExportMDP on the instance of MainPage that *already exists*, not a new one that you create in memory that is not actually being displayed on the screen – Jason May 19 '19 at 14:32
  • I need example... – RexLond May 19 '19 at 15:00
  • Possible duplicate of [Hamburger Menu Xamarin Forms (MasterDetailPage)](https://stackoverflow.com/questions/49169049/hamburger-menu-xamarin-forms-masterdetailpage) – FreakyAli May 19 '19 at 15:58
  • I don't have a problem with Hamburgermenu – RexLond May 19 '19 at 17:45

1 Answers1

1

According to your description, if you want to call masterdetailPage from other content page, you can use Navigation.PushModalAsync() method to do this.

Navigation.PushModalAsync(new MastDetailPage());

The you can navigate detail page in MasterDetailPage.

Here is the MastDetailPage:

 <MasterDetailPage.Master>
    <local:MasterPage x:Name="masterPage" />
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
    <NavigationPage>
        <x:Arguments>
            <local:ContactsPage />
        </x:Arguments>
    </NavigationPage>
</MasterDetailPage.Detail>

public partial class MainPage : MasterDetailPage
{
    public MainPage()
    {
        InitializeComponent();

        masterPage.listView.ItemSelected += OnItemSelected;

        if (Device.RuntimePlatform == Device.UWP)
        {
            MasterBehavior = MasterBehavior.Popover;
        }
    }

    void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        var item = e.SelectedItem as MasterPageItem;
        if (item != null)
        {
            Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
            masterPage.listView.SelectedItem = null;
            IsPresented = false;
        }
    }
}

Here is the MastPage:

  <StackLayout>

    <ListView x:Name="listView" x:FieldModifier="public">
       <ListView.ItemsSource>
            <x:Array Type="{x:Type local:MasterPageItem}">
                <local:MasterPageItem Title="Contacts" IconSource="contacts.png" TargetType="{x:Type local:ContactsPage}" />
                <local:MasterPageItem Title="TodoList" IconSource="todo.png" TargetType="{x:Type local:TodoListPage}" />
                <local:MasterPageItem Title="Reminders" IconSource="reminders.png" TargetType="{x:Type local:ReminderPage}" />
            </x:Array>
        </ListView.ItemsSource>
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid Padding="5,10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="30"/>
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Image Source="{Binding IconSource}" />
                        <Label Grid.Column="1" Text="{Binding Title}" />
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>

enter image description here

Cherry Bu - MSFT
  • 10,160
  • 1
  • 10
  • 16