question

JassimAlRahma-9056 avatar image
0 Votes"
JassimAlRahma-9056 asked JarvanZhang-MSFT commented

A ContentView using dynamic name

Hi,

I have the contentview name saved in my database for every specific category and I am saving it into a variable called page_code.

In Below code, I am trying to Add the contentview using the variable page_code

 INavigation navService;
    
 navService = App.Current.MainPage.Navigation;
    
 var targetPageType = Type.GetType("Zeera." + page_code);
 var targetPageInstance = (View)Activator.CreateInstance(targetPageType, "test");
    
 GridNewAdDetails.Children.Add(targetPageInstance);



but I am getting below error:

System.MissingMethodException: Constructor on type 'Zeera.NewPostPet' not found.
at System.RuntimeType.CreateInstanceImpl (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes, System.Threading.StackCrawlMark& stackMark) [0x001bf] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/rttype.cs:5535
at System.Activator.CreateInstance (System.Type type, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) [0x00083] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/activator.cs:107
at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/activator.cs:112
at Zeera.NewPost.ButtonNewAdNext_Clicked (System.Object sender, System.EventArgs e) [0x00311] in /Users/jassim/Projects/Zeera/Zeera/NewPost.xaml.cs:244
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021
at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSAction.cs:178
at at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:86
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0000e] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:65
at Zeera.iOS.Application.Main (System.String[] args) [0x00001] in /Users/jassim/Projects/Zeera/Zeera.iOS/Main.cs:17


Kindly help..

Thanks,
Jassim

dotnet-csharpdotnet-xamarin
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

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

Hello,​

Welcome to our Microsoft Q&A platform!

System.MissingMethodException: Constructor on type 'Zeera.NewPostPet' not found.

The MissingMethodException occurs when there is no matching public constructor was found in the type class. How do you define the 'NewPostPet' class? Does it have the constructor like below?

public partial class NewPostPet : ContentView
{
    public NewPostPet(string value)
    {
        InitializeComponent();
    }
}

Please make sure the constructor matches the code. If you don't add the parameterized constructor for the class, please use the Activator.CreateInstance(Type) directly.

var targetPageInstance = (ContentView)Activator.CreateInstance(targetPageType);

Check the doc: https://docs.microsoft.com/en-us/dotnet/api/system.activator.createinstance?view=net-5.0

Best Regards,

Jarvan Zhang


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.

· 4
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.

How can I retreive the targetPageInstance? For example if I want to have different condition for different targetPage targetPageInstance Type and need to do this:

 Iif (targetPageInstance == "Books")
 {

 }
 Ielse if (targetPageInstance == "Phone")
 {

 }


0 Votes 0 ·

You could get the type of the instance to check the condition.

var targetPageType = Type.GetType("TestApplication_5." + "View1");
var targetPageInstance = (ContentView)Activator.CreateInstance(targetPageType);

var type = targetPageInstance.GetType().ToString();

if (type.Contains("View1"))
{
    layout.Children.Add(targetPageInstance);
}
0 Votes 0 ·

One more question please, currently I am declarin g the ontentView manually using this:

 Books_contentview = new NewPostPet();

So how can I do it when dynamically like this:

 var targetPageType = Type.GetType("Zeera." + page_code);
 var targetPageInstance = (ContentView)Activator.CreateInstance(targetPageType);



because when I try to read the value here I am just getting null:

 string title = ((Entry)Books_contentview.FindByName<Entry>("EntryTitle")).ClassId


0 Votes 0 ·

You need to convert the type of the instance first. Such as:

var targetPageInstance = (Books)Activator.CreateInstance(targetPageType);
 string title = ((Entry)targetPageInstance.FindByName<Entry>("EntryTitle")).ClassId;
0 Votes 0 ·