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

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.

Related

Uno Platform custom renderer

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.

Custom view in Object Library and IB compatible in Xcode?

I have a control in one of my storyboard that I copy in a lot of screen and even projects. It's just native controls with stack view, scroll views, labels, etc placed with auto-layout already wired together.
Is there a way to add this into the Object Library in Xcode so I can just drag it from there onto my storyboard and still see all the native controls? I want to be able to drag and drop other controls into the stack view and even modify it from there within IB.
I do not want to do this programatically because I want to continue using Interface Builder for my custom control.
s there a way to add this into the Object Library in Xcode so I can just drag it from there onto my storyboard and still see all the native controls?
No. The usual strategy is to keep it in a xib file (so you are using Interface Builder to construct the view complex) and load the nib programatically to get at the view and put it in the interface at runtime. To some extent you can automate that programmatic process by means of a view subclass that loads its own content from the nib.
Or, if this view has different manifestations in different contexts, you might just have to use copy and paste of some template version (presumably you’re already doing that).

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.

OSX Cocoa embed opengl view inside webview

I am trying to build an MacOS app which create an OpenGL view and embed it to webview as it's element (using OpenGL instead of WebGL). The main goal is to use the web (html/javascript) as UI and draw the result with OpenGL view. (besides, the OpenGL view will also handle the user input)
The issue is the overlay between the webview and OpenGL view. If OpenGL view on top of webview, the webview UI (like: dropdown box) possible cut by the OpenGL view. The webview on top of OpenGL view, all of the webview view will block the OpenGL view.
Is there any solution to solve this? (OpenGL and Webview)
WebKit has an interesting functionality where you can associate webviews with certain MIME types to your own custom NSView classes.
In your HTML webview, you can put an iframe which has a custom mimetype (e.g. "application/my-custom-view").
After that, from your native app, you can call:
[WebView registerViewClass:[MyCustomView class] representationClass:[MyCustomRepresentation class] forMIMEType:#"application/my-custom-view"];
See https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Webkit/Classes/WebView_Class/Reference/Reference.html#//apple_ref/occ/clm/WebView/registerViewClass:representationClass:forMIMEType:
MyCustomRepresentation can be pretty lightweight, my Representation class doesn't do anything, it just defines stub delegate functions.

Using custom components in Cappuccino with Interface Builder

I know that in Cocoa, in order to use custom component in Interface Builder I should go to property inspector panel and change, say, my standard NSView to the custom one.
Is there any solution for using custom components in Cappuccino while laying out components in Interface Builder?
You can do the exact same thing.
You have your CPSupaView, you just drop a view in IB and you change the class name to CPSupaView.

Resources