MVVM with multiple projects Xamarin - android

Hellow , i am a student and i am working on a project. It is a Xamarin native project (cross-platform). I Use MVVM. This was working perfect when i was using 1 shared project and my 3other projects (android,iOS,winphone). Now I have different projects for my shared code.
-Appname.Repositories
-Appname.Services
-Appname.Viewmodel
Now the problem is that when i start my app using an constructor in my viewmodel with an interface he is given me the next error :
//Constructor
public LoginViewModel(IMvxMessenger messenger, IUserDataService userDataService) : base(messenger)
{
_userDataService = userDataService;
}
Error:
MvvmCross.Platform.Exceptions.MvxException: Failed to construct and initialize ViewModel for type C_Tracker.Mobile.Domain.ViewModel.LoginViewModel from locator MvxDefaultViewModelLocator - check InnerException for more information
InnerException :
MvvmCross.Platform.Exceptions.MvxIoCResolveException: Failed to resolve parameter for parameter userDataService of type IUserDataService
But when i use a constructor without this interface it is working.
public LoginViewModel(IMvxMessenger messenger) : base(messenger)
{
}
Please help me.

Probably you forgot about Dependencies Injection.
Add to Setup.cs something like this Mvx.RegisterType<IUserDataService, UserDataService>();
For more information about ServiceLocator please check https://github.com/MvvmCross/MvvmCross/wiki/service-location-and-inversion-of-control

Related

kotlin- PaymentSessionConfiguration

Hello I am trying to update stripe but when I have to call PaymentSessionConfig my code blocks because the companion of the PaymentSessionConfig class is in private, I cannot modify the class because it is in read only, here is the line :
mPaymentSession = PaymentSession (activity = summaryActivity, config = PaymentSessionConfig)
and the error message I have :
Cannot access 'Companion': it is private in 'PaymentSessionConfig'
Type mismatch.
Required:
PaymentSessionConfig
Found:
PaymentSessionConfig.Companion
Can you share the details of what versions of the SDK you're updating from and to? The migration docs cover a lot of details changes for various versions.
Is it possible you mean to reference some paymentSessionConfig, or what is your config here?
See this example implementation using the PaymentSessionConfig.Builder() (github)

Kotlin not recognized in Android Studio

I am writing an app and just redo it from a youtube video to get back into app writing.
All this Kotlin is new to me as it didn't exist last time I programmed some apps.
When creating the new App I chose Kotlin instead of java.
Now I want to create a Constants class with a companion:
public class Constants {
companion object {
const BASE_URL = "https://whatever.com"
}
}
But too many errors are shown:Shown errors
companion: cannot resolve
object: missing ;
const: unexpected token
BASE_URL: cannot resolve
string: missing ;
Furthermore, I have created a new class which should be a data class, but it doesn't recognize it as well:
data class
I did install the KOTLIN plugins but it didn't help.

Had anyone success with using Room over multi-module Kotlin setup?

Had anyone success with using Room over Android multi-module Kotlin setup.
#Entity
data class School(#Embedded val student: Student)
data class Student(val age: Int = 0)
Whenever I have both above classes in main module everything compiles properly.
But if I move the Student class to another android library module and School in main module. It throws compile time error as:
error: Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
Tried the following constructors but they failed to match:
Student(int) : [arg0 : null]
Note: On debug found this might be name mangling issue. If I change the Student class to data class Student(val arg0: Int = 0) it compiles fine.
Looks like at compile time age exposed as arg0
Any idea how to resolve this issue?
I had the same issue. Entities and Room DAO placed in same module everything ok, putting entities in separate module break compilation. In my case the problem was putting enum declaration in same file as entity. Declaring enum in separate file solved the problem.

[MissingMethodException: No constructor found for Xamarin.Forms.Maps.Android.MapRenderer::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership)]

I have implemented a Google Map in my Xamarin forms app but am getting the above exception when navigating away from the page before the map has completed loading the current location.
This is probably the same issue previously raised but unanswered here.
From my research I believe the issue is related to leaky abstration answer given in this separate question: MonoDroid: Error when calling constructor of custom view - TwoDScrollView
However I do not have enough knowledge of Java or Android development to know how to resolve this issue. I am hoping that someone can explain where and how I can handle this exception when it occurs. Basically what I believe I need to achieve is catching the exception and handle it in the Droid project, but where?
These are the key exception messages that I am getting.
Message: [NotSupportedException: Unable to activate instance of type Xamarin.Forms.Maps.Android.MapRenderer from native handle 0xbef7ad5c (key_handle 0xd4608e7).]
Message: [MissingMethodException: No constructor found for Xamarin.Forms.Maps.Android.MapRenderer::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership)]
Message: [Exception of type 'Java.Interop.JavaLocationException' was thrown.]
However I do not have enough knowledge of Java or Android development to know how to resolve this issue. I am hoping that someone can explain where and how I can handle this exception when it occurs.
I think the reason is when you are navigating the pages, the Dispose method of Xamarin.Forms.Maps.Android.MapRenderer is called before the loading of the rest of the map. ACW need to create a new instance of MapRenderer but failed to create a new one because MapRenderer has no constructor method of (IntPtr, JniHandleOwnership).
If you refer to Premature Dispose() Calls you can find following statement:
If the subclass does contain an (IntPtr, JniHandleOwnership) constructor, then a new instance of the type will be created.
So the workaround for this exception is to create a subclass(Let's say MyMapRenderer) for Xamarin.Forms.Maps.Android.MapRenderer,which has a constructor with two arguments: (IntPtr, JniHandleOwnership), and use MyMapRenderer for map rendering:
In PCL create a custom control for Xamarin.Forms.Map.Map and use MyMap instead in your project:
public class MyMap:Map{}
Create a Custom MapRenderer in Droid project,which has a (IntPtr, JniHandleOwnership) contructor:
[assembly:ExportRenderer(typeof(MyMap),typeof(MyMapRenderer))]
namespace YourNameSpace.Droid
{
public class MyMapRenderer:MapRenderer
{
public MyMapRenderer(IntPtr handle, JniHandleOwnership transfer) { }
}
}
For details about create a custom MapRenderer, please refer to Custom a Map.
Updated version for answer from Elvis. Constuctor should look like as below.
public MyMapRenderer(System.IntPtr handle, JniHandleOwnership transfer): base(Android.App.Application.Context) { }
And additionally most probably you will also an empty constuctor like below. Otherwise first instance creation fails. Also for Proguard it is important.
public MyMapRenderer(Context context) : base(context)
{
}

invalid 'this' to a Java object in Nativescript Plugin

I am trying to develop a nativescript plugin to perform some functionality with the Azure SDK. The docs for the SDK show below:
So I have added the following function to my plugin:
MobileServiceUser.newUser = function (userId, client) {
var userObject = com.microsoft.windowsazure.mobileservices.authentication.MobileServiceUser(userId); // causing the error
client.setCurrentUser(userObject);
};
UserId is a string.
However, the top line throws the error:
JS: Error: Trying to link invalid 'this' to a Java object
I have a full repo showing the minimal implimentation to create this issue on Github.
I would be really grateful of any suggestions how to fix this.
This answer is a little late but I recently ran into this issue myself. Hopefully this answer helps someone in the future!
The error is a bit plain but if you take a look at the code source, you'll see that it's actually telling you that you cannot link a variable to the Object/Class itself as a reference:
MobileServiceUser.newUser = function (userId, client) {
var userObject = com.microsoft.windowsazure.mobileservices.authentication.MobileServiceUser(userId);
};
When you want to instantiate a class, you need to use the keyword new to signify this action. Without new listed before the class constructor call, the program just sees that you're making a direct link between userObject and the class itself. This should fix your problem:
var userObject = new com.microsoft.windowsazure.mobileservices.authentication.MobileServiceUser(userId);
Cheers
~Px

Categories

Resources