question

80240195 avatar image
0 Votes"
80240195 asked LeonLu-MSFT commented

error on MasterDetailPage

I created a masterdetail page, but it was running

private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var item = e.SelectedItem as MasterDetailPage1MasterMenuItem;
            if (item == null)
                return;

            var page = (Page)Activator.CreateInstance(item.TargetType); //error on this line
            page.Title = item.Title;

            Detail = new NavigationPage(page);
            IsPresented = false;

            MasterPage.ListView.SelectedItem = null;
        }


error below, how to solve?
113896-4.png



dotnet-xamarin
4.png (190.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented

Hello,​

Welcome to our Microsoft Q&A platform!

Your var page = (Page)Activator.CreateInstance(item.TargetType); //error on this line convert is wrong. First of all, I notice you have Model called MasterDetailPage1MasterMenuItem. it is an object, not basic types. So, I create a model to make a test. Please notice when you set the value for PageName, it must set same name of page.

public class MasterDetailPage1MasterMenuItem
    {

        public string Title { get; set; }

        public string PageName { get; set; }
    }

Then in the ListView_ItemSelected method. After we get selectItem object by var item=e.SelectedItem as MasterDetailPage1MasterMenuItem;, we can use var pageType = Type.GetType($"MaterDetails.{item.PageName}"); to convert pagename to pageType. Please notice MaterDetails is your NamespaceOfYourView. And your Page is not MasterDetailPage1MasterMenuItem, it should be the item.PageName

private void MyListview_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var item=e.SelectedItem as MasterDetailPage1MasterMenuItem;
         
            var pageType = Type.GetType($"MaterDetails.{item.PageName}");

            if (item == null)
                return;

           var page= (Page)Activator.CreateInstance(pageType);
            page.Title = item.Title;
            Detail = new NavigationPage(page);
            IsPresented = false;
       
        }


113973-image.png


If you still cannot fix this issue, you can create a new project called MaterDetails. Then Copy my following code.


Here is MainPage.xaml.

<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage  xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MaterDetails.MainPage">

    <MasterDetailPage.Master >
        <ContentPage Padding="10" BackgroundColor="Gray" Title="Master">
            <ContentPage.Content>
                <StackLayout Margin="5,30,5,5">
                  
                    <ListView x:Name="myListview"
                                ItemsSource="{Binding PageLists}">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <TextCell Text="{Binding Title}" />
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

                </StackLayout>
            </ContentPage.Content>
        </ContentPage>
    </MasterDetailPage.Master>
    <MasterDetailPage.Detail>
        <ContentPage Padding="10">
            <ContentPage.Content>
                <StackLayout Margin="5,30,5,5">
                    <Label Text="Detail Page"></Label>
                </StackLayout>
            </ContentPage.Content>
        </ContentPage>
    </MasterDetailPage.Detail>

</MasterDetailPage>


Here is MainPage.xaml.cs.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace MaterDetails
{

    public class MasterDetailPage1MasterMenuItem
    {

        public string Title { get; set; }

        public string PageName { get; set; }
    }
    public partial class MainPage : MasterDetailPage
    {
        public List<MasterDetailPage1MasterMenuItem> PageLists { get; set; }
        public MainPage()
        {
            InitializeComponent();
            PageLists = new List<MasterDetailPage1MasterMenuItem>();
            PageLists.Add(new MasterDetailPage1MasterMenuItem() { PageName= "AboutUs", Title= "About us page" });
            PageLists.Add(new MasterDetailPage1MasterMenuItem() { PageName = "ContactUs", Title = "Contact us page" });
            PageLists.Add(new MasterDetailPage1MasterMenuItem() { PageName = "HomePage", Title = "Home page content" });
            PageLists.Add(new MasterDetailPage1MasterMenuItem() { PageName = "AddEmployee", Title = "Add Employee page" });
            PageLists.Add(new MasterDetailPage1MasterMenuItem() { PageName = "ListEmployee", Title = "List Employee page" });
            BindingContext = this;

            myListview.ItemSelected += MyListview_ItemSelected;

            Detail = new NavigationPage(new HomePage());
            IsPresented = false;
        }

        private void MyListview_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var item=e.SelectedItem as MasterDetailPage1MasterMenuItem;
         
            var pageType = Type.GetType($"MaterDetails.{item.PageName}");

            if (item == null)
                return;

           var page= (Page)Activator.CreateInstance(pageType);
            page.Title = item.Title;
            Detail = new NavigationPage(page);
            IsPresented = false;
       
        }

        
    }
}


AboutUs.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MaterDetails.AboutUs">
    <ContentPage.Content>
        <StackLayout>
            <Label Text="AboutUs"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

AboutUs.xaml.cs, ContactUs, HomePage, AddEmployee and ListEmployee you can create by yourself.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace MaterDetails
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class AboutUs : ContentPage
    {
        public AboutUs()
        {
            InitializeComponent();
        }
    }
}


Here is running screenshot.

114008-image.png 114022-image.png

Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



image.png (135.1 KiB)
image.png (28.8 KiB)
image.png (16.8 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@80240195 May I know whether your issue has been solved or not? If not, please share it in here. We can work together to figure it out.

0 Votes 0 ·