Default constructor not found for type MvxPropertyInjector - xamarin

I'm using MvvmCross and Xamarin Support libraries in my project and, after I updated MvvmCross from version 4.2.0 to 4.4.0 and Xamarin Support Packages from version 23.3.0 to 25.1.1, I can't execute the project. The app always crash in SplashScreen with error: Default constructor not found for type MvvmCross.Platform.IoC.MvxPropertyInjector. The error occurs in debug mode and release mode.
My SplashScreen:
[Activity(Icon = "#drawable/ic_launcher",
Theme = "#style/InflorTheme.Splash",
NoHistory = true,
MainLauncher = true,
ScreenOrientation = ScreenOrientation.Portrait)]
public class SplashScreen : MvxSplashScreenActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
}
}

Add the following to your LinkerPleaseInclude file:
public void Include(MvvmCross.Platform.IoC.MvxPropertyInjector injector)
{
injector = new MvvmCross.Platform.IoC.MvxPropertyInjector();
}
You might need other hints for the linker not to not throw away code that is only invoked through Reflection.

Related

JavascriptInterface in Xamarin.Android is obsolete and cant invoke C# from Javascript

few months ago I had a custom Xamarin.Android renderer for a webview based on the sample code in
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/hybridwebview
Javascript code was perfectly invoking my C# code however recently after the latest updates, the WebView control is no longer able to invoke the C# action,
(to be more precise, if I am targeting Android 9.0 (API level 28) or higher)
using API level 27 still works fine
after more investigation, I figured out that the compiler gives a warning on [JavascriptInterface] is being obsolete!
https://learn.microsoft.com/en-us/dotnet/api/android.webkit.javascriptinterface?view=xamarin-android-sdk-9
and they advised to use the (IJavascriptInterface) instead
here is the code to be reviewed
[JavascriptInterface]
[Export ("invokeAction")]
public void InvokeAction (string data)
{
HybridWebViewRenderer hybridRenderer;
if (hybridWebViewRenderer != null && hybridWebViewRenderer.TryGetTarget (out hybridRenderer))
{
hybridRenderer.Element.InvokeAction (data);
}
}
Does anyone know how to implement this properly to fix that and get Javascript to invoke my C# code again.
it still works in my Xamarin.Android project with [JavascriptInterface]
this is part of my sample :
var webview = FindViewById<WebView>(Resource.Id.webView1);
WebSettings settings = webview.Settings;
settings.JavaScriptEnabled = true;
// load the javascript interface method to call the foreground method
webView.AddJavascriptInterface(new MyJSInterface(this), "CSharp");
webview.SetWebViewClient(new WebViewClient());
MyJSInterface class :
class MyJSInterface : Java.Lang.Object
{
Context context;
public MyJSInterface (Context context)
{
this.context = context;
}
[JavascriptInterface]
[Export]
public void ShowToast (string msg)
{
Toast.MakeText(context, msg, ToastLength.Short).Show();
}
}
and in html :
<button type="button" onClick="CSharp.ShowToast ('Call C#')">Call C#</button>
you could refer to https://stackoverflow.com/a/54069075/10768653

Resolution failed exception in Xamarin IOS prism

I have an iOS application written in Xamarin and I am getting a Unity Exceptions Resolution Failed exception when I try and run the application in iOS. However this error does not happen when I run the android version of the application. The exception is being thrown while the initalize function from prism is taking place.
Here is a snippet from my app.xaml.cs
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
this.RegisterLocal(containerRegistry);
this.RegisterServices(containerRegistry);
this.RegisterPagesForNavigation(containerRegistry);
}
This code all executes and passes.
This is the iOS initialization
Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
PullToRefreshLayoutRenderer.Init();
LoadApplication(new App(new IosInitializer()));
return base.FinishedLaunching(app, options);
}
public class IosInitializer : IPlatformInitializer
{
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.Register<IUAirshipUpdate, UAirshipUpdate>();
}
}
This code also executes
The exception being thrown is an argument null exception indicating that IModuleCatelog is not present. I do not understand why it is looking for that module and can not find it. The source code on GitHub indicates that class was registered.
This issue was caused because linker behavior for IOS application was set to full and that causes issues with Unity IOC Container.

MVVMCross 5.3.2 UWP: Where to Get IMvxWindowsFrame for MvxFormsUwpViewPresenter

I'm working out of the Xamarin Forms for MVVMCross 5 Solution Template and updated the packages to the latest version (5.3.2 for MVVMCross). Doing that changes some namespaces around particularly in the UWP project.
It seems that I need to resolve IMvxViewPresenter as MvxFormsUwpViewPresenter which takes a IMvxWindowsFrame as an argument. In the setup file method of Setup.cs there's a XamlControls.Frame rootFrame passed as an argument but I'm not sure if that's suppose to be cast somehow as IMvxWindowsFrame.
Where can you pull the object that implements IMvxWindowsFrame from or is there another way to turn the rootFrame into an IMvxWindowsFrame legitimately.
public class Setup : MvxFormsWindowsSetup
{
private readonly LaunchActivatedEventArgs _launchActivatedEventArgs;
public Setup(XamlControls.Frame rootFrame, LaunchActivatedEventArgs e) : base(rootFrame, e)
{
_launchActivatedEventArgs = e;
// Mvx.RegisterSingleton<IMvxWindowsFrame>(rootFrame);
}
protected override void InitializeFirstChance()
{
base.InitializeFirstChance();
Mvx.RegisterSingleton<Core.Services.ILocalizeService>(new Services.LocalizeService());
Mvx.RegisterSingleton<ISettings>(CrossSettings.Current);
Mvx.RegisterType<IMvxViewPresenter, MvxFormsUwpViewPresenter>();
}
protected override MvxFormsApplication CreateFormsApplication()
{
return new Core.FormsApp();
}
protected override IMvxApplication CreateApp()
{
return new Core.MvxApp();
}
protected override IMvxTrace CreateDebugTrace()
{
return new Core.DebugTrace();
}
}
public sealed partial class MainPage : WindowsPage
{
public MainPage()
{
this.InitializeComponent();
var start = Mvx.Resolve<IMvxAppStart>();
start.Start();
var presenter = Mvx.Resolve<IMvxViewPresenter>() as MvxFormsUwpViewPresenter;
LoadApplication(presenter.FormsApplication);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
}
EDIT: I've been looking more into the class MvxFormsWindowsSetup in the source code at https://github.com/MvvmCross/MvvmCross/blob/develop/MvvmCross-Forms/MvvmCross.Forms.Uwp/Platform/MvxFormsWindowsSetup.cs. It appears that in the method CreateViewPresenter that the IMvxViewPresenter is registered as a singleton with the MvxWrappedFrame already inside but by default the code does not resolve when calling var presenter = Mvx.Resolve() as MvxFormsUwpViewPresenter; in the windows page. Possible bug? Trying to see if I can resolve it myself.
Looks like it fails to resolve even if I put the code right after when Mvx is suppose to register the type / singleton
protected override IMvxWindowsViewPresenter CreateViewPresenter(IMvxWindowsFrame rootFrame)
{
var presenter = new MvxFormsUwpViewPresenter(rootFrame, FormsApplication);
Mvx.RegisterSingleton<IMvxFormsViewPresenter>(presenter);
var presenter2 = Mvx.GetSingleton<IMvxViewPresenter>() as MvxFormsUwpViewPresenter;
return presenter;
}
When updating to MvvmCross 5.3.2 for UWP, the presenter needs to resolve as IMvxFormsViewPresenter rather than IMvxViewPresenter. Change the interface type and it should load properly.
public MainPage()
{
this.InitializeComponent();
var start = Mvx.Resolve<IMvxAppStart>();
start.Start();
var presenter = Mvx.Resolve<IMvxFormsViewPresenter>() as MvxFormsUwpViewPresenter;
LoadApplication(presenter.FormsApplication);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}

Xamarin Form Exception Heaps size and OutOfMemoryError

How can I enable large heap in my Xamarin.Forms application ?
Here is MainActivity.cs Android code:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
RoundedBoxViewRenderer.Init();
LoadApplication(new App());
}
}
See Exception Screenshot below:
Go to project Options > Android Build > General > Enable MultiDex & Options > Android Build > Advacnced > JavaHeapSize (Set 3G) , In manifest file you can add android:largeHeap="true" in Application Tag
<application android:largeHeap="true"/>
The Xamarin way of setting the Dalvik/Art large heap via the Appication attribute:
Application.cs
using System;
using Android.App;
using Android.Runtime;
namespace LargeHeap
{
[Application(LargeHeap = true)]
public class XamarinApplication : Application
{
public XamarinApplication(IntPtr handle, JniHandleOwnership ownerShip) : base(handle, ownerShip)
{
}
}
}
If true, then the process should be created with a large Dalvik heap; otherwise, the process will be created with the default Dalvik heap.
Ref: https://developer.xamarin.com/api/property/Android.App.ApplicationAttribute.LargeHeap/
My contribution, it worked for me:
[assembly: Application(LargeHeap = true)]
namespace Project.Droid
{
[Activity(.............)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
}
}

How to use Prism with Spring.net

I am currently trying to complete this tutorial to get Prism to work with Spring.net.
After referencing Prism4 and Spring.Net through NuGet (or manualy referencing the assemblies), setting up a bootstrapper and running the application I get a "File or Assembly "System, Version=1.0.3300.0, Culture=neutral, ..." FileNotFoundException.
I am succesfully using Prism and Spring.Net in seperate projects. Above exception only occours in a project where Prism AND Spring.net is referenced. Spring.net is not even used in code or app.config. Searching various sites I could not find any informations on version issues or similar problems.
namespace PrismSpringSandbox {
/// <summary>
/// Interaktionslogik für "App.xaml"
/// </summary>
public partial class App : Application {
protected override void OnStartup(StartupEventArgs e) {
base.OnStartup(e);
Bootstrapper bootstrapper = new Bootstrapper();
bootstrapper.Run();
}
}
}
The Exception occours on "bootstrapper.Run()".
namespace HelloWorld {
public class Bootstrapper : UnityBootstrapper {
protected override DependencyObject CreateShell() {
Shell1 shell = new Shell1();
shell.Activate();
RegionManager.UpdateRegions();
shell.Show();
return shell;
}
protected override IModuleCatalog CreateModuleCatalog() {
DirectoryModuleCatalog catalog = new DirectoryModuleCatalog{ModulePath = #".\"};
//ModuleCatalog catalog =
// new ModuleCatalog().AddModule(typeof(HelloWorldModule.HelloWorldModule)).AddModule(
// typeof(SecondaryModule.SecondaryModule));
return catalog;
}
}
}
Maybe someone knows a solution for this problem when trying to use current Prism with current Spring.Net versions.
Ok, got it!
Problem was a reference to unity while referncing spring.net at the same time.

Resources