Skip to content

Instantly share code, notes, and snippets.

@grimorde
grimorde / MediaAsset.cs
Last active July 30, 2020 08:36
Media Asset class
using System.ComponentModel;
using System.Runtime.CompilerServices;
using MyWandle.Core.Enums;
namespace MyApp.Core.Models
{
public class MediaAsset : INotifyPropertyChanged
{
private bool _isSelected;
public string Id { get; set; }
@grimorde
grimorde / ItemsChangeObservableRangeCollection.cs
Created July 30, 2020 08:02
Extend the ObservableRangeCollection object to recognise when an item is changed
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using MvvmHelpers;
namespace MyApp.Controls
{
public class ItemsChangeObservableRangeCollection<T> :
ObservableRangeCollection<T> where T : INotifyPropertyChanged
@grimorde
grimorde / App.xaml
Created February 20, 2020 12:45
Declaring your converter
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:converter="clr-namespace:MyApp.Converter;assembly=MyApp"
x:Class="MyApp.App">
<Application.Resources>
<ResourceDictionary>
<converter:ItemTappedEventArgsConverter x:Key="ItemTappedEventArgsConverter" />
<converter:FirstValidationErrorConverter x:Key="FirstValidationErrorConverter" />
@grimorde
grimorde / icon.xaml
Created February 20, 2020 12:37
Using the Direction Icon Converter
<Label
Grid.Column="0"
FontFamily="{StaticResource MaterialFontFamily}"
Style="{StaticResource IconStyle}"
Text="{Binding Direction, Converter={StaticResource DirectionIconConverter}}" />
@grimorde
grimorde / DirectionIconConverter.cs
Created February 20, 2020 12:34
Direction Icon Converter
public class DirectionIconConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var direction = value.ToString().ToLower();
switch (direction)
{
case "north":
return IconConstants.ArrowUpBoldCircle;
case "south":
@grimorde
grimorde / ListView.xaml
Created February 20, 2020 12:32
Alternate Row Converter on a Listview
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid
Padding="5"
BackgroundColor="{Binding ., Converter={StaticResource AlternateRowColourConverter}, ConverterParameter={x:Reference ItemsToFit}}"
HorizontalOptions="FillAndExpand">
<!-- your stuff here -->
</Grid>
@grimorde
grimorde / AlternateRowColourConverter.cs
Created February 20, 2020 12:28
Alternate Row Colour Converter
public class AlternateRowColourConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || parameter == null) return Color.LightGray;
var index = ((ListView)parameter).ItemsSource.Cast<object>().ToList().IndexOf(value);
if (index % 2 == 0)
{
return "#eaecef";
@grimorde
grimorde / indicator.xaml
Created February 20, 2020 12:27
Busy Indicator with converter to show a message
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary
x:Class="MyApp.Styles.Indicator"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<ControlTemplate x:Key="MainTemplate">
<Grid BindingContext="{TemplateBinding BindingContext}">
<ContentPresenter Grid.Row="0" />
@grimorde
grimorde / StringHasValueConverter.cs
Created February 20, 2020 12:20
String Has Value Converter
public class StringHasValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null) return false;
return !string.IsNullOrEmpty(value.ToString());
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
@grimorde
grimorde / EventToCommandBehavior.cs
Created February 19, 2020 12:42
Event To Command Behavior
public class EventToCommandBehavior : BindableBehavior<Xamarin.Forms.View>
{
public static BindableProperty CommandProperty =
BindableProperty.CreateAttached(
"Command",
typeof(ICommand),
typeof(EventToCommandBehavior),
null,
BindingMode.OneWay);