EricGrahamMacEachern EricGrahamMacEachern - 2 months ago 5
C# Question

How do I change the value of the DetailPage parameter to so that it actually opens a selected page instead of just a blank page?

I am trying to get this search page in my app to work properly. Right now, it searches through the ListView well, but the ListView does not link to the other pages in the app. Instead, when each ListView element is tapped, it just brings up a blank page. I think I made a mistake in how I structure the DetailPage class, but I am not sure how to fix it.

This is the code for Search.cs.

This is a Xamarin forum post where I have been discussing this problem. Someone has helped me and they have been very helpful so far.

I have been searching around for answers in the MSDN, but I have not been able to figure it out yet. How do I change the value of the DetailPage parameter to so that it actually opens a selected page instead of just a blank page?

This is the DetailPage class.

class DetailPage : ContentPage
{
public DetailPage(pageList page_list)
{
this.page_list = page_list;
}

public pageList page_list { private set; get; }

}


This is where it the DetailPage object detailPage gets its parameter itemSelected. It just opens a blank page, which is not what I want.

listView.ItemTapped += async (sender, args) =>
{

var itemSelected = args.Item as pageList;
if (itemSelected != null)
{
DetailPage detailPage = new DetailPage(itemSelected);
await Navigation.PushAsync(detailPage, true);

}

};

Answer

since you know the Type of the page, you can use Activator.CreateInstance to instantiate it

         var itemSelected = args.Item as pageList;
         if (itemSelected != null)
         {
             var page = (ContentPage) System.Activator.CreateInstance(itemSelected.fileName);
             await Navigation.PushAsync(page, true);                                     
         }