Cast Application to CustomApplication(System.InvalidCastException)

Hello,

In one of my activities I want to get my CustomApplication.
But if I trie the following i get an error(System.InvalidCastException: Cannot cast from source type to destination type.):

((CustomApplication)Application).DoSomething();

I'm using Monodroid 4.2.8.
I think this has worked in the past.
Is this a Bug or has something changed?

Thank you very much in advance for your answer.

Robin

Posts

  • TeHaTeHa DEMember ✭✭✭

    I think you should say something more. I don't really know what you want to do? BUT:
    Where does the Application-object come from? If you don't instantiate it by your self*) you can't cast it to your CustomApplication, because you can't cast a base-class to a derived-class! So it's definitely no bug.

    *) means: CustomApplication c = new CustomApplication();

  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    I'm using Monodroid 4.2.8. I think this has worked in the past. Is this a Bug or has something changed?

    Yes, this is a bug: https://bugzilla.xamarin.com/show_bug.cgi?id=7934

    The workaround is to set the ApplicationAttribute.Name property:

    [Application(Name="my/App")]
    public class MyApp : Application {
        public MyApp (IntPtr handle, JniHandleOwnership transfer)
            : base (handle, transfer)
        {
        }
        // ...
    }
    
  • br3zzerbr3zzer DEMember

    Thank you it works.

  • MihaMarkicMihaMarkic SI ✭✭✭✭

    Jonathan, how exactly is Name supposed to be? I've tried several variations but always get "application has stopped unexpectedly" followed by "assembly installation failed: The assembly directory could not be created."

  • Hi,

    I rolled back to Mono For Android 4.2.5. Go to https://store.xamarin.com/, sign in, and download the Android from there. This error only happened with 4.2.8.

  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    @MihaMarkic: My bad; it needs to be a Java name, not a JNI name:

    [Application(Name="my.App")]
    public class MyApp : Application {
        public MyApp (IntPtr handle, JniHandleOwnership transfer)
            : base (handle, transfer)
        {
        }
        // ...
    }
    

    I wish I could edit original answers after an hour...

  • MihaMarkicMihaMarkic SI ✭✭✭✭

    That works :-)

Sign In or Register to comment.