Creating a basic mobile application using C#.NET typically involves using Xamarin, a popular open-source platform for building modern and performant applications for iOS, Android, and Windows with .NET.
Here’s a simple step-by-step tutorial on how to create a basic “Hello, World!” mobile application using Xamarin.Forms in Visual Studio.
Prerequisites: Visual Studio 2019 or later with the Mobile development with .NET (Xamarin) workload installed.
Step 1: Create a New Project
- Launch Visual Studio and select “Create a new project”.
- In the “Create a new project” window, select the “Mobile App (Xamarin.Forms)” template and click “Next”.
- In the “Configure your new project” window, enter a name for your project and choose a location to save it, then click “Create”.
- In the “New Mobile App” window, select the “Blank” template, and ensure “Android” and “iOS” platforms are checked, then click “Create”.
Step 2: Understand the Project Structure
The solution created by Visual Studio includes three projects:
- A .NET Standard project (shared code)
- An Android project
- An iOS project
The shared code project is where you’ll write most of your code, which will be shared across Android and iOS. The other two projects are for platform-specific implementations.
Step 3: Write Your Code
- In the Solution Explorer, expand the shared code project and open the MainPage.xaml file.
- Replace the
<Label>
element in the XAML code with the following:
<StackLayout>
<Label
Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
<Button
Text="Click Me!"
Clicked="OnButtonClicked"/>
</StackLayout>
- Open the MainPage.xaml.cs file and add the following method:
void OnButtonClicked(object sender, EventArgs args)
{
(sender as Button).Text = "Hello, World!";
}
Step 4: Run Your App
- Select the Android or iOS project as the startup project.
- Press F5 or click on the “Start Debugging” button.
Your application will launch in the selected platform’s emulator. The screen will display a “Welcome to Xamarin.Forms!” message and a “Click Me!” button. When you press the button, the text on the button will change to “Hello, World!”.
This is a very simple example of a mobile application. Xamarin.Forms allows you to create much more complex applications with features like multiple pages, navigation, animations, data binding, and more.