Skip to content

Instantly share code, notes, and snippets.

@grimorde
grimorde / Modal.xaml
Created January 30, 2020 15:36
Modal xaml
<ContentView.ControlTemplate>
<ControlTemplate>
<StackLayout
BackgroundColor="#4f000000"
HorizontalOptions="Fill"
VerticalOptions="Fill">
<Frame
Margin="15,25"
Padding="5"
Style="{StaticResource ModalStyle}"
@grimorde
grimorde / Modal.xaml.cs
Created January 30, 2020 15:41
Code behind for the modal control
namespace XamarinModalExample.Controls
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Modal : ContentView
{
public static readonly BindableProperty ShowModalProperty
= BindableProperty.Create(nameof(ShowModal), typeof(bool), typeof(Modal), defaultValue: false, defaultBindingMode: BindingMode.TwoWay, propertyChanged: ShowModalPropertyChanged);
public static readonly BindableProperty ModalHeaderProperty
= BindableProperty.Create(nameof(ModalHeader), typeof(string), typeof(Modal), defaultValue: string.Empty, defaultBindingMode: BindingMode.TwoWay);
@grimorde
grimorde / MainView.xaml
Created February 3, 2020 07:32
Declaration of control namespace
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="XamarinModalExample.MainView"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:XamarinModalExample.Controls;assembly=XamarinModalExample">
@grimorde
grimorde / MainView.xaml
Created February 3, 2020 07:42
Modal usage
<Grid>
<ScrollView Grid.Row="0">
<StackLayout>
<!-- main content here -->
</StackLayout>
</ScrollView>
<controls:Modal
Grid.Row="0"
ModalHeader="Help Text"
@grimorde
grimorde / MyConverter.cs
Created February 19, 2020 12:12
Converter
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// do something to get a new value
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// and change it back if you want to
@grimorde
grimorde / BooleanNegationConverter.cs
Created February 19, 2020 12:18
Boolean Negation Converter
public class BooleanNegationConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool)value;
@grimorde
grimorde / FirstValidationErrorConverter.cs
Created February 19, 2020 12:24
First Validation Error Converter
public class FirstValidationErrorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
ICollection<string> errors = value as ICollection<string>;
return errors != null && errors.Count > 0 ? errors.ElementAt(0) : null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
@grimorde
grimorde / ItemTappedEventArgsConverter.cs
Created February 19, 2020 12:34
ItemTappedEventArgsConverter
public class ItemTappedEventArgsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var eventArgs = value as ItemTappedEventArgs;
if (eventArgs == null)
throw new ArgumentException("Expected TappedEventArgs as value", "value");
return eventArgs.Item;
}
@grimorde
grimorde / ItemTappedUse.xaml
Last active February 19, 2020 12:39
Using the Item Tapped Event Arg Converter
<ListView ItemsSource="{Binding ListData}" HasUnevenRows="True">
<ListView.Behaviors>
<behaviors:EventToCommandBehavior EventName="ItemTapped" Command="{Binding ListItemSelected}" EventArgsConverter="{StaticResource ItemTappedEventArgsConverter}" />
</ListView.Behaviors>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<!- your layout here-->
</ViewCell>
@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);