Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Xamarin.Forms How to create custom Forms view

RedRaRedRa Member ✭✭✭
edited August 12 in Xamarin.Forms

I have faced with issue that I do not know how to create custom Forms view that renders differently on each platform Android, iOS ...
On Android it is FrameLayout with some custom content, on iOS it is another view or layout with custom content ...

Seems like I have to use Custom Renderer, but I do not know which Renderer to use ... ViewRenderer, FrameRenderer, MapRenderer ...

I mean I do not have class from which I can inherit from like in this example:

    public class MyEntry : Entry
    {
    }

    public class MyEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged (e);

            if (Control != null) {
                // do whatever you want to the UITextField here!
                Control.BackgroundColor = UIColor.FromRGB (204, 153, 255);
                Control.BorderStyle = UITextBorderStyle.Line;
            }
        }
    }

I do not have class like Entry because on each platform I have specific rendering ...

Can somebody show direction ?

Best Answers

  • RedRaRedRa Member ✭✭✭
    Accepted Answer

    @LandLu said:
    The Android project in Xamarin Forms only contains one single activity.
    If you want to retrieve the bundle from the activity, try to define a static property in the MainActivity like:

    public static MainActivity Instance;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;
    
        base.OnCreate(savedInstanceState);
    
    
        Instance = this;
    
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
    }
    

    And then we could access the activity anywhere in the Android project:

    MainActivity.Instance
    

    Thanks, all !!

    I did it with CustomRenderer.Init(savedInstanceState)

    Also to create custom renderer I have inherited from ViewRenderer and to manipulate visibility instead of OnCreate, OnPause, OnResume I use OnVisibilityChanged

Answers

  • RedRaRedRa Member ✭✭✭

    Okay, I know this link, but what should I use if I do not have class from which I can inherit ???

  • NMackayNMackay GBInsider, University admin

    You can override appropriate events in the page renderer and see how UI elements are rendereded natively.

    Just a case of figuring out what you want to tweak, obviously it's much more efficient at specific renderer level.

    Have you tried debugging at this level?

  • RedRaRedRa Member ✭✭✭

    @NMackay
    But how can I put Android Bundle in custom renderer on View creation ?

  • RedRaRedRa Member ✭✭✭
    Accepted Answer

    @LandLu said:
    The Android project in Xamarin Forms only contains one single activity.
    If you want to retrieve the bundle from the activity, try to define a static property in the MainActivity like:

    public static MainActivity Instance;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;
    
        base.OnCreate(savedInstanceState);
    
    
        Instance = this;
    
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
    }
    

    And then we could access the activity anywhere in the Android project:

    MainActivity.Instance
    

    Thanks, all !!

    I did it with CustomRenderer.Init(savedInstanceState)

    Also to create custom renderer I have inherited from ViewRenderer and to manipulate visibility instead of OnCreate, OnPause, OnResume I use OnVisibilityChanged

  • NMackayNMackay GBInsider, University admin

    @RedRa
    Thanks for the update, glad you got it working, you can also use Xamarin essentials to get at the current activity

    var context = Platform.CurrentActivity;
    if (context.GetSystemService(Context.InputMethodService) is InputMethodManager inputMethodManager && context != null)
            {
                var activity = context as Activity;
                var token = activity.CurrentFocus?.WindowToken;
    
Sign In or Register to comment.