MvvmCross从5.7升级到6.0.0时创建Setup实例失败求助
I’ve seen this exact issue pop up a lot with MvvmCross 6.0 upgrades—this version made breaking changes to Android’s setup and launch flow, which is almost certainly what’s tripping you up. Let’s walk through the fixes step by step:
1. Update your Setup class inheritance
MvvmCross 6.0 moved to using AppCompat-based setup by default. Your old MvxAndroidSetup won’t work anymore—you need to switch to MvxAppCompatSetup:
Old (broken) Setup code:
public class Setup : MvxAndroidSetup<App> { // ... your old implementation }
New (fixed) Setup code:
using MvvmCross.Droid.Support.V7.AppCompat; using Android.Content; public class Setup : MvxAppCompatSetup<Core.App> { public Setup(Context applicationContext) : base(applicationContext) { } protected override IMvxApplication CreateApp() { return new Core.App(); } }
2. Fix your launch Activity & Application class
Your main launcher Activity needs to inherit from MvxAppCompatActivity instead of the old MvxActivity, and you should also ensure your Application class uses the AppCompat base.
First, update your Application class
Create or modify your Android Application class to use MvxAppCompatApplication:
using MvvmCross.Droid.Support.V7.AppCompat; namespace ClaveiSGApp.Droid { public class ClaveiSGAppApplication : MvxAppCompatApplication<Setup, Core.App> { } }
Then update your AndroidManifest.xml to point to this class:
<application android:name=".ClaveiSGAppApplication" android:label="App" android:theme="@style/AppTheme"> <!-- ... other manifest entries --> </application>
Update your launcher Activity
Here’s a corrected example of your launcher Activity (splash or main):
using Android.App; using Android.OS; using MvvmCross.Droid.Support.V7.AppCompat; namespace ClaveiSGApp.Droid.Views { [Activity(Label = "App", MainLauncher = true, Theme = "@style/AppTheme.Splash", NoHistory = true)] public class SplashActivity : MvxAppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Navigate to your main activity after splash StartActivity(typeof(MainActivity)); Finish(); } } [Activity(Label = "App")] public class MainActivity : MvxAppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.Main); } } }
3. Clean up NuGet packages
Make all MvvmCross-related NuGet packages are on version 6.0.0—mixing versions (even accidentally) will cause this error. Check these packages specifically:
- MvvmCross
- MvvmCross.Droid.Support.V7.AppCompat
- MvvmCross.Platforms.Android
4. Clear build cache and rebuild
Sometimes old build artifacts cause issues after major upgrades:
- Go to
Build > Clean Solution - Delete the
binandobjfolders in your Android project - Rebuild the solution
- Restart Visual Studio 2017 to clear any lingering cache
If you’re still stuck...
Add debug logging or exception handling in your Setup class to get more details about what’s failing:
protected override void InitializeFirstChance() { // Add a breakpoint here or log to console System.Diagnostics.Debug.WriteLine("Setup initializing..."); base.InitializeFirstChance(); }
This will help you pinpoint if the issue is with app creation, service registration, or something else.
内容的提问来源于stack exchange,提问作者Brugui

