Calling Xamarin.Android Interop from Xamarin.UITest - xamarin

I've created a method in the first Activity of my app which I have added the Java.Interop.Export tag to, as below.
[Export]
public string MyInvokeMethod(string myString)
{
_webview.LoadUrl("http://www.google.co.uk");
return "test";
}
And I'm trying to call this method in Xamarin.UITest with the below.
app.Invoke("MyInvokeMethod","test");
However, nothing seems to happen. I looked into the logs of the Android device and it seems as though it is trying to call the method but it gets below error.
"Method not found with correct argument types. Trying to type convert."
Is there a way to get this to work? I have tried with passing an argument, without passing an argument, adding a name for the method in the attribute but nothing has worked.
EDIT
After speaking to one of the Xamarin developers they believe it might be an issue with how MvvmCross handles the Android Activity.

I think you should add
[Export("myInvokeMethod:")]
on top of your method, instead of only add [Export]
Someone using xamarin uitests might find this useful too: https://forums.xamarin.com/discussion/57662/xamarin-forms-uitest

Related

Xamarin Forms - calling a shared code method from the platform project

I have read the two other questions on SO regarding this and I wanted to know if there is a good solution for that now / best practice.
Long story short, we use an SDK which is written natively and we've wrapped it so that it works on Xamarin.Android and Xamarin.iOS. It has asynchronous callback methods. I need to call a method in the shared code when a callback is received in the Android project for instance.
There's a lot of info for doing the opposite - using DependencyService. How about in my scenario? Does anyone have experience with an app like this and what's the best approach to keep code clean and do this using MVVM?
The options I know are:
Using a static App instance - this is what we currently do.
MessagingCenter
Anything else?
Actually I've never seen anyone recommend usage of MessagingCenter for anything else than communication between ViewModels so I am not sure it is recommended here. Also, I need to know the sender object type so I need a reference to the class in the platform specific project.
I would recommend you to use messagingCenter to pass data or call method between shared project and platform project. You can just send a new object instead of the class in the platform specific project.
Also, have a look at using eventhandler as I mentioned in this answer may help someone who want to call from the shared project into the platform specific one.
BTW, I mean you can even pass an object as TSender if it is not necessary to use:
MessagingCenter.Send<Object>(new object(), "Hi");
MessagingCenter.Subscribe<Object>(new object(), "Hi", (sender) =>
{
// Do something whenever the "Hi" message is received
});

Environment.System.Environment.SpecialFolder.MyDocuments Different in Xamarin.UITest than in app

I have a Xamarin Forms Project That targets iOS. I am trying to write Xamarin.UITests that depend on existence of certain files inside the /Documents folder on the device
In trying to read/write the files to the correct Folder here is what I found.
string documentBasePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
Results From actual app :
"/Users/markwardell/Library/Developer/CoreSimulator/Devices/147FD387-FCAD-4E93-BFC7-4BC1572FF7D4/data/Containers/Data/Application/13E0B91B-8C70-4139-B570-7431DDF5B5CA/Documents"
Results From Xamarin.UITest :
"/Users/markwardell/”
Clearly I need a way of getting same result as 1..
How can I get the folder results same as app in the UITest?
The UITest project does not have access to the Xamarin.iOS SDK which is the SDK that is giving you the path from the actual app when deployed to a device or simulator. IOW, the System namespace in Xamarin.iOS's version of .NET/Mono implements some things differently depending on the platform, as is necessary in this case since the documents path is different on iOS than it is on Android, than it is on Windows, etc. So this is why the paths are different.
That said, you can get around this by using a backdoor method. See:
https://learn.microsoft.com/en-us/appcenter/test-cloud/uitest/working-with-backdoors
This allows you to call a method implemented in the iOS project itself, thereby using Xamarin.iOS SDK in that method.
You implement the backdoor method in your AppDelegate class in your iOS app project like so:
[Export("getMyDocumentsPath:")] // notice the colon at the end of the method name
public NSString GetMyDocumentsPath(NSString value)
{
// In through the backdoor - do some work.
return new NSString(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments));
}
Then call it from your UI Test project:
var path = app.Invoke("getMyDocumentsPath:", "").ToString();
Worth noting from the linked doc (in case it ever goes away):
On iOS, IApp.Invoke can call a C# method on the project's AppDelegate according to the following rules:
The method must be public.
The method must be adorned with the ExportAttribute and the name of the exposed C# method identified. The exposed name must append a : (colon) to the name. IApp.Invoke must use the iOS form of the method name.
The method must take a parameter of NSString.
The method must return NSString or void.

NoSuchMethodError using Twilio IPMessaging

I'm trying to use Twilio IP Messaging in a simple Xamarin Android project, but unfortunately running into issues with the latest Nuget libraries: Twilio.Common (v. 0.3.4.2) and Twilio.IPMessaging (I tried both 0.15.0.4 and 0.15.0.6).
My setup is complicated by the fact that the online Xamarin samples are obsoleted within the recent library releases --- so instead of calling methods to Initialize the Twilio SDK, my code simply invokes Twilio.IPMessaging.IPMessagingClient.Create. The input parameters are a bit unclear, but reading elsewhere I'm trying to bind using a signature:
IPMessagingClient IPMessagingClient.create(
Context context,
AccessManager accessManager,
IPMessagingClient.Properties clientProperties,
Constants.CallbackListener<IPMessagingClient> listener)
Invoking it this way, I invariably receive an error message: NoSuchMethodError with details:
"no static method \"Lcom/twilio/ipmessaging/IPMessagingClient;.create(Landroid/content/Context;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Lcom/twilio/ipmessaging/IPMessagingClient;\"
Any ideas? I like the idea of using Twilio, but between the outdated documentation and unfortunate crashes it's looking simpler to just implement messaging myself.
While I check nugets can you provide more insights
why IPMessagingClient.create(...) and not IPMessagingClient.Create(...)?
Is linker turned on? Turn it off please. Then check.
Signature is
public static IPMessagingClient Create (Context context, Object acess_manager, Object properties, Object listener);

Exception while running ibtool: Cannot find value transformer with name

I just upgraded Xcode to 8.0 (8A218a) and am converting my project in Swift 2.3 to Swift 3.0. The only issue left now is this error:
"Exception while running ibtool: Cannot find value transformer with
name UTIToIconTransformer"
The UTIToIconTransformer is defined something like:
#objc(UTIToIconTransformer) class UTIToIconTransformer : ValueTransformer {
// ...
}
The code worked fine when it was in Swift 2.3. The binding using this value transformer is set like this:
If I remove this binding, the app runs, and the row titles are shown correctly.
I have tried calling NSValueTransformer.setValueTransformer() in the app delegate's +initialize(), in applicationDidFinishLaunching and in the value transformer's +initialize(), as suggested here, here at StackOverflow and here at NShipster (Though I don't think the statement of "Typically, the singleton instance would be registered in the +initialize method of the value transformer subclass, so it could be used without further setup." complies with the Apple's doc.), all without success.
In the Apple's doc, it says
Value transformers are typically registered by an application’s delegate
class, in response to receiving a initialize: class message. This allows
registration to occur early in the application startup process, providing
access to the value transformers as nib files load.
Availability in Interface Builder
Your NSValueTransformer subclasses are not automatically listed in the
Interface Builder bindings inspector. When inspecting a binding you can enter
the name that the value transformer is registered with, but the functionality
will not be present in Interface Builder’s test mode. When your application
is compiled and run the transformer will be used.
But registering in the AppDelegate's override class func initialize() didn't help. In Xcode 7 and Swift 2.3, it even worked without the registration.
Finally I solved the problem by removing the NSOutlineView from the storyboard and setting up a new one.
I have another project which also has an outlineview binded with an NSTreeController, and that project has no problem after the Xcode 8.0 upgrade. Then I tried creating a new ValueTransformer with a new name, with no luck.
I guess there may be something wrong with the storyboard, so I tried recreating the outline view. Then Xcode doesn't complain that it can't find the transformers!

How to get Current Page/View or topmost Page/View in Xamarin.Forms

I want to show AlertDialog(DisplayAlert) in a Xamarin.Forms project when an unhandled error occurs.
How can I get the current page instance?
You need to keep a reference of your main page as you mentioned. There are a few ways to do this.
You can have a static reference to it in your App.cs file, which is actually already there as it inherits from Application which has:
App.Current.MainPage
If you use an MVVM helper like MVVMLight you pass the page to the service so it keeps a reference of it.
ACR UserDialogs is a dialog package that can also help as it extends and adds different types of dialogs
To get around this and have a lot more flexibility add 'ACR User Dialogs' package to each of your platform's projects.
Then you can use that from anywhere :
await UserDialogs.Instance.AlertAsync ("Your question has been successfully sent", "Thankyou");

Resources