I have service class implementation in Example.aar
public class DemoService : Service{}
I have created a Native binding library project.
When i tried to bind this service in Xamarin.Android project. bindService() is returning false.
var Result =context.getApplicationContext.bindService(new Intent(context,typeof(DemoService)), new ServiceConnection(),Bind.AutoCreate); //Result is false.
How to bind a service created in 3rd party library project in Xamarin Android project.??
You can refer to this article Binding a Java Library.
In the Microsoft doc, first you can get the class from the .aar file.
var ClassName = new Com.MyCompany.MyProject.MyclassName();
Then use the bindService or other methods to use the Class.
var Result =context.getApplicationContext.bindService(new Intent(context,typeof(ClassName)), new ServiceConnection(),Bind.AutoCreate);
Related
I am Trying to call a web service in Xamarin Android Project. But when adding service reference in my project and trying to create reference of that web service it says 'HelloService' is a namespace but is used like a type.
MainActivity.cs
Solution Structure:
This is not an issue with xamarin.android, it's an issue with naming, i assume you have something like this.
namespace HelloService
{
class HelloService
{
}
Don't use same name for classes and namespace, more detail here
In Xamarin forms app, How can we invoke Shared code Method from Platform specific Dependency class.
I need to call one method implemented in my ContentPage class from my iOS dependency class.
Thanks...
There are different solutions to this:
Use a static method to call it where ever you need to.
Use the messaging center to send a message to your Shared/PCL project and do what ever you need. (link: https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/messaging-center/)
If this is a Custom renderer you can use binded command properties and Execute those in your platforms specific code.
In my case, what I did was having a static class called Helper, that contains all the static methods that I need to call on all platforms/projects.
Hope this helps.
When creating prism pages in Xamarin Forms app using the prism template pack I get the following error.
The parameter is incorrect.(Exception from HRESULT: 0x80070057
(E_INVALIDARG)
I am using VS2015, Prism Template Pack 1.7 and here's my project structure
My mistake was trying to create 'Prism' pages before modifying the app class to inherit from 'PrismApplication'.
I have just noticed that when using the template pack it automatically registers page for navigation within the 'RegisterTypes' method in the App class.
Adding to Muhammad's answer above, i also had to change the constructor to the following after inheriting from PrismApplication
public App(IPlatformInitializer initializer = null) : base(initializer)
{
}
And also had to change the Application to PrismApplication in App.xaml file also.
I'm trying to create a simple wrapper to Application Insights Android SDK
I build the library in Android Studio which resulted in this debug AAR file which I then added to a library binding project in Xamarin Studio.
However, when I reference it I have the namespace:
using Com.Microsoft.Applicationinsights.Library;
But it doesn't contain class ApplicationInsights which it should if it would match the java library it's wrapping.
I don't want a fancy complete CLR wrapper, I just want to call the "setup" method on that class which takes the app's Context as an argument, then be able to call simple tracking methods.
Create a new Java Binding library, add the AAR file then you can do things like:
_appInsights = JNIEnv.FindClass ("com/microsoft/applicationinsights/library/ApplicationInsights");
_methodSetup = JNIEnv.GetStaticMethodID(_appInsights, "setup",
"(Landroid/content/Context;Landroid/app/Application;Ljava/lang/String;)V");
Call setup on it:
public static void Setup(Android.Content.Context context, Android.App.Application app, String key) {
var c = new JValue(context);
var a = new JValue(app);
var s = new JValue(new Java.Lang.String(key));
JNIEnv.CallStaticVoidMethod (_appInsights, _methodSetup, c, a, s);
}
I added ASP.NET Web API RC to my MVC3 project using NuGet:
Install-Package AspNetWebApi
Then I configured it. In Global.asax.cs:
// configure regular controllers
var configuration = GlobalConfiguration.Configuration;
var container = Bootstrapper.ConfigureContainer(configuration);
containterProvider = new ContainerProvider(container);
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
// Set the dependency resolver implementation for Web API.
var resolver = new AutofacWebApiDependencyResolver(container);
configuration.DependencyResolver = resolver;
And in Boostrapper.ConfigureContainer(...) I added:
// I register my types as InstancePerLifetimeScope()
// but I also tried .InstancePerHttpRequest().InstancePerApiRequest()
// to the same end
builder.RegisterType<SomeService>()
.AsImplementedInterfaces().InstancePerLifetimeScope();
// Register API controllers using assembly scanning.
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterWebApiFilterProvider(config);
This is described here and here.
I also updated Autofac, Autofac.Web, Autofac.Mvc3 packages using NuGet and installed Autofac.WebApi package.
With this configuration I tried running my ApiController and got the following error:
No scope with a Tag matching 'httpRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being reqested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.
Then as I read this comment from Alex Meyer-Gleaves:
I suspect you are using the Web API package with MVC3 and this is causing the problem. In the MVC3 integration the tag for the InstancePerHttpRequest lifetime scope was "httpRequest". It was in the MVC4 Beta and Web API Beta packages that I changed both InstancePerHttpRequest and InstancePerApiRequest to use the common tag "AutofacWebRequest". You can grab the MVC4 integration package from NuGet using Autofac.Mvc4.
source article with comment
So following the advice from Alex I got the package Autofac.Mvc4 but it works only with Mvc4 and my project wouldn't build. I then grabbed the source code of Autofac to build Autofac.Mvc4 against Mvc3:
hg clone https://code.google.com/p/autofac/ --branch "MVC4 beta" C:\my\path
After using this assembly as my reference ApiController started working but regular Controllers worked ok only for a single controller action call. When the view called Html.RenderAction(...) and when I refresh or navigate to another controller action it crashes with this error:
No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being reqested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.
I thought building from the newest source for Autofac.Mvc4 version 2.6.2.859 against Mvc3 could help but I can't find the source for that. Or maybe there's something else wrong in here?
I found the problem. I also used Autofac.Integration.Web to inject dependencies into custom Membership and Role providers. But in WebLiftime.cs there was this line:
public static readonly object Request = "httpRequest";
Once I changed it to:
public static readonly object Request = "AutofacWebRequest";
and used the built assembly everything works fine and I get no errors :)
I believe this constant value should the same as in all projects Autofac.Integration.Web, Autofac.Integration.Mvc and 'Autofac.Integration.WebApi for Mvc4 so this supposedly is a bug.