Share via

Dynamic ContentView with Parameters

Anonymous
2021-11-03T21:43:34.597+00:00

Hi,

I am opening a ContentView dynamically like this:

var targetPageType = Type.GetType("MyApp." + page_code);
post_details = (ContentView)Activator.CreateInstance(targetPageType);

I want to ask what if my ContentView has a parameter, for example:

MyView(string id)

and I want to pass it using the same above code because the MyView is populating from my database

Thanks
Jassim

Developer technologies | .NET | Xamarin
Developer technologies | C#
Developer technologies | 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.

0 comments No comments

Answer accepted by question author

Anonymous
2021-11-04T06:49:44.357+00:00

Hello,​

Welcome to our Microsoft Q&A platform!

I want to ask what if my ContentView has a parameter

Yes, you can add parameter like following code. Add new constructor with one parameter.

   public partial class MyCustomEntry : ContentView  
       {  
           public MyCustomEntry()  
           {  
               InitializeComponent();  
           }  
           public MyCustomEntry(int id)  
           {  
               InitializeComponent();  
           }  
   }  

Then, if you opening a ContentView dynamically like following code. For example, My project called ImageLongClickDemo, the custom contentview called MyCustomEntry, then I need transfer 1 as parameter, add the parameter in the end of the targetPageType.

   var targetPageType = Type.GetType("ImageLongClickDemo." + "MyCustomEntry");  
               ContentView post_details = (ContentView)Activator.CreateInstance(targetPageType,1);  

Best Regards,

Leon Lu


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

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.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. P a u l 10,766 Reputation points
    2021-11-04T00:15:41.847+00:00

    Do you mean you have a MyView class and you want to check if it has a constructor that accepts string id? If so you can just use the Type.GetConstructor method to find a constructor that has that type signature, then an additional check to make sure it has the correct type & name, then if it's the correct type we .Invoke() it with the parameter to create it:

    var type = typeof(MyView);
    var ctor = type.GetConstructor(new[] { typeof(string) });
    
    if (ctor != null && ctor.GetParameters().Any(p => p.Name == "id" && p.ParameterType == typeof(string))) {
        var instance = (ContentView)ctor.Invoke(new[] { "test_value" });
    }
    
    abstract class ContentView { }
    
    class MyView : ContentView {
        public MyView(string id) {
        }
    }
    

    Apologies if I've misunderstood your query.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.