### Blazor Counter Component Example
Source: https://github.com/dreamescaper/blazorbindings.maui/blob/main/Readme.md
A sample Blazor component demonstrating a counter that increments a value on button press. It uses familiar Blazor syntax with mobile-specific components for native UI rendering. This example showcases the integration of C# code and Razor markup for UI definition.
```razor
@code {
int count;
void HandleClick()
{
count++;
}
}
```
--------------------------------
### Blazor MAUI Navigation with INavigation Service
Source: https://context7.com/dreamescaper/blazorbindings.maui/llms.txt
This code snippet demonstrates how to use the `INavigation` service in a Blazor MAUI application for page navigation. It shows examples of navigating to different pages, passing parameters, opening modal pages, and using event callbacks. The code includes examples for both basic navigation and more complex scenarios.
```xml
@* NavigationPage.razor *@
@inject INavigation Navigation
@* Navigate with no parameters *@
@* Navigate with parameters *@
@* Navigate with event callback *@
@* Modal navigation *@
@* Navigate using RenderFragment *@
@if (_callbackCount > 0)
{
}
@code {
ItemModel _item = new("Sample", 42);
int _callbackCount;
// Basic navigation
Task NavigateToDetails() => Navigation.PushAsync();
// Navigation with parameters
Task NavigateWithItem() => Navigation.PushAsync(new()
{
[nameof(DetailsPage.Item)] = _item
});
// Navigation with event callback
Task NavigateWithCallback() => Navigation.PushAsync(new()
{
[nameof(DetailsPage.Item)] = _item,
[nameof(DetailsPage.OnSaved)] = EventCallback.Factory.Create(this, OnItemSaved)
});
// Modal navigation
Task OpenModal() => Navigation.PushModalAsync(new()
{
[nameof(ModalPage.Title)] = "Edit Item"
});
// Navigation using RenderFragment syntax
Task NavigateWithMarkup() => Navigation.PushAsync(
@);
void OnItemSaved() => _callbackCount++;
record ItemModel(string Name, int Value);
}
```
```xml
@* DetailsPage.razor - Target page *@
@inject INavigation Navigation
@code {
[Parameter] public ItemModel Item { get; set; }
[Parameter] public EventCallback OnSaved { get; set; }
async Task SaveAndGoBack()
{
await OnSaved.InvokeAsync();
await Navigation.PopAsync();
}
Task GoBack() => Navigation.PopAsync();
record ItemModel(string Name, int Value);
}
```
--------------------------------
### Switch and Checkbox Components in Blazor MAUI
Source: https://context7.com/dreamescaper/blazorbindings.maui/llms.txt
Demonstrates the use of Switch and Checkbox components for boolean value toggling with two-way binding. The examples show how to bind the `IsToggled` property of a Switch and the `IsChecked` property of a Checkbox to boolean variables. It also includes an example of enabling a button based on the checkbox state.
```xml
@* Switch with two-way binding *@
@* Checkbox with two-way binding *@
@code {
bool _notificationsEnabled;
bool _agreedToTerms;
void HandleContinue()
{
// Process form submission
}
}
```
--------------------------------
### StackLayout for Linear UI Arrangement
Source: https://context7.com/dreamescaper/blazorbindings.maui/llms.txt
Demonstrates the use of `StackLayout` for arranging UI elements in a vertical or horizontal line. This example includes `Label`, `Entry`, `Button`, and `ActivityIndicator` components, showcasing two-way data binding and conditional enabling of a button.
```razor
@code {
string username;
string password;
bool isLoading;
async Task HandleLogin()
{
isLoading = true;
await Task.Delay(1000); // Simulate API call
isLoading = false;
}
}
```
--------------------------------
### FlexLayout Usage in Blazor MAUI
Source: https://context7.com/dreamescaper/blazorbindings.maui/llms.txt
Demonstrates the use of FlexLayout for creating responsive layouts in a Blazor MAUI application. It showcases row layouts, wrapping layouts for dynamic content, and a page layout with header, sidebar, content, and footer sections. The examples utilize attached properties like `FlexLayout.Basis`, `FlexLayout.Grow`, and `FlexLayout.Order` for layout control.
```xml
@using Microsoft.Maui.Layouts
@* Row layout with percentage-based sizing *@
@* Wrapping layout for dynamic content *@
@foreach (var item in items)
{
}
@* Page layout with header, sidebar, content, footer *@
@code {
List items = new()
{
new(Colors.AliceBlue), new(Colors.Aqua), new(Colors.Azure),
new(Colors.Beige), new(Colors.Crimson), new(Colors.DarkGray)
};
record ColorItem(Color Color);
}
```
--------------------------------
### Gesture Recognizers in Blazor MAUI
Source: https://context7.com/dreamescaper/blazorbindings.maui/llms.txt
Explains how to add touch interactions to elements using gesture recognizers, supporting tap, pan, swipe, pinch, and drag gestures. The example demonstrates tap, pan, and swipe gestures with corresponding event handlers.
```xml
@* Tap gesture *@
@* Pan gesture for dragging *@
@* Swipe gestures for actions *@
@code {
int _tapCount;
double _panX, _panY;
string _swipeState = "None";
void OnTapped() => _tapCount++;
void OnPan(PanUpdatedEventArgs e)
{
if (e.StatusType == GestureStatus.Running)
{
_panX = e.TotalX;
_panY = e.TotalY;
}
}
void OnSwiped(SwipedEventArgs e)
{
_swipeState = e.Direction switch
{
SwipeDirection.Left => "Deleted",
SwipeDirection.Right => "Archived",
_ => "Unknown"
};
}
}
```
--------------------------------
### Access Deployed Assets with C# Essentials
Source: https://github.com/dreamescaper/blazorbindings.maui/blob/main/samples/FlyoutPageSample/Resources/Raw/AboutAssets.txt
This C# code snippet shows how to load and read the contents of a raw asset that has been deployed with your MAUI application. It utilizes the `FileSystem.OpenAppPackageFileAsync` method from the Essentials library to get a stream to the asset, which can then be read into a string.
```csharp
async Task LoadMauiAsset()
{
using var stream = await FileSystem.OpenAppPackageFileAsync("AboutAssets.txt");
using var reader = new StreamReader(stream);
var contents = reader.ReadToEnd();
}
```
--------------------------------
### Blazor MAUI Shell Navigation Structure
Source: https://context7.com/dreamescaper/blazorbindings.maui/llms.txt
Defines the main application shell using Blazor Bindings for MAUI. It supports flyout menus, tabs, and URI-based navigation, allowing for declarative app structure and automatic route registration. Includes examples of flyout items, tabs, standalone menu items, and menu items with click handlers.
```xml
@* AppShell.razor - Main application shell *@@
@* Flyout items with tabs *@
@* Standalone menu items *@
@* Menu items with click handlers *@
@code {
[Inject] INavigation Navigation { get; set; }
ImageSource _backgroundImage = ImageSource.FromFile("background.png");
ImageSource _dashboardIcon = ImageSource.FromFile("dashboard.png");
ImageSource _profileIcon = ImageSource.FromFile("profile.png");
ImageSource _settingsIcon = ImageSource.FromFile("settings.png");
ImageSource _helpIcon = ImageSource.FromFile("help.png");
ImageSource _logoutIcon = ImageSource.FromFile("logout.png");
async Task OnHelpClick()
{
await Launcher.OpenAsync("https://docs.example.com");
}
async Task OnLogout()
{
// Navigate to specific route using URI
await Navigation.NavigateToAsync("//login");
}
}
```
--------------------------------
### Entry Component with Data Binding in Blazor MAUI
Source: https://context7.com/dreamescaper/blazorbindings.maui/llms.txt
Illustrates the use of the Entry component for text input, including two-way binding, keyboard types, and binding to different data types. It covers basic text entry, email input, numeric input, and password entry. The examples demonstrate how to handle user input and update the UI accordingly.
```xml
@* Basic text entry with two-way binding *@
@* Email keyboard *@
@* Numeric entry bound to int *@
@* Nullable double with programmatic updates *@
@* Password entry *@
@code {
string _text;
string _email;
int _quantity;
double? _price;
string _password;
void OnTextCompleted()
{
// Called when user presses return/enter
}
}
```
--------------------------------
### Configure MAUI App with BlazorBindings
Source: https://context7.com/dreamescaper/blazorbindings.maui/llms.txt
Sets up the MAUI application builder to use BlazorBindings by calling `UseMauiBlazorBindings()` and specifying the root component with `BlazorBindingsApplication`. It also demonstrates registering services for dependency injection.
```csharp
using BlazorBindings.Maui;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp>()
.UseMauiBlazorBindings();
// Register services for dependency injection
builder.Services.AddSingleton();
builder.Services.AddSingleton();
return builder.Build();
}
}
```
--------------------------------
### Include Raw Assets with MauiAsset Build Action (.csproj)
Source: https://github.com/dreamescaper/blazorbindings.maui/blob/main/samples/FlyoutPageSample/Resources/Raw/AboutAssets.txt
This snippet demonstrates how to configure the `.csproj` file to include raw assets in your MAUI application. The `MauiAsset` build action ensures that files in the specified directory (and its children) are deployed with the application package. The `LogicalName` property controls how the file is named within the deployed package.
```xml
```
--------------------------------
### Dependency Injection in Blazor Bindings MAUI (C#)
Source: https://context7.com/dreamescaper/blazorbindings.maui/llms.txt
Illustrates how to register services in MauiProgram.cs and inject them into Blazor components using standard Blazor patterns. This facilitates managing application dependencies.
```csharp
// MauiProgram.cs - Service registration
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder.UseMauiApp>()
.UseMauiBlazorBindings();
// Register services
builder.Services.AddSingleton();
builder.Services.AddSingleton();
builder.Services.AddTransient();
return builder.Build();
}
```
--------------------------------
### Hybrid Apps with BlazorWebView in Blazor Bindings MAUI
Source: https://context7.com/dreamescaper/blazorbindings.maui/llms.txt
Shows how to build hybrid applications by embedding Blazor web UI within native MAUI screens using BlazorWebView. This enables sharing state and components between native and web parts of the application.
```xml
@* MainPage.razor - Hybrid page with native and web content *@
@using HybridSample.Web
@implements IDisposable
@* Native MAUI UI in top half *@
@* Blazor Web UI in bottom half *@
@code {
[Inject] CounterState CounterState { get; set; }
ImageSource _logoSource = ImageSource.FromFile("dotnet_bot.png");
string _buttonText => CounterState.CurrentCount switch
{
0 => "Click me",
1 => "Clicked 1 time",
_ => $"Clicked {CounterState.CurrentCount} times"
};
void OnCounterClicked()
{
CounterState.IncrementCount();
}
protected override void OnInitialized()
{
// Share state between native and web components
CounterState.StateChanged += StateHasChanged;
}
public void Dispose()
{
CounterState.StateChanged -= StateHasChanged;
}
}
```
--------------------------------
### Grid Layout for Row and Column Arrangement
Source: https://context7.com/dreamescaper/blazorbindings.maui/llms.txt
Illustrates the `Grid` layout for arranging elements in rows and columns. It shows two methods for positioning child elements: using the `GridCell` wrapper and using attached properties (`Grid.Row`, `Grid.Column`). Supports flexible sizing with Auto, numeric, and star (*) options.
```razor
@* Using GridCell wrapper for positioning *@
@* Using attached properties for positioning *@
```
--------------------------------
### Dependency Injection in Blazor Bindings MAUI (Razor)
Source: https://context7.com/dreamescaper/blazorbindings.maui/llms.txt
Demonstrates injecting services like IDataService and AppState into a Blazor component using the `@inject` directive. The component then uses these services to fetch and display data.
```xml
@* Component using injected services *@
@inject IDataService DataService
@inject AppState AppState
@if (_loading)
{
}
else
{
@foreach (var item in _items)
{
}
}
@code {
List _items = new();
bool _loading = true;
protected override async Task OnInitializedAsync()
{
_items = await DataService.GetItemsAsync();
_loading = false;
}
}
```
--------------------------------
### Basic Blazor Component with State and Event Handling
Source: https://context7.com/dreamescaper/blazorbindings.maui/llms.txt
A basic Blazor component demonstrating state management and event handling. It displays a counter value from `AppState` and increments it when a button is clicked.
```razor
@* Counter.razor - Basic component with state and event handling *@@inject AppState AppState
@code {
void HandleClick()
{
AppState.Counter++;
}
}
```
--------------------------------
### TabbedPage Navigation in Blazor Bindings MAUI
Source: https://context7.com/dreamescaper/blazorbindings.maui/llms.txt
Demonstrates how to create a TabbedPage for tab-based navigation in a Blazor Bindings MAUI application. This allows users to switch between different content pages via tabs at the bottom of the screen.
```xml
@* TodoApp.razor - Root component with tabs *@
```
--------------------------------
### Blazor MAUI ContentPage Navigation Bar and Toolbar Customization
Source: https://context7.com/dreamescaper/blazorbindings.maui/llms.txt
Demonstrates customizing ContentPage properties in MAUI using Blazor Bindings. This includes controlling navigation bar visibility and shadow, tab bar visibility, title color, back button text and visibility, and implementing custom title views and toolbar items. It also shows how to handle back button presses and display alerts.
```xml
@* Custom title view in navigation bar *@
@* Toolbar items *@
@code {
BlazorBindings.Maui.Elements.Page _page;
bool _navBarVisible = true;
bool _tabBarVisible = true;
async Task HandleBackButton()
{
// Custom back button behavior
bool confirm = await _page.NativeControl.DisplayAlert(
"Confirm", "Discard changes?", "Yes", "No");
if (confirm)
{
await Navigation.PopAsync();
}
}
async Task ShowAlert()
{
await _page.NativeControl.DisplayAlert("Info", "Hello!", "OK");
}
Task Save() => Task.CompletedTask;
Task OpenSettings() => Task.CompletedTask;
}
```
--------------------------------
### CollectionView Component in Blazor MAUI
Source: https://context7.com/dreamescaper/blazorbindings.maui/llms.txt
Demonstrates how to use the CollectionView component to display scrollable lists with customizable item templates, selection modes, and empty states. It utilizes ItemsSource for data binding and ItemTemplate for custom rendering. Includes functionality for adding and removing items.
```xml
@using System.Collections.ObjectModel
@* Empty state when no items *@
@* Item template for each item *@
@code {
List _items = new()
{
new("Widget", 9.99m),
new("Gadget", 19.99m),
new("Gizmo", 14.99m)
};
Product _selectedItem;
void AddItem()
{
_items.Add(new Product($"Item {_items.Count + 1}",
Random.Shared.Next(5, 50)));
}
void RemoveSelected()
{
if (_selectedItem != null)
{
_items.Remove(_selectedItem);
_selectedItem = null;
}
}
record Product(string Name, decimal Price);
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.