A ContentView using dynamic name

Jassim Al Rahma 1,516 Reputation points
2021-06-21T23:42:30.187+00:00

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

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,288 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,191 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,936 Reputation points
    2021-06-22T02:42:15.12+00:00

    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://learn.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.


0 additional answers

Sort by: Most helpful