Custom Control in Xamarin.Forms from scratch - c#

I would like to write a custom control which will look like this one using Xamarin.Forms. I know that when we want to modify the existing control from Xamarin.Forms we are using Custom Renderers, but this control is something more than Entry and the solution is not obvious to me.
My question is how do I start working on this task? What control should I extend if I would like to use custom renderer? Is there some other more effective way to achieve this?

Looks like the linked control is extending a UIPickerView.
You have two options
Create the binding for https://github.com/nicklockwood/CountryPicker
Create your own Xamarin.Forms control, which is basically a custom renderer. You can refer the Forms source code to create the controls for it.

You should create a pure Xamarin.Forms view class and renderers (inherited from ViewRenderer) for each platform.
Please have a look at XLabs projects - it should be a good point to start:
Cross-platfrom view: Separator.cs.
Android renderer: SeparatorRenderer.cs.

Related

Is it possible to reference/embed an xib file into a XamarinForms view and inherit from a custom view controller?

We're using a third party SDK that defines its own views(Around 15 xib's) and uses the outlets set on the views to perform some actions. This logic resides in their SDK, driven by their CustomViewController and delegates. We use it pretty much out of the box without having to customize it further.
Is there a way to embed the entire xib into a ContentPage directly, without having to build out the views in xaml? Ideally, we'd like to embed the xibs, and use the SDK's CustomViewController as is. Maybe in a PageRenderer, which is a problem in itself because the PageRenderer is just a wrapper around a UIViewController, but we need to inherit from the CustomViewController.
While we understand that it's possible to embed native UI elements into a Forms view, what we want is to be able to embed the whole xib, and also inherit from a custom VC. Any insight into how these two problems can be tackled?
Basically you have those options, none of them is exactly what you ask and you'll have to find what works the best for you:
building a custom native view with a custom renderer
using the native pages and loading Xamarin ContentPages there as ViewControllers and mixing as you wish with your xibs
Eventually you may also try to access the root UIView and inject your subview there, but it is likely to interfere with Xamarin layout and likely to lead to problems, so do it on your own risk.

Custom Toggle Button using c# code in xamarin forms

I need to create custom toggle button using C# code. Please refer below snap, it should act like a toggle button.
If you don't want to use the plugin, you need to perform custom rendering.
https://github.com/chrispellett/Xamarin-Forms-SegmentedControl
The above github repository has the source code for custom rendering for segmented button in andorid and ios only not for windows.
Take a look at SegmentedButtonGroup in FreshEssentials. It looks like that is what you need.
http://www.michaelridland.com/xamarin/freshessentials-for-xamarin-forms-the-must-have-nuget-for-forms/

Accessing a Custom Renderer Instance from Xamarin Forms Element Shared Code

I am creating a component that uses a custom renderer on each platform. Let's just call it a SpecialButton element. BindableProperty works fine for values but I also want to allow a user to invoke operations on the renderer from shared code (call methods on the renderer from the Forms control code). I was looking at DependencyService as a possible solution but I don't think that will work because it is possible for multiple buttons to appear on the same page so I need the specific renderer instance that was created and linked to my Xamarin Forms element.
So, is there an elegant way for my Xamarin Forms control element to
get access to the custom renderer that was instantiated by the Forms
framework?
I had thought about exposing a property on the control and letting the renderer set itself to the property in its constructor but this feels hacky and also exposes it to the user of the control which I don't want to do.
I think I figured out a solution. I didn't realize the MessagingCenter had a source parameter which can be used to specify the element property.
MessagingCenter.Subscribe(this, "DoOperation", myButton => DoOperation(), Element);

Custom GUI object

I know how to create a simple XAML interface for a page, but I spend a lot of my time copying and pasting code since I have no idea how to create reusable XAML in a proper way, I know about styles but they don't quite fill the role. What I would prefer to do is to call them in the following way:
<CustomElement attribute1="bla" attribute2="{Binding somethingElse}"/>
Just like all the other GUI objects in the Silverlight framework.
Any help or hint would be useful.
Basically there are two options. Both solutions act like any other Silverlight control. They can be inserted at random places in pages like you would normally insert a Button or a StackPanel. The option of choice depends on the specific reuse scenario.
Create a UserControl. These define their own XAML layout and are very easy to create. Consider looking at ScottGu's tutorial.
Create a custom control. Custom controls also define their own XAML layout but through templating and styling. They are harder to create but support templating; this means other developers can decide that they will use the code behind your control, but specify a completely different layout. For more information look at Silverlight templating.
For a (much) better understanding, please look at this page comparing the features of UserControls and custom controls more in-depth.

Monotouch custom controls, how?

I want to create a custom ui control for my monotouch application.
My control consists in a view with 2 labels and 1 image. I need to re-use it more times.
Can i develop a custom uiview in monotouch? is possible to use custom controls in Interface Builder?
thanks.
Take a look at MonoTouch.Dialog

Categories