PageRenderer does not work with camera2 API - xamarin

I am trying to get Camera2 API to work from Xamarin Forms through Page Rendering.
[assembly: ExportRenderer(typeof(CameraPage), typeof(CameraActivity))]
namespace BlueDemo.Droid
{
[Activity(Label = "CameraActivity")]
public class CameraActivity : PageRenderer
{
Activity activity;
TextureView textureView;
global::Android.Views.View view;
public CameraActivity(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
{
return;
}
try
{
SetupUserInterface();
AddView(view);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(#" ERROR: ", ex.Message);
}
}
void SetupUserInterface()
{
//activity = Context as Activity;
view = activity.LayoutInflater.Inflate(Resource.Layout.activity_camera, this, false);
activity.FragmentManager.BeginTransaction().Replace(Resource.Id.container, Camera2BasicFragment.NewInstance()).Commit();
}
The break point does hit the LayoutInflater but it does not open the view for the camera. The original Camera2 API: Camera2 API link for camera activity
This has SetContentView which does not work with Page Renderer.

Related

Xamarin.macOS Navigated event not fire for CustomWebView

I have created custom renderer for my WebView because I need to set property CustomUserAgent. But for my CustomWebView event Navigated is not fired. As I understand I need set NavigationDelegate but can't find any working example. Here is my code:
[assembly: ExportRenderer(typeof(CustomWebView), typeof(CustomWebViewRenderer))]
namespace DesktopClient.MacOS.CustomRenderers
{
public class CustomWebViewRenderer : ViewRenderer<CustomWebView, WKWebView>
{
private WKWebView wkWebView;
protected override void OnElementChanged(ElementChangedEventArgs<CustomWebView> e)
{
base.OnElementChanged(e);
if (this.Control == null)
{
WKWebViewConfiguration config = new();
this.wkWebView = new WKWebView(this.Frame, config)
{
NavigationDelegate = new WKNavigationDelegate()
};
this.SetNativeControl(this.wkWebView);
WKWebViewContainer.WKWebView = this.wkWebView;
}
if (e.NewElement != null)
{
UrlWebViewSource source = (UrlWebViewSource)e.NewElement.Source;
if (source != null)
{
this.Control.LoadRequest(new NSUrlRequest(new NSUrl(source.Url)));
}
this.Control.CustomUserAgent = e.NewElement.UserAgent;
this.wkWebView.NavigationDelegate = new WKNavigationDelegate();
}
}
}
}
Create a new class inherit from WKNavigationDelegate and assign to Webview.NavigationDelegate ,and then you can override the method like DidFinishNavigation in the new class .
Sample code
NavigationDelegate = new MyDelegate();
public class MyDelegate : WKNavigationDelegate {
public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{
}
}
However , I found the method DidFinishNavigation does not trigger at initial load ,it can only be triggered when we navigate to other web page.

How to get a callback from a custom renderer in xamarin forms

I Have a custom hybridWebView for android and ios to load the URL. What I required is to pass a callback to the content page once the URL has completed the loading. Code as below, help would much appreciate.
Content Page
public partial class ConveyancingLeadPage : ContentPage
{
DashboardViewModel viewmodel;
StorageService storage = new StorageService();
public ConveyancingLeadPage()
{
InitializeComponent();
GetUserAvatar();
}
protected async override void OnAppearing()
{
// I need the callback to be execute here
customView.weblink = viewmodel.BrokerData.config.conveyancing.listing_webview;
}
}
Android HybridView
[assembly: ExportRenderer(typeof(HCHybridWebview), typeof(HCHybridWebviewRendererAndroid))]
namespace HashChing.Droid.CustomRenderers
{
public class HCHybridWebviewRendererAndroid : ViewRenderer<HCHybridWebview, Android.Webkit.WebView>
{
Context _context;
public HCHybridWebviewRendererAndroid(Context context) : base(context)
{
_context = context;
}
protected override void OnElementChanged(ElementChangedEventArgs<HCHybridWebview> e)
{
base.OnElementChanged(e);
const string JavascriptFunction = "function invokeCSharpAction(data){jsBridge.invokeAction(data);}";
if (Control == null)
{
//Do something
if (e.NewElement != null)
{
//Load URL
Control.AddJavascriptInterface(new JSBridge(this), "jsBridge");
var hybridWebView = e.NewElement as HCHybridWebview;
if (hybridWebView != null)
{
hybridWebView.RefreshView += LoadUrl;
}
}
Load URL
public void LoadUrl(object sender, EventArgs e)
{
Control.LoadUrl(webView.weblink, headers);
}
Once the URL been loaded it will navigate to this method in the same class, and this is where I want to pass a callback TO my content page once the loading is completed inside the "OnPageFinished" method. Help would much appreciate.
public class JavascriptWebViewClient : WebViewClient
{
string _javascript;
public JavascriptWebViewClient(string javascript)
{
_javascript = javascript;
}
public override void OnPageFinished(Android.Webkit.WebView view, string url)
{
base.OnPageFinished(view, url);
view.EvaluateJavascript(_javascript, null);
}
}
you could use MessagingCenter to send and get the callback:
in your ContentPage,for example Page1.xaml.cs
public Page1 ()
{
InitializeComponent ();
//here you could get the callback,and arg = "this is call back"
MessagingCenter.Subscribe<Page1,string>(this,"callback", (send, arg) =>
{
Console.WriteLine(arg);
});
}
then in your OnPageFinished method:
public override void OnPageFinished(Android.Webkit.WebView view, string url)
{
base.OnPageFinished(view, url);
view.EvaluateJavascript(_javascript, null);
//send the callback content,Parameters can be defined by yourself
MessagingCenter.Send<Page1,string>(new Page1(), "callback","this is call back");
}
more information:MessagingCenter
create your own event LoadCompleted in your customrender and invoke it from your custom class once load is finish.
And in your JavascriptWebViewClient class you can subscribe to that event and do whatever you want do do at that time.
in case you need it: Events in c#
public partial class ConveyancingLeadPage : ContentPage
{
protected async override void OnAppearing()
{
customView.weblink = viewmodel.BrokerData.config.conveyancing.listing_webview;
// I need the callback to be execute here
customView.LoadCompleted+=LoadCompleted;
}
}

SwitchRender.SwitchRender()' is obsolete with Xamarin.Forms renderer

I am implementing code for SwitchRenderer in Android but it appears that there is an issue with the code as I get a warning:
'SwitchRender.SwitchRender()' is obsolete; 'This constuctor is
obsolets as of version 2.5. Please use SwitchRender(Context) instead.'
Does anyone have any ideas how I can resolve this problem:
using Japanese.Android;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Japanese;
[assembly: ExportRenderer(typeof(ExtSwitch), typeof(ExtSwitchRenderer))]
namespace Japanese.Android
{
public class ExtSwitchRenderer : SwitchRenderer
{
private ExtSwitch view;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Switch> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || e.NewElement == null)
return;
view = (ExtSwitch)Element;
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBean)
{
if (this.Control != null)
{
if (this.Control.Checked)
{
this.Control.TrackDrawable.SetColorFilter(view.SwitchOnColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
}
else
{
this.Control.TrackDrawable.SetColorFilter(view.SwitchOffColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
}
this.Control.CheckedChange += this.OnCheckedChange;
UpdateSwitchThumbImage(view);
}
//Control.TrackDrawable.SetColorFilter(view.SwitchBGColor.ToAndroid(), PorterDuff.Mode.Multiply);
}
}
private void UpdateSwitchThumbImage(CustomSwitch view)
{
if (!string.IsNullOrEmpty(view.SwitchThumbImage))
{
view.SwitchThumbImage = view.SwitchThumbImage.Replace(".jpg", "").Replace(".png", "");
int imgid = (int)typeof(Resource.Drawable).GetField(view.SwitchThumbImage).GetValue(null);
Control.SetThumbResource(Resource.Drawable.icon);
}
else
{
Control.ThumbDrawable.SetColorFilter(view.SwitchThumbColor.ToAndroid(), PorterDuff.Mode.Multiply);
// Control.SetTrackResource(Resource.Drawable.track);
}
}
private void OnCheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
{
if (this.Control.Checked)
{
this.Control.TrackDrawable.SetColorFilter(view.SwitchOnColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
}
else
{
this.Control.TrackDrawable.SetColorFilter(view.SwitchOffColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
}
}
protected override void Dispose(bool disposing)
{
this.Control.CheckedChange -= this.OnCheckedChange;
base.Dispose(disposing);
}
}
}
Check out the answer here. The new way to do Android custom renderers would be this:
public class ExtSwitchRenderer : SwitchRenderer
{
private ExtSwitch view;
public ExtSwitchRenderer(Context context) : base(context) { }
....
}
Then if you actually need to use the Android context in your custom renderer, you would use the one passed in through the constructor, instead of Forms.Context for example.

Is there a way to recognize a short or long press on a screen with Xamarin.Forms?

I have an application that responds to a short tap on the screen. I do this by adding a gesture recognizer.
Is there a way that I can make it respond to either a short or a long press and have these call different methods?
You will have implement renderers for that. In case of iOS you can use UILongPressGestureRecognizer to detect a long-press action, while in case of Android, you can use GestureDetector to do the same.
Forms control
public class CustomView : ContentView
{
public event EventHandler<EventArgs> LongPressEvent;
public void RaiseLongPressEvent()
{
if (IsEnabled)
LongPressEvent?.Invoke(this, EventArgs.Empty);
}
}
iOS renderer
[assembly: ExportRenderer(typeof(CustomView), typeof(CustomViewRenderer))]
namespace AppNamespace.iOS
{
public class CustomViewRenderer : ViewRenderer<CustomView, UIView>
{
UILongPressGestureRecognizer longPressGestureRecognizer;
protected override void OnElementChanged(ElementChangedEventArgs<CustomView> e)
{
longPressGestureRecognizer = longPressGestureRecognizer ??
new UILongPressGestureRecognizer(() =>
{
Element.RaiseLongPressEvent();
});
if (longPressGestureRecognizer != null)
{
if (e.NewElement == null)
{
this.RemoveGestureRecognizer(longPressGestureRecognizer);
}
else if (e.OldElement == null)
{
this.AddGestureRecognizer(longPressGestureRecognizer);
}
}
}
}
}
Android renderer
[assembly: ExportRenderer(typeof(CustomView), typeof(CustomViewRenderer))]
namespace AppNamespace.Droid
{
public class CustomViewRenderer : ViewRenderer<CustomView, Android.Views.View>
{
private CustomViewListener _listener;
private GestureDetector _detector;
public CustomViewListener Listener
{
get
{
return _listener;
}
}
public GestureDetector Detector
{
get
{
return _detector;
}
}
protected override void OnElementChanged(ElementChangedEventArgs<CustomView> e)
{
base.OnElementChanged(e);
if (e.OldElement == null)
{
GenericMotion += HandleGenericMotion;
Touch += HandleTouch;
_listener = new CustomViewListener(Element);
_detector = new GestureDetector(_listener);
}
}
protected override void Dispose(bool disposing)
{
GenericMotion -= HandleGenericMotion;
Touch -= HandleTouch;
_listener = null;
_detector?.Dispose();
_detector = null;
base.Dispose(disposing);
}
void HandleTouch(object sender, TouchEventArgs e)
{
_detector.OnTouchEvent(e.Event);
}
void HandleGenericMotion(object sender, GenericMotionEventArgs e)
{
_detector.OnTouchEvent(e.Event);
}
}
public class CustomViewListener : GestureDetector.SimpleOnGestureListener
{
readonly CustomView _target;
public CustomViewListener(CustomView s)
{
_target = s;
}
public override void OnLongPress(MotionEvent e)
{
_target.RaiseLongPressEvent();
base.OnLongPress(e);
}
}
}
Sample Usage
<local:CustomView LongPressEvent="Handle_LongPress" />
Code-behind
void Handle_LongPressEvent(object sender, System.EventArgs e)
{
//handle long press event here
}
You can also customize above to add a command to make it more MVVM friendly.
You can refer this link for more details regarding gesture recognizers.
http://arteksoftware.com/gesture-recognizers-with-xamarin-forms/
You will have implement renderers for that. In case ios and android
best way for do that!

How to use wikitude plugin in xamarin forms?

I am using Xamarin to develop cross platform AR application. I am using Wikitude instant tracking.
I am able to start the Wikitude activity and able to run the Instant tracking...Now I want capture the high resolution image while tracking...I am trying to build the plugin to get the frame and then convert it to image stream
Her is my Wikitude activity
namespace XamarinExample.Droid
{
[Activity(Label = "WikitudeActivity")]
public class WikitudeActivity : Activity, ArchitectView.IArchitectUrlListener
{
ArchitectView architectView;
string worldUrl;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.sample_cam);
Title = Intent.GetStringExtra("id");
worldUrl = "Wikitude" + File.Separator + Intent.GetStringExtra("id") + File.Separator + "index.html";
architectView = FindViewById<ArchitectView>(Resource.Id.architectView);
ArchitectStartupConfiguration startupConfiguration = new ArchitectStartupConfiguration();
startupConfiguration.setLicenseKey(Constants.WIKITUDE_SDK_KEY);
startupConfiguration.setFeatures(ArchitectStartupConfiguration.Features.ImageTracking);
startupConfiguration.setCameraResolution(CameraSettings.CameraResolution.Auto);
/////////////////////////////// Register Plugin////////////////////////////////////
var plugins = new Plugin01("test");
architectView.RegisterPlugin(plugins);
architectView.OnCreate(startupConfiguration);
architectView.RegisterUrlListener(this);
}
}
My Plugin code taken from
public class Plugin01 : Com.Wikitude.Common.Plugins.Plugin
{
public Plugin01(string p0) : base(p0)
{
}
Frame currentFrame = null;
public override void CameraFrameAvailable(Frame p0)
{
System.Diagnostics.Debug.WriteLine("AVAILABLE FRAME");
try
{
var data = p0.GetData();
currentFrame = p0;
}
catch (System.Exception ex) { }
}
public override void Update(RecognizedTarget[] p0)
{
System.Diagnostics.Debug.WriteLine("AVAILABLE FRAME");
if (p0 != null)
{
if (currentFrame != null)
{
// ConvertYuvToJpeg(currentFrame, p0[0]);
}
}
}
}
I have registered the plugins but it is not calling
public override void Update(RecognizedTarget[] p0) Method....What am I doing wrong here ?
I think the problem is calling "RegisterPlugin" in the wrong method, as you know the cycle of calling activity methods are different.you should call it in "OnPostCreate" method of activity.
try below code and let me know the result:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
try
{
SetContentView(Resource.Layout.Main);
architectView = FindViewById<ArchitectView>(Resource.Id.architectView);
var config = new ArchitectStartupConfiguration();
config.setLicenseKey(WIKITUDE_SDK_KEY);
architectView.OnCreate(config);
}
catch (Exception ex) { Toast.MakeText(this, ex.ToString(), ToastLength.Long); }
}
protected override void OnPostCreate(Bundle savedInstanceState)
{
base.OnPostCreate(savedInstanceState);
if (architectView != null)
architectView.OnPostCreate();
try
{
try
{
string url = string.Format(#"file:///android_asset/01_ImageRecognition_1_ImageOnTarget/index.html");
architectView.Load(url);
Plugin01 cardPlugin = new Plugin01("com.plugin.dpiar");
architectView.RegisterPlugin(cardPlugin);
}
catch (Exception ex) { }
}
catch (Exception ex) { Toast.MakeText(this, ex.ToString(), ToastLength.Long); }
}
consider changing variables name.

Resources