Xamarin WebView - Call C# Method - xamarin

Is there a way in Xamarin's WebView that allows me to attach javascript events to my html elements and call C# method.
I could easily do this in Android by using JavaScriptInterface
<video width="320" height="240" controls="controls" poster='poster.gif'
onclick="window.JSInterface.startVideo('file:///sdcard/test.3gp');"
How would I manage to this in Xamarin

Create a JavaScript Interface Class
Create a C# class that contains methods to be called from JavaScript.
If you are targeting Android API level 17 or later, this
JavaScript-to-C# interface class must annotate each
JavaScript-callable method with [JavascriptInterface] and [Export]
as shown in the following example. If you are targeting Android API
Level 16 or earlier, this interface class must implement
Java.Lang.IRunnable as explained in Android API Level 16 and
Earlier (later in this recipe):
Create a C# class that is derived from Java.Lang.Object. In the following example, we name our class MyJSInterface and implement a
method to display a toast when it is called from JavaScript:
public class MyJSInterface : Java.Lang.Object
{
Context context;
public MyJSInterface (Context context)
{
this.context = context;
}
public void ShowToast ()
{
Toast.MakeText (context, "Hello from C#", ToastLength.Short).Show ();
}
}
Annotate each method that is to be exposed to JavaScript with [Export] and [JavascriptInterface] (see IJavascriptInterface
for more information about the JavascriptInterface annotation). In
the following example, the ShowToast method is annotated so that it
can be called from JavaScript. Note that you must include the
Java.Interop and Android.Webkit using statements as shown in this
example:
using Java.Interop;
using Android.Webkit;
...
[Export]
[JavascriptInterface]
public void ShowToast ()
{
Toast.MakeText(context, "Hello from C#", ToastLength.Short).Show();
}
Add a project reference to Mono.Android.Export (so you can use the [Export] annotation):
1.In Visual Studio, right-click References in the Solution Explorer and select Add Reference.... In Xamarin Studio,
right-click References in the Solution Pad and select Edit
References....
2.In the search field, enter Mono.Android.Export. When you have located it, enable the check mark next to it and click OK.
Refer :
http://dotnetthoughts.net/how-to-invoke-c-from-javascript-in-android/
https://developer.xamarin.com/recipes/android/controls/webview/call_csharp_from_javascript/
https://developer.xamarin.com/samples/monodroid/WebViewJavaScriptInterface/
https://developer.xamarin.com/api/type/Android.Webkit.JavascriptInterface/

Related

#inherhits in razor files with ASP.NET Core 3.1

I am trying to use a custom base class for my razor files in ASP.NET Core 3.1 by using #inherits in the cshtml file. Unfortunately Visual Studio 2019 as well as msbuild do not recognize, that a custom base class is used. I need to use this within a website, that I want to port from .NET 4.6.1 to .NET Core 3.1. With .NET 4.6.1 and ASP.NET this is working.
Here is a really simple example. My base class looks like this:
// Really simple base class just for demonstration purposes
public abstract class ViewBase
{
public MyHtmlHelper Html { get; } = new MyHtmlHelper();
public abstract Task ExecuteAsync();
// Just to make the compiler happy
public void WriteLiteral(string v) { }
public void Write(string v) { }
}
public class MyHtmlHelper
{
public string Checkbox() => "";
}
Whereas my cshtml file looks like this:
#inherits Example.ViewBase
#this.Html.CheckBox()
The following image shows that Visual Studio does not understand that this view should be derived from the base class Example.ViewBase. The Property Html should be of type MyHtmlHelper, but it is showing IHtmlHelper<dynamic> instead.
Also the method Checkbox is not recognized correctly.
When compiling this, the following error is shown:
Error CS7036 There is no argument given that corresponds to the required formal parameter 'expression' of 'IHtmlHelper.CheckBox(string, bool?, object)'
It seems to me that with ASP.NET Core 3.1 razor views are tied to specific base class (of MVC?). Is that correct? Is there a way to properly use a custom base class with support by the editor?

How can I launch a Android Activity from Xamarin Forms Page?

I know that i can launch a Xamarin Forms page from an Native Android Activity, but how do i start a Native Android Activity from Xamarin Forms ContentPage. I tried with DependencyService but without success.
I figured it out.
First i needed to create a Interface like this in Xamarin Project
public interface IRecordVideoPage
{
void StartNativeIntentOrActivity();
}
then i launched the Native Android Activity from my Xamarin Forms Content Page using Dependency Service like this:
DependencyService.Register<IRecordVideoPage>();
DependencyService.Get<IRecordVideoPage>().StartNativeIntentOrActivity();
next I created a RecordActivity class in my Android Project that loads the Test Activity
[assembly: Xamarin.Forms.Dependency(typeof(RecordActivity))]
namespace StreamTest.Droid
{
[Activity(Label = "RecordActivity")]
public class RecordActivity : IRecordVideoPage
{
public void StartNativeIntentOrActivity()
{
var intent = new Intent(Forms.Context, typeof(Test));
Forms.Context.StartActivity(intent);
}
}
finally I set the content view in the Test class:
public class Test : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public Test() { }
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Record);
}
}
}
DependencyService is your best option. The concept behind PCL (Portable Class Library) is exactly to be platform independet.
Maybe we can help if you show the error you are getting with Dependency Service

Implementing IntelliSense support for custom language (C++)

I wan to implement IntelliSense support for custom language. Actually it is a customize version of C++. i.e the methods resides in separate files
So my main class is like followings and it has import file MyClassMethods which has all the methods.
public class MyClass {
#import MyClassMethods
// my code goes here
}
So my MyClassMethods fiel looks like following and it has two methods,
public void testMethod1() {
}
public void testMethod2() {
}
Then at the end I want to have IntelliSense features when I working on MyClass. Example when I put dot character on that class in a required place I want to have testMethod1() and testMethod2() in the IntelliSense menu.
Is this possible to achieve and if so how can I achieve this?

How do I provide custom Intellisense descriptions in Visual Studio

Ok so let's say I have the following class in C#:
class Foo
{
public void Bar() { Console.WriteLine("FooBar"); }
}
In Visual Studio, whenever I instantiate my class and implement my method intellisense looks like this:
All this is showing is the name, return type and input parameters of my method. When viewing any method inside any of the .Net base classes using intellisense, a description is provided.
How do I add a description for my own methods to intellisense, so anybody who uses a *.dll I have written in the future can understand what my methods do without having to refer to external documentation?
Thanks
Add xml documentation :
/// <summary>
/// Foos something
/// </summary>
public void Foo()
{
}

Event using generic EventHandler<> not visible in Designer

I've just noticed that if I add an event using a generic eventhandler to my UserControl, the event is not visible in the designer when I add the user control to a form.
public event EventHandler<TEventArgs<int>> EventNotVisibleInDesigner;
public event EventHandler EventVisibleInDesigner;
Not particularly worrisome, but is this by-design/normal, or am I doing something wrong?
The Windows Forms designer has limited support for generic types. It will work okay when you avoid the generic type argument for EventHandler<T>:
public class TEventArgs<T> : EventArgs { }
public class MyEventArgs : TEventArgs<int> { }
public event EventHandler<MyEventArgs> EventNowAlsoVisibleInDesigner;

Resources