I have a problem with navigating to MasterDetailPage. I know how to do it from xaml with button:
<Button IsVisible="False" Grid.Row="6" x:Name="Start" Text="START"
VerticalOptions="Center" Margin="0,0,0,15" HorizontalOptions="Center"
Command="{Binding Path=NavigateCommand}"
CommandParameter="/MasterDetail/NavigationPage/MainPage" />
But how to achieve the same goal using code behind?
I was trying to do this like this:
MasterDetailPage mdet;
Page detpage;
Page mastpage;
mdet = new MasterDetailPage();
detpage = new MainPage();
mastpage = new MasterDetail();
mdet.Master = mastpage;
mdet.Detail = detpage;
App.Current.MainPage = mdet;
But as a result I get:
System.InvalidOperationException: Master and Detail must be set before adding MasterDetailPage to a container
EDIT.
I'm sorry, I wrote the question a bit unclearly. To clarify what i need to achive:
I have a page which is displayed when the application starts. In OnAppearing method I'm checking some conditions. If they're not met the initial page is showed with Button with Command="{Binding Path=NavigateCommand}" and CommandParameter="/MasterDetail/NavigationPage/MainPage". But if the conditions are met I want to go directly to MainPage (achieve the same what is done with mentioned button clicked but without clicking)
EDIT2.
I have two pages, MasterDetail which is type of MasterDetailPage and has a master part definied in xaml and the MainPage which is ContentPage and is added to MasterDetail as detail part. When I'm adding the code Navigation.PushModalAsync(new MasterDetail()); nothing happens. To have any respons I need to do either Navigation.PushModalAsync(new MainPage()); or
var MaDe = new MasterDetail();
MaDe.Detail = new MainPage();
Navigation.PushModalAsync(MaDe);
But in both options after showing the detail page some buttons doesn't work. I have 4 buttons (well, in fact 4 grids with TapGestureRecognizers) which have similar code:
private async void TapImage_Tapped1(object sender, System.EventArgs e)
{
if (App.Current.Properties.ContainsKey("Boxisconf1"))
{
var masterDetail = App.Current.MainPage as MasterDetailPage;
if (masterDetail == null || masterDetail.Detail == null)
return;
var navigationPage = masterDetail.Detail as NavigationPage;
if (navigationPage == null)
{
masterDetail.Detail = new NavigationPage(new Box1());
masterDetail.IsPresented = false;
return;
}
var NavPage = new Box1();
NavPage.Title = "Part 1";
switch (isInsideButOutside1)
{
case 0:
NavPage.Children.Add(new Letter1 {});
NavPage.Children.Add(new Parcel1 {});
NavPage.Children.Add(new Exit1 { });
break;
(...)
}
await navigationPage.Navigation.PushAsync(NavPage);
navigationPage.Navigation.RemovePage(navigationPage.Navigation.NavigationStack[navigationPage.Navigation.NavigationStack.Count - 2]);
masterDetail.IsPresented = false;
}
else
{
await Navigation.PushAsync(new Configure());
}
}
And in case of App.Current.Properties.ContainsKey("Boxisconf1") is true the function returns on the next if (masterDetail is null) and if the first condition is false I'm getting an exception: System.InvalidOperationException: PushAsync is not supported globally on Android, please use a NavigationPage.


NavigateCommandthere's no need to duplicate already existing code. - Freggar