Accessing a Custom Renderer Instance from Xamarin Forms Element Shared Code - c#

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);

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.

Binding from code to a custom attached property in WinRT/UWP

I'm trying to create a binding from code in a library that targets multiple frameworks (WPF, WinRT, UWP etc), and I'm hitting a brick wall. The property I'm trying to bind to is a custom attached property. In WPF, I can pass the DependencyProperty itself as the binding path:
new PropertyPath(MyClass.MyAttachedProperty)
But in WinRT the PropertyPath class only accepts a string. I tried to pass the name of the property like this:
new PropertyPath("(MyClass.MyAttachedProperty)")
but of course it doesn't work, since my class is not in the default namespace. In XAML I could map the namespace to a prefix and use that prefix, but as far as I know it's not possible to do this from code.
Is there any way to create this binding in code?
Good question, after researching and discussing we've confirmed that in UWP, we cannot programmatically binding to custom attached property. Sadly.
You may submit a request to add this new features for development through the Windows Feedback tool.
It looks as though there may be a solution here, which involves using XamlReader.Load and a resource dictionary containing the binding, to get the loader to do the work for you.
How can I bind to a custom attached property in c# from code behind in a windows store app?

Custom Control in Xamarin.Forms from scratch

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.

Winform Composite Control Project?

Is it possible to have a single project which allow to include a custom custom control within another custom control ?
Update: of course I'm not asking about how to put a custom control on a winform!!! But if I can create put a CUSTOM user CONTROL inside ANOTHER CUSTOM user CONTROL within the SAME PROJECT.
Sure, you're able to do this. In fact you're able to put a custom control inside another custom control the same way you'd do it on a form (it's essentially the same while editing). You might as well base your custom control off another class (or custom control), but not all will support the built in gui editor (so you might just see an error message but the code will still work).

C# UserControl factory

Let's say you have two classes that extend UserControl. Each of the controls provides a custom event (this could be done by using an interface).
You want to display one of the controls in the odd days and the other in the even days.
You also want to be able to drag&drop (Visual Studio) the UserControl on your form without knowing what the Control type will finally be.
How do you do that ? Is the factory pattern useful here ?
I would make a container control that is added on the form (and that is present in the designer toolbox), that internally uses some factory to create an instance of the actual control to use and then adds it to the container with Dock set to Fill.
You could make a third usercontrol that creates & hosts the usercontrol depending on the day.
But, this has a bad feeling to it, could you explain more in detail what you actually are trying to do?

Categories