Skip to content

Commit

Permalink
Two more samples (dotnet#237)
Browse files Browse the repository at this point in the history
* Two more samples.

* Add readme's.
  • Loading branch information
davidbritch authored Jul 12, 2022
1 parent c82cc59 commit 4bbedc9
Show file tree
Hide file tree
Showing 184 changed files with 6,592 additions and 0 deletions.
27 changes: 27 additions & 0 deletions 6.0/Fundamentals/DataBindingDemos/DataBindingDemos.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31611.283
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DataBindingDemos", "DataBindingDemos\DataBindingDemos.csproj", "{76AFC62D-A22C-4057-A411-D5A1E1D068F4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{76AFC62D-A22C-4057-A411-D5A1E1D068F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{76AFC62D-A22C-4057-A411-D5A1E1D068F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{76AFC62D-A22C-4057-A411-D5A1E1D068F4}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{76AFC62D-A22C-4057-A411-D5A1E1D068F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{76AFC62D-A22C-4057-A411-D5A1E1D068F4}.Release|Any CPU.Build.0 = Release|Any CPU
{76AFC62D-A22C-4057-A411-D5A1E1D068F4}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {61F7FB11-1E47-470C-91E2-47F8143E1572}
EndGlobalSection
EndGlobal
29 changes: 29 additions & 0 deletions 6.0/Fundamentals/DataBindingDemos/DataBindingDemos/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DataBindingDemos"
x:Class="DataBindingDemos.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>

<Style TargetType="StackLayout">
<Setter Property="Spacing" Value="6" />
</Style>

<Style TargetType="Grid">
<Setter Property="RowSpacing" Value="6" />
<Setter Property="ColumnSpacing" Value="6" />
</Style>

<x:String x:Key="fallbackImageUrl">https://upload.wikimedia.org/wikipedia/commons/2/20/Point_d_interrogation.jpg</x:String>
<x:String x:Key="locationUnknown">Location unknown</x:String>
<x:String x:Key="populationUnknown">Population size unknown</x:String>
<x:String x:Key="unknown">Unknown</x:String>

</ResourceDictionary>
</Application.Resources>
</Application>
11 changes: 11 additions & 0 deletions 6.0/Fundamentals/DataBindingDemos/DataBindingDemos/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace DataBindingDemos;

public partial class App : Application
{
public App()
{
InitializeComponent();

MainPage = new AppShell();
}
}
14 changes: 14 additions & 0 deletions 6.0/Fundamentals/DataBindingDemos/DataBindingDemos/AppShell.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="DataBindingDemos.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DataBindingDemos"
Shell.FlyoutBehavior="Disabled">

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />

</Shell>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DataBindingDemos;

public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace DataBindingDemos.Controls
{
public class CardView : ContentView
{
public static readonly BindableProperty CardTitleProperty = BindableProperty.Create(nameof(CardTitle), typeof(string), typeof(CardView), string.Empty);
public static readonly BindableProperty CardDescriptionProperty = BindableProperty.Create(nameof(CardDescription), typeof(string), typeof(CardView), string.Empty);
public static readonly BindableProperty BorderColorProperty = BindableProperty.Create(nameof(BorderColor), typeof(Color), typeof(CardView), Colors.DarkGray);
public static readonly BindableProperty CardColorProperty = BindableProperty.Create(nameof(CardColor), typeof(Color), typeof(CardView), Colors.White);
public static readonly BindableProperty IconImageSourceProperty = BindableProperty.Create(nameof(IconImageSource), typeof(ImageSource), typeof(CardView), default(ImageSource));
public static readonly BindableProperty IconBackgroundColorProperty = BindableProperty.Create(nameof(IconBackgroundColor), typeof(Color), typeof(CardView), Colors.LightGray);

public string CardTitle
{
get => (string)GetValue(CardView.CardTitleProperty);
set => SetValue(CardView.CardTitleProperty, value);
}

public string CardDescription
{
get => (string)GetValue(CardView.CardDescriptionProperty);
set => SetValue(CardView.CardDescriptionProperty, value);
}

public Color BorderColor
{
get => (Color)GetValue(CardView.BorderColorProperty);
set => SetValue(CardView.BorderColorProperty, value);
}

public Color CardColor
{
get => (Color)GetValue(CardView.CardColorProperty);
set => SetValue(CardView.CardColorProperty, value);
}

public ImageSource IconImageSource
{
get => (ImageSource)GetValue(CardView.IconImageSourceProperty);
set => SetValue(CardView.IconImageSourceProperty, value);
}

public Color IconBackgroundColor
{
get => (Color)GetValue(CardView.IconBackgroundColorProperty);
set => SetValue(CardView.IconBackgroundColorProperty, value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace DataBindingDemos.Controls
{
public class CardViewExpander : ContentView
{
public static readonly BindableProperty CardTitleProperty = BindableProperty.Create(nameof(CardTitle), typeof(string), typeof(CardViewExpander), string.Empty);
public static readonly BindableProperty CardDescriptionProperty = BindableProperty.Create(nameof(CardDescription), typeof(string), typeof(CardViewExpander), string.Empty);
public static readonly BindableProperty BorderColorProperty = BindableProperty.Create(nameof(BorderColor), typeof(Color), typeof(CardViewExpander), Colors.DarkGray);
public static readonly BindableProperty CardColorProperty = BindableProperty.Create(nameof(CardColor), typeof(Color), typeof(CardViewExpander), Colors.White);
public static readonly BindableProperty IconImageSourceProperty = BindableProperty.Create(nameof(IconImageSource), typeof(ImageSource), typeof(CardViewExpander), default(ImageSource));
public static readonly BindableProperty IconBackgroundColorProperty = BindableProperty.Create(nameof(IconBackgroundColor), typeof(Color), typeof(CardViewExpander), Colors.LightGray);
public static readonly BindableProperty IsExpandedProperty = BindableProperty.Create(nameof(IsExpanded), typeof(bool), typeof(CardViewExpander), false);

public string CardTitle
{
get => (string)GetValue(CardViewExpander.CardTitleProperty);
set => SetValue(CardViewExpander.CardTitleProperty, value);
}

public string CardDescription
{
get => (string)GetValue(CardViewExpander.CardDescriptionProperty);
set => SetValue(CardViewExpander.CardDescriptionProperty, value);
}

public Color BorderColor
{
get => (Color)GetValue(CardViewExpander.BorderColorProperty);
set => SetValue(CardViewExpander.BorderColorProperty, value);
}

public Color CardColor
{
get => (Color)GetValue(CardViewExpander.CardColorProperty);
set => SetValue(CardViewExpander.CardColorProperty, value);
}

public ImageSource IconImageSource
{
get => (ImageSource)GetValue(CardViewExpander.IconImageSourceProperty);
set => SetValue(CardViewExpander.IconImageSourceProperty, value);
}

public Color IconBackgroundColor
{
get => (Color)GetValue(CardViewExpander.IconBackgroundColorProperty);
set => SetValue(CardViewExpander.IconBackgroundColorProperty, value);
}

public bool IsExpanded
{
get => (bool)GetValue(CardViewExpander.IsExpandedProperty);
set => SetValue(CardViewExpander.IsExpandedProperty, value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace DataBindingDemos
{
// This is a hack until the Expander is present in .NET MAUI Community Toolkit.
public class Expander : Grid
{
public static readonly BindableProperty IsExpandedProperty = BindableProperty.Create(nameof(IsExpanded), typeof(bool), typeof(Expander), false);

public bool IsExpanded
{
get => (bool)GetValue(IsExpandedProperty);
set => SetValue(IsExpandedProperty, value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace DataBindingDemos.Controls
{
public class Separator : BoxView
{
public Separator()
{
Color = Colors.Gray;
HeightRequest = 2;
Opacity = 0.5;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Globalization;

namespace DataBindingDemos
{
public class AllTrueMultiConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null || !targetType.IsAssignableFrom(typeof(bool)))
{
return false;
// Alternatively, return BindableProperty.UnsetValue to use the binding FallbackValue
}

foreach (var value in values)
{
if (!(value is bool b))
{
return false;
// Alternatively, return BindableProperty.UnsetValue to use the binding FallbackValue
}
else if (!b)
{
return false;
}
}
return true;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
if (!(value is bool b) || targetTypes.Any(t => !t.IsAssignableFrom(typeof(bool))))
{
// Return null to indicate conversion back is not possible
return null;
}

if (b)
{
return targetTypes.Select(t => (object)true).ToArray();
}
else
{
// Can't convert back from false because of ambiguity
return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Globalization;

namespace DataBindingDemos
{
public class AnyTrueMultiConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null || !targetType.IsAssignableFrom(typeof(bool)))
{
return false;
// Alternatively, return BindableProperty.UnsetValue to use the binding FallbackValue
}

foreach (var value in values)
{
if (!(value is bool b))
{
return false;
// Alternatively, return BindableProperty.UnsetValue to use the binding FallbackValue
}
else if (b)
{
return true;
}
}
return false;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
if (!(value is bool b) || targetTypes.Any(t => !t.IsAssignableFrom(typeof(bool))))
{
// Return null to indicate conversion back is not possible
return null;
}

if (!b)
{
return targetTypes.Select(t => (object)false).ToArray();
}
else
{
// Can't convert back from true because of ambiguity
return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Globalization;

namespace DataBindingDemos
{
public class BoolToObjectConverter<T> : IValueConverter
{
public T TrueObject { set; get; }

public T FalseObject { set; get; }

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? TrueObject : FalseObject;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((T)value).Equals(TrueObject);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Globalization;

namespace DataBindingDemos
{
public class FloatToIntConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (int)Math.Round((float)value * GetParameter(parameter));
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (int)value / GetParameter(parameter);
}

double GetParameter(object parameter)
{
if (parameter is float)
return (float)parameter;

else if (parameter is int)
return (int)parameter;

else if (parameter is string)
return float.Parse((string)parameter);

return 1;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Globalization;

namespace DataBindingDemos
{
public class IntToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (int)value != 0;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? 1 : 0;
}
}
}
Loading

0 comments on commit 4bbedc9

Please sign in to comment.