### Install Sharpnado CollectionView in .NET MAUI
Source: https://github.com/roubachof/sharpnado.collectionview/blob/main/Maui/Sharpnado.CollectionView.Maui/ReadMe.md
This snippet shows how to install and enable Sharpnado's CollectionView in your .NET MAUI application's core project. It involves modifying the MauiProgram.cs file to include the UseSharpnadoCollectionView extension method.
```csharp
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp()
.UseSharpnadoCollectionView(loggerEnable: false);
}
```
--------------------------------
### Load Raw Asset using C# and .NET MAUI Essentials
Source: https://github.com/roubachof/sharpnado.collectionview/blob/main/Maui/MauiSample/Resources/Raw/AboutAssets.txt
This C# code demonstrates how to load a raw asset that has been deployed with the application package. It uses `FileSystem.OpenAppPackageFileAsync` to get a stream to the file and `StreamReader` to read its contents. Ensure the asset file exists in the correct location within the project.
```csharp
async Task LoadMauiAsset()
{
using var stream = await FileSystem.OpenAppPackageFileAsync("AboutAssets.txt");
using var reader = new StreamReader(stream);
var contents = reader.ReadToEnd();
}
```
--------------------------------
### Usage: CarouselView
Source: https://github.com/roubachof/sharpnado.collectionview/blob/main/Maui/Sharpnado.CollectionView.Maui/ReadMe.md
A basic example of how to implement a carousel view using Sharpnado's CollectionView. This snippet is a placeholder and assumes default configurations for a carousel layout.
```xml
```
--------------------------------
### Initialize Sharpnado.CollectionView in .NET MAUI
Source: https://context7.com/roubachof/sharpnado.collectionview/llms.txt
Initializes the Sharpnado.CollectionView library within a .NET MAUI application's startup configuration by calling the UseSharpnadoCollectionView extension method. This setup is typically done in the MauiProgram.cs file. It allows disabling logging and debug logs.
```csharp
// MauiProgram.cs
using Microsoft.Maui;
using Microsoft.Maui.Hosting;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp()
.UseSharpnadoCollectionView(loggerEnable: false, debugLogEnable: false);
return builder.Build();
}
}
```
--------------------------------
### Usage: GridView with Drag and Drop
Source: https://github.com/roubachof/sharpnado.collectionview/blob/main/Maui/Sharpnado.CollectionView.Maui/ReadMe.md
Example of using Sharpnado's GridView for a grid layout. This snippet demonstrates setting the column count, enabling drag and drop, and binding the ItemsSource. It is suitable for displaying items in a fixed grid format.
```xml
```
--------------------------------
### C# ViewModel for Drag and Drop - Sharpnado CollectionView
Source: https://context7.com/roubachof/sharpnado.collectionview/llms.txt
Implements the ViewModel for the grid layout, managing the collection of items and handling drag and drop events. It initializes the item list, defines commands for drag start, drag end, and item taps, and updates the item order upon drag completion.
```csharp
public class GridViewModel : INotifyPropertyChanged
{
public ObservableCollection Items { get; set; }
public ICommand OnDragStartedCommand { get; }
public ICommand OnDragEndedCommand { get; }
public ICommand ItemTappedCommand { get; }
public GridViewModel()
{
Items = new ObservableCollection();
for (int i = 1; i <= 30; i++)
{
Items.Add(new GridItem
{
Id = i,
Name = $"Item {i}",
ImageUrl = $"item{i}.png"
});
}
OnDragStartedCommand = new Command(info =>
{
Debug.WriteLine($"Drag started: from index {info.From} to {info.To}");
});
OnDragEndedCommand = new Command(info =>
{
Debug.WriteLine($"Drag ended: moved from {info.From} to {info.To}");
var item = Items[info.From];
Items.RemoveAt(info.From);
Items.Insert(info.To, item);
});
ItemTappedCommand = new Command(item =>
{
Debug.WriteLine($"Tapped item: {item.Name}");
});
}
}
```
--------------------------------
### Drag and Drop Bindable Properties for Grid/Vertical Layout
Source: https://github.com/roubachof/sharpnado.collectionview/blob/main/README.md
Introduces specific BindableProperties related to drag and drop functionality when the CollectionView is using a Grid or Vertical ListLayout. These include commands to signal the start and end of a drag-and-drop operation, and a property to indicate if a drag-and-drop is currently in progress.
```csharp
public static readonly BindableProperty DragAndDropStartedCommandProperty = BindableProperty.Create(
nameof(DragAndDropStartedCommand),
typeof(ICommand),
typeof(CollectionView));
public static readonly BindableProperty DragAndDropEndedCommandProperty = BindableProperty.Create(
nameof(DragAndDropEndedCommand),
typeof(ICommand),
typeof(CollectionView));
public static readonly BindableProperty IsDragAndDroppingProperty = BindableProperty.Create(
nameof(IsDragAndDropping),
typeof(bool),
typeof(CollectionView),
defaultValue: false);
```
--------------------------------
### C# Grid Layout Rotation Reveal Animation for CollectionView
Source: https://context7.com/roubachof/sharpnado.collectionview/llms.txt
An alternative implementation for custom reveal animations, specifically tailored for grid layouts. This example demonstrates fading and rotating items based on the CollectionView's layout orientation. It uses FindByName to access the CollectionView and applies conditional rotation (X for vertical, Y for horizontal).
```csharp
// Alternative: Rotation reveal for grid layouts
public class GridPageWithAnimation : ContentPage
{
public GridPageWithAnimation()
{
InitializeComponent();
var collectionView = this.FindByName("CollectionView");
collectionView.PreRevealAnimationAsync = async (viewCell) =>
{
viewCell.View.Opacity = 0;
if (collectionView.CollectionLayout == CollectionViewLayout.Vertical)
{
viewCell.View.RotationX = 90;
}
else
{
viewCell.View.RotationY = -90;
}
};
collectionView.RevealAnimationAsync = async (viewCell) =>
{
var fadeTask = viewCell.View.FadeTo(1, 250);
var rotateTask = collectionView.CollectionLayout == CollectionViewLayout.Vertical
? viewCell.View.RotateXTo(0, 250)
: viewCell.View.RotateYTo(0, 250);
await Task.WhenAll(fadeTask, rotateTask);
};
}
}
```
--------------------------------
### Define MauiAsset Build Action in .csproj
Source: https://github.com/roubachof/sharpnado.collectionview/blob/main/Maui/MauiSample/Resources/Raw/AboutAssets.txt
This XML snippet shows how to configure the `.csproj` file to include raw assets. The `MauiAsset` Build Action ensures that files in the specified directory are deployed with the application package. `LogicalName` is used to define the path of the asset within the package.
```xml
```
--------------------------------
### C# Custom Reveal Animations for CollectionView
Source: https://context7.com/roubachof/sharpnado.collectionview/llms.txt
Defines pre-reveal, main reveal, and post-reveal animations for a CollectionView. The pre-reveal sets initial states, the main reveal animates the item into view, and the post-reveal performs optional cleanup. Assumes use of Sharpnado's CollectionView and Task.WhenAll for concurrent animations.
```csharp
public partial class AnimatedListPage : ContentPage
{
public AnimatedListPage()
{
InitializeComponent();
// Pre-reveal animation (setup before item becomes visible)
CollectionView.PreRevealAnimationAsync = async (viewCell) =>
{
viewCell.View.Opacity = 0;
viewCell.View.TranslationX = -50;
viewCell.View.Scale = 0.8;
};
// Main reveal animation (item becoming visible)
CollectionView.RevealAnimationAsync = async (viewCell) =>
{
await Task.WhenAll(
viewCell.View.FadeTo(1, 300),
viewCell.View.TranslateTo(0, 0, 300, Easing.SpringOut),
viewCell.View.ScaleTo(1, 300, Easing.SpringOut)
);
};
// Post-reveal animation (optional cleanup after reveal)
CollectionView.PostRevealAnimationAsync = async (viewCell) =>
{
await viewCell.View.RotateTo(360, 200, Easing.CubicOut);
viewCell.View.Rotation = 0; // Reset rotation
};
}
}
```
--------------------------------
### CollectionView Commands and Animation Properties
Source: https://github.com/roubachof/sharpnado.collectionview/blob/main/README.md
Details properties for imperative control and customization of the CollectionView's behavior. Includes a command to force layout updates, event handlers for layout changes, and delegates for custom pre-reveal, reveal, and post-reveal animations on view cells. Also includes properties for view caching and drag-and-drop enabling.
```csharp
///
/// Force the layout of the collection view to be updated.
/// Useful if you changed a property that should impact the layout but the layout is not updated.
///
public Command UpdateLayout { get; internal set; } = new Command(() => { });
public event EventHandler ListLayoutChanging;
public Func PreRevealAnimationAsync { get; set; }
public Func RevealAnimationAsync { get; set; }
public Func PostRevealAnimationAsync { get; set; }
/// In certain scenarios, the first scroll of the list can be smoothen
/// by pre-building some views.
public int ViewCacheSize { get; set; } = 0;
public bool EnableDragAndDrop { get; set; } = false;
public SnapStyle SnapStyle { get; set; } = SnapStyle.None;
public int ColumnCount { get; set; } = 0;
public ScrollSpeed ScrollSpeed { get; set; } = ScrollSpeed.Normal;
```
--------------------------------
### Initialize Sharpnado.CollectionView in Xamarin.Forms (Core)
Source: https://github.com/roubachof/sharpnado.collectionview/blob/main/README.md
This code initializes Sharpnado.CollectionView for Xamarin.Forms applications. The Initialize method should be called from the App.xaml.cs file to enable the namespace schema for the collection view.
```csharp
public App()
{
InitializeComponent();
Sharpnado.CollectionView.Initializer.Initialize(true, false);
...
}
```
--------------------------------
### XAML Resource Dictionary for CollectionView Item Templates
Source: https://github.com/roubachof/sharpnado.collectionview/blob/main/README.md
Provides XAML definitions for `DataTemplate` resources used in a `CollectionView`. It includes templates for a header, a footer, and a group header. Each template is designed to be used with `sho:DraggableViewCell` and defines the visual structure for these distinct item types, including labels, layouts, and activity indicators for the footer.
```xml
```
--------------------------------
### Initialize Sharpnado.CollectionView in Xamarin.Forms (Android)
Source: https://github.com/roubachof/sharpnado.collectionview/blob/main/README.md
This code demonstrates the initialization of Sharpnado.CollectionView on Android within the OnCreate method. It needs to be executed before Xamarin.Forms.Forms.Init() and LoadApplication(new App()).
```csharp
public override OnCreate(Bundle savedInstanceState)
{
Initializer.Initialize();
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
}
```
--------------------------------
### Initialize Sharpnado.CollectionView in Xamarin.Forms (iOS)
Source: https://github.com/roubachof/sharpnado.collectionview/blob/main/README.md
This snippet shows the initialization of Sharpnado.CollectionView on iOS within the FinishedLaunching method. It must be called before Xamarin.Forms.Forms.Init() and LoadApplication(new App()).
```csharp
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
Initializer.Initialize();
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
}
```
--------------------------------
### C# ViewModel for Populating CollectionView with Headers, Footers, and Groups
Source: https://github.com/roubachof/sharpnado.collectionview/blob/main/README.md
Demonstrates a C# ViewModel (`HeaderFooterGroupingPageViewModel`) that loads and processes data to populate a collection view with headers, footers, and grouped items. It uses LINQ for ordering and grouping, and constructs a list of `IDudeItem` which includes custom header, footer, and group header objects along with data items. This ViewModel is designed to work with asynchronous data loading.
```csharp
public class HeaderFooterGroupingPageViewModel : ANavigableViewModel
{
public List SillyPeople
{
get => _sillyPeople;
set => SetAndRaise(ref _sillyPeople, value);
}
private async Task> LoadSillyPeoplePageAsync(int pageNumber, int pageSize, bool isRefresh)
{
PageResult resultPage = await _sillyDudeService.GetSillyPeoplePage(pageNumber, pageSize);
var dudes = resultPage.Items;
if (isRefresh)
{
SillyPeople = new List();
_listSource = new List();
}
var result = new List { new DudeHeader() };
_listSource.AddRange(dudes);
foreach (var group in _listSource.OrderByDescending(d => d.SillinessDegree)
.GroupBy((dude) => dude.SillinessDegree))
{
result.Add(new DudeGroupHeader { StarCount = group.Key});
result.AddRange(group.Select(dude => new SillyDudeVmo(dude, TapCommand)));
}
result.Add(new DudeFooter());
SillyPeople = result;
}
}
```
--------------------------------
### Bindable Properties for CollectionView Layout and Data
Source: https://github.com/roubachof/sharpnado.collectionview/blob/main/README.md
Defines essential BindableProperties for the CollectionView, including layout options, item source binding, item templates, sizing, spacing, and commands for user interactions like tapping and scrolling. These properties are fundamental for controlling the appearance and behavior of the collection view.
```csharp
public static readonly BindableProperty ListLayoutProperty = BindableProperty.Create(
nameof(ListLayout),
typeof(CollectionViewLayout),
typeof(CollectionView),
CollectionViewLayout.Linear,
propertyChanged: OnListLayoutChanged,
propertyChanging: OnListLayoutChanging);
public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(
nameof(ItemsSource),
typeof(IEnumerable),
typeof(CollectionView),
default(IEnumerable