Xamarin Live测试iOS应用构造函数调用异常及相关问题咨询
Hey Dan, let's work through each of your issues step by step to get you back on track:
1. Why Your Parameterized App(string db_path) Constructor Isn't Being Called in Xamarin Live Player
Xamarin Live Player has a specific quirk here: when it spins up your app for testing, it doesn’t always honor the constructor call from AppDelegate.FinishedLaunching. Instead, it defaults to instantiating the App class using the parameterless constructor directly. This is because Live Player uses a simplified launch flow to speed up testing, skipping some native initialization steps that would normally pass your custom parameters.
To confirm your parameterized constructor works as intended, try testing with a full iOS build (either on a physical device or simulator, not Live Player). If you still need to use Live Player for quick tests, you can temporarily mimic the database path in your parameterless constructor like this:
public App() { // Temp setup for Live Player testing string fileName = "books_db.sqlite"; string fileLocation = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "..", "Library"); string full_path = Path.Combine(fileLocation, fileName); InitializeComponent(); // Rest of your app setup using full_path }
2. Is Your Session Token Storage Path Correct?
Absolutely—your chosen path is perfect for storing session tokens!
The Library directory (accessed via SpecialFolder.Personal/../Library) is designed for app-specific, non-user-facing data. It’s not synced with iCloud by default, which makes it a safe spot for tokens, cached data, or database files that don’t need to be visible to the user.
For extra security with sensitive data like session tokens, you might also consider using the iOS Keychain (it encrypts data at rest), but your current path is totally valid for general app data storage.
3. Fixing the ArgumentNullException: Value cannot be null. Parameter name: image Error
When you switched to the parameterless constructor, this error means an Image control somewhere in your app is being passed a null value for its Source property. Here’s how to debug and fix it:
- Check the Debug Output Stack Trace: Look for the full exception details in the Debug Output window—it will pinpoint exactly which line of code or XAML element is causing the issue.
- Audit All Image Sources:
- In XAML, verify every
<Image Source="..." />element: ensure the filename is correct, and the image’s Build Action is set toBundleResource(for iOS local images). - In C# code, check for places where you set
Image.Source = nullor bind to a view model property that hasn’t been initialized (e.g., a property that’snullwhen the page loads).
- In XAML, verify every
- Add Null Checks: If you load images dynamically, add a null check before assigning the source, or set a fallback image for cases where the source is null.
Your AppDelegate.FinishedLaunching Code for Reference
public override bool FinishedLaunching(UIApplication app, NSDictionary options) { string fileName = "books_db.sqlite"; string fileLocation = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "..", "Library"); string full_path = Path.Combine(fileLocation, fileName); global::Xamarin.Forms.Forms.Init(); LoadApplication(new App(full_path)); return base.FinishedLaunching(app, options); }
内容的提问来源于stack exchange,提问作者Dan

