Zum Inhalt

Xamarin Cheatsheet

Xamarin - Cross-Platform Mobile Apps mit .NET

Xamarin ist eine Open-Source-Plattform für den Aufbau moderner und performanter Anwendungen für iOS, Android und Windows mit .NET. Xamarin ist eine Abstraktionsschicht, die die Kommunikation des gemeinsamen Codes mit dem zugrunde liegenden Plattformcode verwaltet. < p>

generieren

Inhaltsverzeichnis

  • [Installation](#installation
  • (#getting-started)
  • [Projektstruktur](#project-structure_
  • Xamarin.Formulare
  • Xamarin.Native
  • (XAML) (#xaml)
  • [Datenbindung](LINK_6__
  • [Navigation](LINK_7__
  • (#dependencyservice_)
  • [Kustom Renderers](LINK_9_
  • Effekte
  • [MessagingCenter](LINK_11__
  • [Daten Persistence](LINK_12_
  • [Netzwerk](#networking_
  • Test
  • Bestellung
  • Beste Praktiken
  • (#troubleshooting_)

Installation

Voraussetzungen

# Install Visual Studio (Windows) or Visual Studio for Mac (macOS)
# Make sure to include the "Mobile development with .NET" workload

# For iOS development on Windows, you need a Mac with Xcode and Visual Studio for Mac installed
```_

### Installation überprüfen
```bash
# Open Visual Studio Installer
# Check if "Mobile development with .NET" is installed

# Check for Android SDK and NDK
# Visual Studio > Tools > Options > Xamarin > Android Settings

# Check for Xcode and Apple SDKs (macOS)
# Visual Studio for Mac > Preferences > Projects > SDK Locations > Apple
```_

## Erste Schritte

### Neues Projekt erstellen
```bash
# In Visual Studio
# File > New > Project
# Select "Mobile App (Xamarin.Forms)"
# Choose a template (Blank, Tabbed, Shell)

# In Visual Studio for Mac
# File > New Solution
# Select "Multiplatform > App > Xamarin.Forms"
# Choose a template (Blank, Tabbed, Shell)
```_

### Die App starten
```bash
# Select a startup project (Android or iOS)
# Choose a device or emulator
# Click the "Run" button or press F5
```_

## Projektstruktur
MyApp/ ├── MyApp/ # .NET Standard library (shared code) │ ├── App.xaml │ ├── App.xaml.cs │ ├── MainPage.xaml │ ├── MainPage.xaml.cs │ ├── Models/ │ ├── Views/ │ ├── ViewModels/ │ └── Services/ ├── MyApp.Android/ # Android-specific project │ ├── MainActivity.cs │ ├── Resources/ │ └── Assets/ ├── MyApp.iOS/ # iOS-specific project │ ├── AppDelegate.cs │ ├── Main.cs │ └── Info.plist └── MyApp.sln # Solution file ```_

Xamarin. Formulare

Seiten

```csharp // ContentPage public class MyContentPage : ContentPage { public MyContentPage() { Content = new StackLayout { Children = { new Label { Text = "Welcome to Xamarin.Forms!" } } }; } }

// TabbedPage public class MyTabbedPage : TabbedPage { public MyTabbedPage() { Children.Add(new MyContentPage { Title = "Tab 1" }); Children.Add(new MyContentPage { Title = "Tab 2" }); } }

// FlyoutPage (Master-Detail) public class MyFlyoutPage : FlyoutPage { public MyFlyoutPage() { Flyout = new ContentPage { Title = "Menu" }; Detail = new NavigationPage(new MyContentPage()); } } ```_

Layouts

```csharp // StackLayout new StackLayout { Orientation = StackOrientation.Vertical, Spacing = 10, Children = { new Label(), new Button() } };

// Grid new Grid { RowDefinitions = { new RowDefinition { Height = GridLength.Auto } }, ColumnDefinitions = { new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) } }, Children = { { new Label(), 0, 0 } } };

// FlexLayout new FlexLayout { Direction = FlexDirection.Row, JustifyContent = FlexJustify.SpaceBetween, Children = { new Label(), new Button() } };

// AbsoluteLayout new AbsoluteLayout { Children = { { new BoxView { Color = Colors.Blue }, new Rectangle(0, 0, 100, 100) } } };

// RelativeLayout new RelativeLayout { Children = { { new BoxView(), Constraint.Constant(0) } } }; ```_

Kontrolle

```csharp // Label new Label { Text = "Hello, Xamarin!", FontSize = 24 };

// Button new Button { Text = "Click Me", Command = new Command(() => DisplayAlert("Title", "Message", "OK")) };

// Entry new Entry { Placeholder = "Enter text" };

// Editor new Editor { Placeholder = "Enter multi-line text" };

// SearchBar new SearchBar { Placeholder = "Search..." };

// Slider new Slider { Minimum = 0, Maximum = 100 };

// Stepper new Stepper { Minimum = 0, Maximum = 10 };

// Switch new Switch { IsToggled = true };

// DatePicker new DatePicker();

// TimePicker new TimePicker();

// ListView new ListView { ItemsSource = new[] { "Item 1", "Item 2" }, ItemTemplate = new DataTemplate(() => { var cell = new TextCell(); cell.SetBinding(TextCell.TextProperty, "."); return cell; }) };

// CollectionView new CollectionView { ItemsSource = new[] { "Item 1", "Item 2" } }; ```_

Xamarin. Native

Xamarin. Android

```csharp // MainActivity.cs [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)] public class MainActivity : AppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.activity_main);

    var button = FindViewById<Button>(Resource.Id.myButton);
    button.Click += (sender, e) =>
    {
        Toast.MakeText(this, "Button Clicked!", ToastLength.Short).Show();
    };
}

} ```_

Xamarin.iOS

```csharp // ViewController.cs public partial class ViewController : UIViewController { public ViewController(IntPtr handle) : base(handle) { }

public override void ViewDidLoad()
{
    base.ViewDidLoad();

    var button = new UIButton(UIButtonType.System);
    button.Frame = new CGRect(20, 200, 280, 44);
    button.SetTitle("Click Me", UIControlState.Normal);
    button.TouchUpInside += (sender, e) =>
    {
        var alert = UIAlertController.Create("Title", "Message", UIAlertControllerStyle.Alert);
        alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
        PresentViewController(alert, true, null);
    };
    View.AddSubview(button);
}

} ```_

XAML

```xml