No class named AppDelegate is loaded - xamarin

We have a Xamarin.iOS project that shares a code backend with a WPF project that has been going through a refactor. All the shared code libraries have been converted to be .net standard.
The Xamarin project builds, but the simulator throws an exception on startup as such :
Foundation.MonoTouchException has been thrown
Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Unable to instantiate the UIApplication delegate instance. No class named AppDelegate is loaded.
Just for testing purposes, I added an empty UIApplicationDelegate to Main.cs and I'll still get the exception (only now referring to TestDelegate).
Main.cs
using Foundation;
using UIKit;
namespace App.IOS
{
public class Application
{
static void Main (string[] args)
{
UIApplication.Main (args, null, nameof(AppDelegate));
}
}
}
Appdelegate.cs (very large file, so just the declarations here).
namespace App.IOS
{
public class MiniSetup
{
public static readonly MiniSetup Instance = new MiniSetup ();
public void EnsureInit()
{
//Setting up Binding and Dependency Injection Here.
}
}
[Register (nameof(AppDelegate))]
public partial class AppDelegate : UIApplicationDelegate
{
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
//Invoke Minisetup here
}
public override void WillEnterForeground (UIApplication application)
{
//snip
}
public override void DidEnterBackground (UIApplication
application)
{
//snip
}
// snip other code for setting UI controls and other app logic.
}
It seems apparent that I've goofed up the project in some way, but I haven't found any leads as to where to look for a solution. Any ideas on how to troubleshoot this?

Two classed needed, the UIApplicationDelegate subclass and the class that defines the Main entry point that calls UIApplication.Main will the Xamarin.iOS registered name that you used on the UIApplicationDelegate subclass.
Basic/Minimum UIApplicationDelegate:
[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
public override UIWindow Window
{
get;
set;
}
}
Basic/Minimum Main entry point:
public class Application
{
static void Main(string[] args)
{
UIApplication.Main(args, null, "AppDelegate");
}
}

I don't have a smoking gun here, but near as I was able to figure out, an incompatible (with xamarin) reference or nuget package was added to the IOS project. While the native code was throwing a native assembly, I'd imagine that AppDelegate was actually throwing a NotSupportedException or an AssemblyLoader Exception or similar.
So removing all references and nuget packages, then re-adding just the bare minimum seems to have resolved this issue.

Set the Build Action property of AppDelegate.cs to Compile.

Rebuild your solutions then restart your IDE.

Nothing stands out as a problem in the code you have posted.
However, you can try the following :
AppDelegate code is missing a 'Window' property. If you have just not added it to your code snippet in the question for brevity, fine. If not, do add it to your app delegate. Both Xamarin and Apple mention this in their documentation for ApDelegate and Storyboards.
public override UIWindow Window { get; set; }
Make sure the Build action for you AppDelegate is set to Compile and not mistakenly changed to none or any other value. This is highly unlikely, but because you mentioned you're going through a huge refactor, this could have been changed accidentally.
Similar problems were reported with different Xamarin.iOS versions before. So try running you project with a different xamarin version setup.
Although, i presume, you've tried this multiple times before posting the question, i'd mention : try a complete clean and rebuild. (Manually delete the bin' andobj` folders in your iOS project folder, clean the trash, and then do a rebuild just to be sure. Also, try out a full machine restart, remember you're using a Microsoft product!)
I also found this weird line in the same Xamarin documentation, although i am not sure how this can be checked, because the storyboards do not have an owner/AppDelegate reference.
Application's that are launched from a XIB or storyboard use the
UIApplicationDelegate specified in the XIB or storyboard.
Hope either of these steps helps you out. Good luck.

For me it was some missing reference in the iOS project.
Make sure that whatever the Shared and the iOS projects reference at least the same packages.

Related

Importing LibGDX to already existing project [duplicate]

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.

Xamarin Android using Dependency Injection

Im currently working on a DDD layer architecture. I am not sure how to use Dependency Injection on Android project solution. Currently my Android solution holds a reference to class library solution. I have used Unity on my service layer and register 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();
}
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 goes from outer to inner ?
Old question, but for future readers:
Subclassing the application class would do what you are after in regard to registering the container at start up.
[Application]
public class App : Application
{
public static UnityContainer Container { get; set; }
public App( IntPtr javaRef, JniHandleOwnership transfer ) : base( javaRef, transfer )
{
}
public override void OnCreate( )
{
base.OnCreate( );
Container = new UnityContainer( );
Container.RegisterType<ISomeService, ServiceImplementation>( );
}
}
Application Class:
https://developer.xamarin.com/api/type/Android.App.Application/
That's all I can offer at this stage... I'm still in the process of implementing a full Xam.Android solution with Unity & new to dependency injection in general.

How to fix the Veracode Flaw: CWE-489: Leftover Debug Code?

Here in my application I have class testapp in that I have some methods and main method. When I'm using veracode tool its showing flaw at main method saying Veracode CWE-489:Leftover Debug Code. In my psvm main method I have some Syso lines only.
public class testapp {
public static void main(String[] args) {
System.out.println("Test_One");
System.out.println("Test_Two");
System.out.println("Test_Three");
System.out.println("Test_Four");
System.out.println("Test_Five");
}
void dotest() {
}
void runtest() {
}
}
Can anybody guide me how to fix this issue.
The Veracode engine is looking for the presence of the main() function for this vulnerability within the Java code. All you can do in essence is remove it.
The check within Veracode SAST for this issue is controlled by the fact that for a Java Enterprise web application, you shouldn't have a main function as per the spec for a J2EE app.
As long as you don't have any CWE-470 Unsafe Reflection issues in the app that are exploitable from the outside or that the app server is insecurely configured then you shouldn't need to worry about this.

AutoFac for Window Phone 7 - IContainter.Resolve extension method is not found

I'm building a fun application for Windows Phone 7. I'm using MVVM pattern and AutoFac for resolving the dependencies. I have got a classes AutoFacConfiguration holding a property as below
public static IContainer Container { get; private set; }
I try to resolve one of the registered types in some other part of the application like below
AutoFacConfiguration.Container.Resolve<IExpenseRepository>()
But this is not compiling. The compiler says Container does not have Resolve method. I know that IContainer derives from IComponentContext and an extension method with following signature does existing in the AutoFac assembly
public TService Resolve<TService>(this IComponentContext context)
Am I missing something here? I have just referenced the AutoFac.dll in my porject. This is downloaded from autofac site.
Have you added the relevant namespace as a using directive? For example, if the extension method is in class Foo.Bar.Baz, you should have:
using Foo.Bar;
in the source file that's trying to use the extension method.

Testing If a class is being activated using WebActivator and if add a IModelBinder to ModelBinderProviders.BinderProviders.

Dear fellows from Stack Exchange.
I'm trying to test if my Custom Model Binder is being added to the ModelBinderProviders.BinderProviders collection.
I decided to activate this through WebActivator, to avoid messing global.asax,
Everything works fine, but the Test:
I tried using the WebActivator.ActivationManager.Run() method, but my things weren't loaded.
I've something like this in my test:
[TestMethod]
public void TemplateModelBinderProvider_Should_Be_Registered_In_BinderProviders()
{
WebActivator.ActivationManager.Run();
IModelBinderProvider templateModelBinderProvider = ModelBinderProviders.BinderProviders.
Where(x => x is TemplateModelBinderProvider).
FirstOrDefault();
Assert.IsNotNull(templateModelBinderProvider);
}
And this is my app_Start class:
[assembly: WebActivator.PreApplicationStartMethod(typeof(MVC.App_Start.MVCBindings), "Start")]
namespace MVC.App_Start
{
public static class MVCBindings
{
public static void Start()
{
ModelBinderProviders.BinderProviders.Add(new TemplateModelBinderProvider());
}
}
}
Sorry you have problems with the piece of code I wrote.
I don't have access to the source code right now but will take a look in the evening (UK time).
Do you think you could send me your solution so I could replicate it locally? My email is jkonecki at gmail.com
UPDATE
I have received your source code but unfortunately it contains references to libraries I cannot obtain so I cannot compile it.
I have created a separate solution (emailed to you) with MVC3 web app and unit test projects that uses your custom model binder provide. There are two tests that prove that WebActivatorManager.Run method properly registers a custom provider.
Try debugging your unit test to make sure that Run method calls your static Start method.
WebActivator source code is here - you might want to get it and step through.

Resources