Trying to integrate facebook with libgdx, where to implement this code because there is no "module:app" is present? - libgdx

According to facebook:-
"Add the following to the dependencies {} section of your build.gradle (module: app) file to compile the latest version of the Facebook SDK:
implementation 'com.facebook.android:facebook-android-sdk:[4,5)'"
I don't see any module named app in the android studio project.where to add above line?

module:app in this case just means the main android app. So you should add the dependency to the android module.
Then I bet your next question how to use this library from the core since the Android module depends on the core module and not vice versa so you do not have access to the library in the core project. One way is to pass a contract to the platform launchers where each implements it differently.
//Simple contract
public interface IPlatformContract {
void runThis();
}
// Core project (MyGame)
private IPlatformContract platformContract;
public MyGame(IPlatformContract platformContract) {
this.platformContract = platformContract;
}
//DesktopLauncher
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
// Launch desktop with it's own implementation of the contract.
new LwjglApplication(new MyGame(new IPlatformContract() {
#Override
public void runThis() {
System.out.println(" I run on desktop!");
}
}), config);
}
//AndroidLauncher, different way. Here the class itself implements the contract.
public class AndroidLauncher extends AndroidApplication implements IPlatformContract{
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new LibgdxTestEnvironment(this), config);
}
#Override
public void runThis() {
System.out.println("I run on android!");
}
}
You can pass along the contract to other classes like screens in your core project so you have access to it. You can even make a Singleton.

Related

How do I configure Web API .Net core to accept Binary JSON?

In Web API 2.1 I register BSON with the following:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Formatters.Add(new BsonMediaTypeFormatter());
}
}
I'm not sure how to register the formatter .Net Core Web API. It seems certain that it will be in the Startup.Configuration, but I'm still pretty new to Core and not sure how to add this type formatter.
It looks like in the startup.cs, the mvc config should look something like this.
using WebApiContrib.Core.Formatter.Bson;
namespace MyApp
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddBsonSerializerFormatters();
}
}
}
For anyone using .NET Core and using the minimal services for Mvc with AddMvcCore(), AddBsonSerializerFormatters() doesn't show up. I checked the project on GitHub (github.com/WebApiContrib/WebAPIContrib.Core). There is only an extension written for IMvcBuilder and none for IMvcCoreBuilder. As a workaround I wrote my own extension method. Hope it helps someone.
BuilderExtensions.cs:
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using System;
using WebApiContrib.Core.Formatter.Bson;
namespace ApiGateway.Extensions
{
public static class BuilderExtensions
{
public static IMvcCoreBuilder AddBsonSerializerFormatters(this IMvcCoreBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Services.TryAddEnumerable(
ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, MvcBsonSerializerOptionsSetup>());
return builder;
}
}
}
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
// ...
// example usage
services.AddMvcCore()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddJsonFormatters().AddBsonSerializerFormatters()
.AddApiExplorer();
// ...
}

MvvmCross Custom IoC Integration breaks Plugin Registration

I'm trying to integrate TinyIoc with MvvmCross. I followed the instructions from
https://github.com/MvvmCross/MvvmCross/wiki/Customising-using-App-and-Setup#changing-the-ioc-container-that-mvvmcross-uses
and created an adapter
public class TinyIoCMvxIoCAdapter : MvxSingleton<IMvxIoCProvider>, IMvxIoCProvider
{
...
}
that implements all the methods and forwards it to the TinyIoC container. That was very straight forwared and I only had to implement some additional code to fire the callbacks when something gets subscribed for
void CallbackWhenRegistered<T>(Action action)
void CallbackWhenRegistered(Type type, Action action)
I changed Setup.cs
protected override IMvxIoCProvider CreateIocProvider()
{
var provider = TinyIoCAdapterSetup.CreateIocProvider();
return provider;
}
with
public class TinyIoCAdapterSetup
{
public static IMvxIoCProvider CreateIocProvider()
{
var container = TinyIoCContainer.Current;
container.AutoRegister(t => t == typeof(IMvxViewModel));
return new TinyIoCMvxIoCAdapter(container);
}
}
That all works great. I can see that register is called on TinyIoc and things are getting resovled as well.
What does not work are the plugins. We are using the Messenger plugin and with the TinyIoC integration, the IMvxMessenger cannot be resolved when a ViewModel is resolved that gets the IMvxMessenger ctor injected. I can see that the MessengerPluginBootstrap is created by Mvx but I couldn't see that a call was made to register IMvxMessenger.
Does anybody know what I'm doing wrong?
Each plugin has a PluginLoader class that the Bootstrapper calls to register the plugin in the IoC container.
It looks something like this:
public class PluginLoader
: IMvxPluginLoader
{
public static readonly PluginLoader Instance = new PluginLoader();
private bool _loaded;
public void EnsureLoaded()
{
if (_loaded)
{
return;
}
Mvx.RegisterSingleton<IMvxMessenger>(new MvxMessengerHub());
_loaded = true;
}
}
Without seeing your IoC adapter, it's difficult to say what the issue is. Try manually registering the plugin to see if the IoC container is working properly.

Calling helper function inside android application project from a library project

I have an android application project. I have created a library project and added reference in the application project. Now I need to call/access certain functions/class/methods that is there in the application project from the library project. How can I do that ?
Create an interface in the library that defines the functions you would like the library to call. Have the application implement the interface and then register the implementing object with the library. Then the library can call the application through that object.
In the library, declare the interface and add the registration function:
public class MyLibrary {
public interface AppInterface {
public void myFunction();
}
static AppInterface myapp = null;
static void registerApp(AppInterface appinterface) {
myapp = appinterface;
}
}
Then in your application:
public class MyApplication implements MyLibrary.AppInterface {
public void myFunction() {
// the library will be able to call this function
}
MyApplication() {
MyLibrary.registerApp(this);
}
}
You library can now call the app through the AppInterface object:
// in some library function
if (myapp != null) myapp.myFunction();
You can just create the object of that particular class and then you can directly call that method or variable.
class A{
public void methodA(){
new B().methodB();
//or
B.methodB1();
}
}
class B{
//instance method
public void methodB(){
}
//static method
public static void methodB1(){
}
}
Don't forget to import the necessary packages.

Why is my app crashing in System.Windows.ni.dll on startup with no useful stack trace? (seemingly after adopting Caliburn.Micro)

Start with the default "empty" Windows Phone App project (Windows Phone 8). Let's call it BlackAdder for fun.
Add Caliburn.Micro from Nuget
Move MainPage.xaml to the Views subdirectory and create a matching ViewModel in ViewModels\MainPageViewModel.cs
Fix the namespace for MainPage to Blackadder.Views in both the .xaml and .xaml.cs files
Update App.xaml
<Application
x:Class="BlackAdder.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:local="clr-namespace:BlackAdder">
<Application.Resources>
<local:Bootstrapper x:Key="bootstrapper"/>
</Application.Resources>
</Application>
Update App.xaml.cs
public partial class App : Application
{
public static PhoneApplicationFrame RootFrame { get; private set; }
public App()
{
InitializeComponent();
// Show graphics profiling information while debugging.
if (Debugger.IsAttached)
{
...
}
}
}
Create Bootstrapper.cs
public class Bootstrapper : PhoneBootstrapper
{
private PhoneContainer container;
protected override void Configure()
{
container = new PhoneContainer();
container.RegisterPhoneServices(RootFrame);
container.PerRequest<MainPageViewModel>();
AddCustomConventions();
}
protected static void AddCustomConventions()
{
}
protected override object GetInstance(Type service, string key)
{
return container.GetInstance(service, key);
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return container.GetAllInstances(service);
}
protected override void BuildUp(object instance)
{
container.BuildUp(instance);
}
}
When we run the app, it crashes in a very weird place.
An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in System.Windows.ni.dll
Additional information: Exception has been thrown by the target of an invocation.
There is no inner exception, and the stack trace is (seemingly) useless.
System.Windows.ni.dll!System.Windows.Threading.DispatcherOperation.Invoke()
System.Windows.ni.dll!System.Windows.Threading.Dispatcher.Dispatch(System.Windows.Threading.DispatcherPriority priority)
System.Windows.ni.dll!System.Windows.Threading.Dispatcher.OnInvoke(object context)
System.Windows.ni.dll!System.Windows.Hosting.CallbackCookie.Invoke(object[] args)
System.Windows.RuntimeHost.ni.dll!System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(System.IntPtr pHandle, int nParamCount, System.Windows.Hosting.NativeMethods.ScriptParam* pParams, System.Windows.Hosting.NativeMethods.ScriptParam* pResult)
It turns out this has nothing to do with caliburn.micro at all. The problem lies in Properties\WMAppManifest.xaml.
After moving MainPage.xaml to the Views subdirectory, the Navigation Page setting (found on the first tab, Application UI) needs to be updated to Views\MainPage.xaml

Using MvvmCross from content providers and activities

I am trying to use MvvmCross v3 in one of my applications which consists of activities, content providers and broadcast receivers. However, I am not quite succeeding.
The application consists of a Core PCL which contains logic, models and viewmodels and a Droid application which contains all MonoDroid-specific stuff.
In Core I have an App:MvxApplication class and in Droid I have a Setup:MvxSetup class which creates an App-instance and initialises stuff.
I can use the IOC parts with content providers, broadcast receivers and non-Mvx-activities without problems. When I now want to add an MvxActivity it falls apart.
When the Mvx Activity launches I get an exception "Cirrious.CrossCore.Exceptions.MvxException: MvxTrace already initialized".
Obviously I am initialising things in the wrong order / wrong place. But, I need a pointer in the right direction.
My App Class
public class App
: MvxApplication
{
public override void Initialize()
{
base.Initialize();
InitialisePlugins();
InitaliseServices();
InitialiseStartNavigation();
}
private void InitaliseServices()
{
CreatableTypes().EndingWith("Service").AsInterfaces().RegisterAsLazySingleton();
}
private void InitialiseStartNavigation()
{
}
private void InitialisePlugins()
{
// initialise any plugins where are required at app startup
// e.g. Cirrious.MvvmCross.Plugins.Visibility.PluginLoader.Instance.EnsureLoaded();
}
}
And my setup class
public class Setup
: MvxAndroidSetup
{
public Setup(Context applicationContext)
: base(applicationContext)
{
}
protected override IMvxApplication CreateApp()
{
return new App();
}
protected override IMvxNavigationSerializer CreateNavigationSerializer()
{
return new MvxJsonNavigationSerializer();
}
public override void LoadPlugins(Cirrious.CrossCore.Plugins.IMvxPluginManager pluginManager)
{
pluginManager.EnsurePluginLoaded<Cirrious.MvvmCross.Plugins.Json.PluginLoader>();
base.LoadPlugins(pluginManager);
}
public void RegisterServices()
{
// I register a bunch of singletons here
}
// The following is called from my content provider's OnCreate()
// Which is the first code that is run
public static void DoSetup(Context applicationContext)
{
var setup = new Setup(applicationContext);
setup.Initialize();
setup.RegisterServices();
}
My Content provider's OnCreate():
public override bool OnCreate()
{
Log.Debug(Tag, "OnCreate");
_context = Context;
Setup.DoSetup(_context);
return true;
}
My MvxActivity:
[Activity(Label = "#string/ApplicationName", MainLauncher = true)]
[IntentFilter(new[] { "Settings" })]
public class SettingsView
: MvxActivity
{
public new SettingsViewModel ViewModel
{
get { return (SettingsViewModel) base.ViewModel; }
set { base.ViewModel = value; }
}
protected override void OnViewModelSet()
{
SetContentView(Resource.Layout.Page_SettingsView);
}
}
Short answer (I'm in an airport on mobile)
all the mvx android views will check the setup singleton has been created - https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross.Droid/Platform/MvxAndroidSetupSingleton.cs (vnext tree - but similar on v3)
so if you are creating a setup, but not setting this singleton, then you will get a second setup created when you first show a view
i suspect you can just get your setup created via the singleton class, but if this isn't flexible enough for your needs, then please log an issue on github
would also love to see some blogging about this - I've not used custom content providers much (at all!)