I'm trying to get access to the device lockscreen in a xamarin forms pcl project. I have heard about third-party components such as Lockscreen and Passcode but i dont know how to go about it since this is a xamarin forms app not xamarin.android. How would i implement let's say Lockscreen in the android project of my xamarin forms application?
One way to call Locker.Activate() within Xamarin Forms is to use a dependency service. In your Xamarin.Forms Project, you can add the following code to make the call to the dependency service:
switch (Device.RuntimePlatform)
{
case Device.Android:
DependencyService.Get<IAndroidMethods>().activateLock();
break;
case Device.iOS:
DependencyService.Get<IAppleMethods>().activateLock();
break;
}
Then in your Xamarin.Forms project, create two interfaces: IAppleMethods and IAndroidMethods that contain the activateLock() method.
I'll show the implementation for IAndroidMethods:
public interface IAndroidMethods
{
void activateLock();
}
Then, in your iOS and Android projects, create classes that implement the interfaces you just created. This would be your android class:
//add appropriate using declarations
[assembly: Xamarin.Forms.Dependency(typeof(AndroidMethods))]
namespace <YourApp>.Droid
{
public class AndroidMethods : IAndroidMethods
{
public void activateLock()
{
Locker.Activate(Xamarin.Forms.Forms.Context); //Not sure about any other parameters you need to pass in
}
}
}
Related
I have provided an aplication made in android that has a navigation drawer and in it has a list of games. I have to create another game and to put it there. The game that has to be created by my must use libGDX but the original application didn't use this library.
Is it possible to do this ? If Yes, how can I add the libgdx to the exiting project. On github I found only how to start a new project with libGDx, an not how to add it to exising code. thanks.
You can achieve this with these 2 steps:
1. Add LibGDX to your project
On your android gradle file: build.gradle (app or android module)
dependencies {
...
// Add this line, it is recommended to use the latest LibGDX version
api "com.badlogicgames.gdx:gdx-backend-android:1.9.10"
}
2. Initialize LibGDX for a view or use a LibGDX managed activity
Option 1: Initialize for a view
Since you have a navigation drawer and more code native to android this option fits your needs better. From this question (How to add LibGDX as a sub view in android):
The AndroidApplication class (which extends activity) has a method named initializeForView(ApplicationListener, AndroidApplicationConfiguration) that will return a View you can add to your layout.
-- Matsemann
Also here's documentation on how to implement this (Fragment based LibGDX).
Option 2: Use a managed activity
This is the default/most common use of LibGDX, you will need to change your code as follows:
Your activity needs to extend Android application:
public class MainActivity extends AndroidApplication {
Create a class extending Game or ApplicationAdapter:
import com.badlogic.gdx.Game;
public class LibGDXGame extends Game {
#Override
public void create() {
System.out.println("Success!");
}
}
Initialize the LibGDX managed activity:
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
public class MainActivity extends AndroidApplication {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new LibGDXGame(), config);
}
}
Generally what you want is possible, I reckon your best bet would be to create a new libgdx project with their GUI and to then manually merge the files that are needed.
I created a Xamarin.Forms project/library. The project/library contains pages, views, renderers and other shareable components.
I then created my main project that will reference the shared project. I added each project (shared, android, ios) as an existing solution project.
The problem is when I try to use ShareProject's custom renderer in my MainShareProject, the implementation in SharedProjectAndroid and SharedProjectiOS is not called. When I call ISomeService in MainShareProject via DependencyService it is null
Solution Structure:
Shared Project Structure (Purpose of SharedProject is to reuse it in other projects)
ShareProject (NET standard library)
custom renderer
ISomeService
SharedProjectAndroid (Reference ShareProject; Xamarin.Android library)
custom renderer implementation
ISomeServiceImpl
ShareProjectiOS (Reference ShareProject; Xamarin.iOS library)
custom renderer implementation
ISomeServiceImpl
Main Project Structure
MainShareProject (Reference ShareProject)
use custom renderer (calls custom renderer constructor in ShareProject but not its implementation
use ISomeService by DependencyService (returns null)
MainProjectAndroid (Reference ShareProject and ShareProjectAndroid)
MainProjectiOS (Reference ShareProject and ShareProjectAndroid)
var someService = DependencyService.Get<SharedProject.ISomeService>(); //returs null
Android
[assembly: Dependency(typeof(SharedProjectiAndroid.SomeService))]
{
public class UserPreference : ISomeService
}
I was fighting with the same problem that custom renderer from external assemblies are not called. I never investigated deeper, but it seems that it is somehow related to the code optimization.
For me the solution was to add an explicit call from my project to the external assembly.
Like this:
Assembly
public static class CoreUI
{
public static void Init() { }
}
Project
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
CoreUI.Init(); // Initialisation of CoreUI
}
I want to use my custom renderer inside a PCL. Is it possible? Or can I initialize my custom renderer inside this PCL?
No and No.
What you use in PCL is - let's say - component and it's abstraction. The 'materialization' (or not) of the component will be made by custom renders on each platform.
I can't see a reason to use it on a platform-independent implementation once it can be shown (or to behaves) differently on each one.
Custom Renderers let developers override this process to customize the appearance and behavior of Xamarin.Forms controls on each platform.
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/
Maybe with a real case, we can suggest another solution.
Finally, I've found a solution.
I've just created a class in my PCL and used it in XAML, let's say:
public class MyHelperEntry : Entry { public MyHelperEntry() { } }
that inherits Entry class. And in an App, where I use this PCL, I've created a class, that inherits MyHelperEntry:
public CustomHelperEntry : MyHelperEntry { public CustomHelperEntry() { } }
and used this CustomHelperEntry as a custom renderer.
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
I am not sure how to use Dependency Injection on Xamarin Android project solution. Currently my Android solution holds a reference to another class library solution. I have used Unity on my service layer and registered the container via WebApiConfig.cs.
My question is, how do i go about using Unity on Android side in order to run on start up, would be grateful if code was included. I dont want to new-up the container through main activity of Android. I want the container to register behind the process i.e. AppStart or Global asax where it does it for you for MVC apps. Is there a way to do it for Android? Also I noticed on Main Activity I am unable to create constructor. I guess this isnt possible but how do I go about holding object reference to my Class Library solution ? example that i attempted to do:
private IExample _ex;
MainActivity(IExample ex){
_ex = ex; //depedency Injection rather than newing it up
}
public void DoSomething(){
_ex.HelloWorld();
}
Is there a way to do it via Attribute ? Also for each of my layer do I need to install and create container in order to resolve current solution dependency ? or can I use container from android which would resolve all dependency in each layer as DDD architecture goes from outer to inner ?
In terms of setting up DI at startup you can create a custom Application implementation like so:
// Must include this attribute so that Android knows we want to use this as our Application implementation
[Application(Icon = "#drawable/Icon", Label = "#string/ApplicationName")]
public class MyApplication : Application
{
public override void OnCreate()
{
base.OnCreate();
// Do your DI initialization/registration here
}
}
I'm not sure exactly what you mean by not being able to create a constructor on the main activity. You can create constructors for any activity you feel like. You don't always see it though because people tend to put their initialization logic in OnCreate.