Xamarin development is all about creating native cross-platform user interfaces that seamlessly integrate with ASP.NET Core. In this comprehensive guide, we'll walk you through setting up your development environment and exploring existing features on the ASP.NET Zero Xamarin platform.
Before diving in, make sure you have the necessary tools:
- Visual Studio 2017 v15.4.4+
- Mac for iOS simulator
With these essentials in place, you're ready to develop ASP.NET Zero Xamarin applications that can run on:
- iOS 8 or higher
- Android 4.0.3 (API 15) or higher
To get started with Android development, ensure you have the latest Android SDK Tools and Android API platform installed. Update your tools using the Android SDK Manager.
For iOS development, we recommend having at least the iOS 10 SDK and Xcode installed on your Mac.
Solution Structure (Layers)
When you download your project, you'll see three types of solutions:
- .Mobile Xamarin and related projects included
- .Web Web and related projects included
- .All All the projects included
To focus specifically on Xamarin development, open the .Mobile.sln solution.
Xamarin Architecture
The mobile solution consists of six projects:
- Core.Shared project contains basic primitive types like consts, enums that used in all layers of the solution.
- Application.Shared project contains data transfer objects (DTOs) and interfaces of application services shared with application layer of the backend.
- Application.Client project contains Web Api client (bridge between mobile app and host) and proxy classes of Application Services.
- Mobile.Shared project houses the code shared by iOS and Android platforms, views, view models, localization, and platform abstractions.
- Mobile.iOS project contains iOS-specific classes.
- Mobile.Droid project contains Android-specific classes.
Debugging
Host
To start debugging your Xamarin app, you need to configure host settings. Use Web.Host to feed the Xamarin app.
Open Windows Command Prompt, navigate to the folder containing your Web.Host csproj file, and run the command below:
`
dotnet run --launch-profile Mobile
`
This will start the Web.Host project from the address "https://0.0.0.0:44301" which enables access from external networks.
Alternatively, use the start-host-mobile.bat file in your Web.Host directory for Windows users.
Warning
If you want to start Web.Host from Visual Studio, set Web.Host as the startup project and choose the Mobile profile.
Prepare for Production
When releasing your Xamarin app for production, remember to change DefaultHostUrl in ApiUrlConfig class!
`
ApiUrlConfig.DefaultHostUrl = "https://mywebapi.com/"
`
Important: Be aware that connecting over Wi-Fi requires both the device and computer to be on the same Wi-Fi network. To avoid confusion, you can completely turn off your mobile data connection.
Debugging Android
For Android development, follow these steps:
- Read the Xamarin Android Setup and Deployment document for necessary setups.
- If using the default Android Emulator, no changes are needed. The default loopback address is 10.0.2.2.
- In StartApplication() method of SplashActivity.cs, update DebugServerIpAddresses.Current to "10.0.2.2".
For Genymotion Emulator or a real Android device, update DebugServerIpAddresses.Current accordingly.
After successful setup, set Mobile.Droid as the startup project and choose an Android emulator from the list.
Debugging iOS
For iOS development on Visual Studio, ensure you have:
- A Mac to compile IPA files
- Apple's certificates and code-signing tools for deploying apps to devices
- The iOS simulator can be used only on a Mac
The IP address for the Web.Host backend service is written in the FinishedLaunching() method of your app.