Uno Platform custom renderer - xamarin

Is is possible to create a custom renderer in Uno?
I need to create a native Android view and "embed" it in page or UserControl.
I didn't find any documentation about it.
We need to do the same as this example
https://github.com/UNIT-23/Xam-Android-Camera2-Sample
Thank you!

There's is no renderers in Uno: simply add your native control in the XAML and it should work unchanged.
Example
There's a NativeView test in Uno you can check here, which is used here in XAML.
XAML & Bindings
You can even set native properties directly in XAML, but bindings won't work on those.
If you need bindings, you can create a DependencyProperty and use the callback to set the native value.

Related

Effects vs Behaviors vs Renderers

I'm newbie in Xamarin forms. In xamarin documentation, it seems all effects, behaviors and Renderers give same functionality of modifying native controls. please brief me when exactly we can use each of them to achieve different functionality
BEHAVIOR: Behaviors allow you to enhance the functionality of a Xamarin Forms control without sub classing. Using the Behaviors property you can add behaviors that attached to the Control and can execute code when events of that control are raised. don't need a separate implementation for each platform
EFFECTS: use only when you need a small styling change for any control. Effects need a seperate implementation for each platform.
RENDERER: if you want a customized control for your project create a renderer with native implementation. Renderer need to implemented for each platform

Can I make a Xamarin.Form app (iOS) support Apple Dynamic Type?

In Apple iOS Settings > Display & Brightness there is the ability to change the size of text for applications that use Dynamic Type.
Is it possible to do this with Xamarin.Forms app (iOS custom rendere maybe). If so can someone give an example of how to do this for something like a TableSection Heading and a Label?
For iOS you need for XF TableView TableViewRenderer with native control of UITableView. More here:
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/renderers/
To avoid duplicated answer I will just provide a link
With a custom renderer can I make a TableSection.Title appear in small mixed case?

Get current view for Android in cross-platform (Xamarin)

How can I get current view/layout in Android Xamarin by CustomMapRenderer class? I want to add view in current layout at specific position.
In iOS I am doing it by following lines:
UIView XView;
XView.Frame = new CGRect(50,50,40,40);
Window.AddSubview(XView);
Which View, specifically, are you after? The Xamarin Forms Map itself? Its container? The underlying native Android Control?
If you're after the native control, you have a reference to it in the Control Property of your renderer.
If you're after the Xamarin Forms wrapper for the native piece (a Map in this case), then you want to use the Element property of your renderer.
Both have a Parent property and the Element has a ParentView property.

Xamarin - Custom Renderer - cast Form.View to Android.View

Is it possible to get Android.View (and equivalent view object in iOS) from Xamarin.Form.View? I have custom control which takes dependencies to Android.View. I am writing custom renderer which would set that custom control as native view but I need to convert my ContentView (exposed as BindableProperty, initialized in xaml) from Portable library to Android.View.
Yes you can. I have written an extension that uses reflection to get the underlying native view:
https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/src/Forms/XLabs.Forms.Droid/Extensions/ViewExtensions.cs#L54-L59
Native view in the code is Android.Views.View type and View is Xamarin.Forms.View.
https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/src/Forms/XLabs.Forms.Droid/Extensions/ViewExtensions.cs#L19
The same works for iOS as well, just cast it to UIView.

Adding Xamarin.Forms to an existing Xamarin app

I'm trying to add a Xamarin.Forms page to an existing Xamarin Android app.
I'm struggling to figure out how to mix native Xamarin Android pages and Xamarin.Forms pages.
All the articles and examples I have seen describe either Forms or Xamarin apps and not a hybrid of the two.
The best idea I have read about so far is to add a native stub and replace the content with Xamarin.Forms content but I'm not sure how to achieve this.
Update
I'm using MvvmCross so my android view is inherited from MvxActivity. I have a StackLayout containing views using Xamarin.Forms. How do I get XF to convert to android code so I can use with SetContentView?
When you create a new Xamarin.Forms project, for instance with a SharedProject approach, it will create the launcher project and initial class file that will be executed for you for each platform.
For instance on Android this is in MainActivity.cs. If you look inside this file you will notice that it inherits from AndroidActivity which in turn inherits from Activity, which is the typical class used on activities.
So you have two options available for you to create a mixed hybrid solution:-
1) Inherit from AndroidActivity in your platform-specifc stub-page should you want to display Xamarin.Forms content, or alternatively
2) Inherit from Activity, and do the same as usual.
If you want to display a Xamarin.Forms page from the stub-page you can then just use some boiler-plate code such as the following in the OnCreate method:-
SetPage({some class that returns a Xamarin.Forms.Page object, i.e. ContentPage});
You can then use the normal navigation that is specific to Android to either display a Xamarin.Forms page or a platform-specific Activity page.

Resources