2

I'm working on a Xamarin Forms App. This App has a ContentPage as MainPage, this ContenPage has 2 buttons. One of them, after being clicked, shows a new Page (I'm using App.Current.MainPage = page;) and this new page has a Nav Drawer.

The Nav Drawer shows perfectly, and all its pages work fine, but what I want to do is open a new page (lets call it "events") from one of this nav drawer pages. Every time a Nav Drawer Item is clicked, I use the code above:

if (menu == null)
   return;
Page displayPage = (Page)Activator.CreateInstance(menu.TargetType); //the error shows here after trying to open my "events" page
Detail = new NavigationPage (displayPage);
menuPage.Menu.SelectedItem = null;
IsPresented = false;

But then, on my Nav Drawer Page, I'm trying to open my "events" page, but I don't know how to open it. I tried to use Navigation.PushAsyng(page) or App.Current.MainPage = page; but none of those work. Everytime I try to open it, this Unhandled Exception shows

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation

I use this reference for creating my nav drawer https://syntaxismyui.com/xamarin-forms-masterdetail-page-navigation-recipe/ So, does someone know how to solve this problem and open my "events" page? I've been trying to do this for a long time without any results.

Thanks!

  • if you try hardcoding "Page displayPage = new EventsPage()" (whatever the name of your events page is) does it work? – Jason Mar 25 '16 at 21:10
  • Thanks for your answer! I don't know where I am supossed to write that... Could you please help me with that? – Juan Carlos Durini Serrano Mar 25 '16 at 21:28
  • replace the line where you create displayPage with Activator - if it still breaks that means something is wrong with your Events page. If it works, then the problem is more likely related to Activator/reflection. – Jason Mar 25 '16 at 21:45
  • I can't do that, because I want to make an instance of the item selected from my Nav Drawer! There is an example of that on the link I included. Anyway, If I replace that line with the code you gave me, it shows a compiling error! – Juan Carlos Durini Serrano Mar 25 '16 at 22:35
  • I was trying to help you determine what is causing the error - Activator or something in your Events page. My suggestion was not a permanent code change. – Jason Mar 25 '16 at 23:26
  • Alternately, you can look at the InnerException property of the exception that is thrown to see if that tells you the root cause. – Jason Mar 25 '16 at 23:27
  • Okey, I'll do that. Thanks Jason :) – Juan Carlos Durini Serrano Mar 26 '16 at 00:25
  • Jason, I've solved my problem... It was something stupid actually. The exception was showing because I had a button onClick-event creation before I was initializing the button itself... But thanks anyway ! – Juan Carlos Durini Serrano Mar 26 '16 at 19:24

0 Answers0