Access Native Handle for particular control

Hey guys,

how can i access the native Handle for particular UX component,
i would like to access it and have some native code operations

e.g. Image in android i’d like to access the android.widget.ImageView of it

Best,
Ahmad

I recalled we had to fix an issue in Fuse.Share package recently, where on iPad it required a Visual to be passed in as a reference.

Perhaps looking at the implementation will help you figure out how to access Elements.

Great idea, it works fine with me with the following Example

UX code

<JavaScript>
		var AccessUtility = require("AccessUtility");
		AccessUtility.accessElement(element);
</JavaScript>

	<DockPanel>
		<NativeViewHost>
			<Image ux:Name="element" Url="https://c1.staticflickr.com/4/3089/2573503275_a7c2e659f8_b.jpg" />
		</NativeViewHost>
	</DockPanel>

Uno Code

using Uno;
using Uno.Compiler.ExportTargetInterop;
using Android.Base.Wrappers;
using Fuse.Scripting;
using Fuse.Resources;
using Uno.Platform;
using Uno.Threading;
using Uno.UX;
using Fuse.Elements;

[UXGlobalModule]
public class AccessUtility : NativeModule
{
	static readonly AccessUtility _instance;
	public AccessUtility() : base()
	{
		if(_instance != null) return;
		Resource.SetGlobalKey(_instance = this, "AccessUtility");
		AddMember(new NativeFunction("accessElement", AccessElement));
	}

	object AccessElement(Context c, object[] args)
	{
		float2 position = float2(0);
		TryGetPosition(args[0], out position);
		return null;
	}

	bool TryGetPosition(object arg, out float2 position)
	{
		position = float2(0.0f);
		var obj = arg as Fuse.Scripting.Object;
		if (obj != null && obj.ContainsKey("external_object"))
		{
			var element = ((Fuse.Scripting.External)obj["external_object"]).Object as Fuse.Controls.Control;
			// TODO: continue handling the instance fo native view ;)
			if (element.NativeView != null)
				debug_log element.NativeView.ToString();
			//NativeView
			if (element != null)
			{
				var pos = element.ActualPosition;
				var size = element.ActualSize;
				position = pos + size * 0.5f;
				return true;
			}
		}
		return false;
	}

}