Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidCastException when setting LicenseKey #19

Closed
harry-gordon opened this issue Jul 12, 2019 · 11 comments
Milestone

Comments

@harry-gordon
Copy link

@harry-gordon harry-gordon commented Jul 12, 2019

I'm trying to get started with MR.Gestures in our Xamarin.Forms/MvvmCross project but I'm getting an exception from inside MR.Gestures when trying to set the licence key in MainActivity as per the Getting Started instructions:

protected override void OnCreate(Bundle bundle)
{
    ...
    base.OnCreate(bundle); // MVVM sets up Xamarin.Forms here
    MR.Gestures.Android.Settings.LicenseKey = "<licence key>";
    ...
}

The exception trace:

07-12 11:35:04.259 I/MonoDroid(10865): UNHANDLED EXCEPTION:
07-12 11:35:04.298 I/MonoDroid(10865): System.InvalidCastException: Specified cast is not valid.
07-12 11:35:04.298 I/MonoDroid(10865):   at MR.Gestures.Android.Settings.get_AppName () [0x00005] in <c63931d951104a8ba883711522cfb229>:0 
07-12 11:35:04.298 I/MonoDroid(10865):   at MR.Gestures.Android.Settings.set_LicenseKey (System.String value) [0x0000a] in <c63931d951104a8ba883711522cfb229>:0 
07-12 11:35:04.298 I/MonoDroid(10865):   at TestApp.Android.MainActivity.OnCreate

I've tried moving where we set the licence key around (including 1. finding a hook in MVVM Cross to set it between XF init and LoadApplication and 2. much later just before the component is needed) but I've had the same results.

Any help would be appreciated! Let me know if you have any questions.

For reference we are on XF version 3.6.0 and MvvmCross version 6.2.3.

@MichaelRumpler

This comment has been minimized.

Copy link
Owner

@MichaelRumpler MichaelRumpler commented Jul 12, 2019

Did you set the app name like it is described in https://www.mrgestures.com/#HowToConfigureAppName?

The LicenseKey must be set after Forms.Init(...).
I read the Context.Title (((global::Android.App.Activity)Xamarin.Forms.Forms.Context).Title). If that is not properly set/initialized when you set the LicenseKey, then it will fail.

@harry-gordon

This comment has been minimized.

Copy link
Author

@harry-gordon harry-gordon commented Jul 12, 2019

Thanks for the quick reply Michael. I double checked, we are setting the name as required.

I had a look at ((global::Android.App.Activity)Xamarin.Forms.Forms.Context).Title and that results in an invalid cast. It looks like Xamarin.Forms.Forms.Context is of type Android.App.Application not Android.App.Activity.

I wonder if this is a recent change in Xamarin Forms?

@MichaelRumpler

This comment has been minimized.

Copy link
Owner

@MichaelRumpler MichaelRumpler commented Jul 12, 2019

It still worked for others with XF 3.6.
Can you make a repro?

@harry-gordon

This comment has been minimized.

Copy link
Author

@harry-gordon harry-gordon commented Jul 12, 2019

I made a repo to demonstrate here: https://github.com/harry-gordon/mvvm-cross-mr-gestures

It's based on an MvvmCross template I found so the main activity is called FormsApplicationActivity.

@harry-gordon

This comment has been minimized.

Copy link
Author

@harry-gordon harry-gordon commented Jul 15, 2019

Any thoughts on this @MichaelRumpler? I thought it might be fixed in a later version of MvvmCross (after reading this MvvmCross/MvvmCross#2832) but I get the same result in 6.3.1.

@harry-gordon

This comment has been minimized.

Copy link
Author

@harry-gordon harry-gordon commented Jul 15, 2019

OK, I've got a work-around on my end, it's an issue with MvvmCross not being able to retrieve the current activity when it initializes Xamarin Forms so instead sets the context to the application. I've found a way to override that logic and pass in the correct activity, I'll raise this with MvvmCross as I doubt this is desired behavior.

@MichaelRumpler

This comment has been minimized.

Copy link
Owner

@MichaelRumpler MichaelRumpler commented Jul 15, 2019

They closed the issue you linked because the workaround seemed to work, not because they fixed the bug - not what I would expect.

I couldn't find a reliable way to read the app name without the Xamarin.Forms.Forms.Context. I'll add two more ways to get the app name to the next version. Maybe this will at least help some people.

@harry-gordon

This comment has been minimized.

Copy link
Author

@harry-gordon harry-gordon commented Jul 15, 2019

Sounds good! Thanks for your help on this 🚀

@agat366

This comment has been minimized.

Copy link

@agat366 agat366 commented Sep 8, 2019

@harry-gordon could you actually describe the workarround? It happened I've got same type project: XF + MvvmCross.

@MichaelRumpler

This comment has been minimized.

Copy link
Owner

@MichaelRumpler MichaelRumpler commented Sep 9, 2019

@MichaelRumpler MichaelRumpler added this to the 2.1.1 milestone Nov 18, 2019
@MichaelRumpler

This comment has been minimized.

Copy link
Owner

@MichaelRumpler MichaelRumpler commented Nov 19, 2019

Version 2.1.1 is now available on NuGet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.