Skip to content

Commit

Permalink
Switch Case and NumericUpDown
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavAntonyuk committed Jan 17, 2025
1 parent fb792bf commit c82e7f8
Show file tree
Hide file tree
Showing 11 changed files with 372 additions and 200 deletions.
2 changes: 1 addition & 1 deletion MauiConditionView/ConditionView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class ConditionView : ContentView
{
public static readonly BindableProperty FalseProperty = BindableProperty.Create(nameof(False), typeof(View), typeof(ConditionView), default(View));
public static readonly BindableProperty TrueProperty = BindableProperty.Create(nameof(True), typeof(View), typeof(ConditionView), default(View));
public static readonly BindableProperty IfProperty = BindableProperty.Create(nameof(If), typeof(bool), typeof(ConditionView), default(bool), propertyChanged:ConditionChanged);
public static readonly BindableProperty IfProperty = BindableProperty.Create(nameof(If), typeof(bool), typeof(ConditionView), default(bool), propertyChanged: ConditionChanged);

private static void ConditionChanged(BindableObject bindable, object oldvalue, object newvalue)
{
Expand Down
51 changes: 43 additions & 8 deletions MauiConditionView/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mauiConditionView="using:MauiConditionView"
xmlns:system="clr-namespace:System;assembly=System.Runtime"
x:Class="MauiConditionView.MainPage"
x:DataType="mauiConditionView:MainPage">

Expand All @@ -13,10 +14,17 @@
Padding="30,0"
Spacing="25">

<mauiConditionView:NumericUpDown
Value="{Binding Count}"
Minimum="0"
Maximum="6"
Increment="1"
HorizontalOptions="Center" />

<mauiConditionView:ConditionView If="{Binding Count, Converter={StaticResource CountToBoolConverter}}">
<mauiConditionView:ConditionView.True>
<Label
Text="Hello, World!"
Text="If Condition is TRUE"
Style="{StaticResource Headline}"
SemanticProperties.HeadingLevel="Level1" />
</mauiConditionView:ConditionView.True>
Expand All @@ -29,20 +37,47 @@
SemanticProperties.Description="dot net bot in a hovercraft number nine" />

<Label
Text="Welcome to &#10;.NET Multi-platform App UI"
Text="IF Condition is False"
Style="{StaticResource SubHeadline}"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I" />
</VerticalStackLayout>
</mauiConditionView:ConditionView.False>
</mauiConditionView:ConditionView>

<Button
x:Name="CounterBtn"
Text="Click me"
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked"
HorizontalOptions="Fill" />
<mauiConditionView:SwitchCaseView x:TypeArguments="system:Int32" Switch="{Binding Count}">
<mauiConditionView:SwitchCaseView.Conditions>
<mauiConditionView:CaseView x:TypeArguments="system:Int32" Case="1">
<Label
Text="Case 1"
Style="{StaticResource Headline}"
SemanticProperties.HeadingLevel="Level1" />
</mauiConditionView:CaseView>
<mauiConditionView:CaseView x:TypeArguments="system:Int32" Case="3">
<VerticalStackLayout>
<Label
Text="Case 3"
Style="{StaticResource Headline}"
SemanticProperties.HeadingLevel="Level1" />
<Image
Source="dotnet_bot.png"
HeightRequest="185"
Aspect="AspectFit"
SemanticProperties.Description="dot net bot in a hovercraft number nine" />
</VerticalStackLayout>
</mauiConditionView:CaseView>
</mauiConditionView:SwitchCaseView.Conditions>
<mauiConditionView:SwitchCaseView.Default>
<VerticalStackLayout>
<Label
Text="Case Default"
Style="{StaticResource SubHeadline}"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I" />
</VerticalStackLayout>
</mauiConditionView:SwitchCaseView.Default>
</mauiConditionView:SwitchCaseView>

</VerticalStackLayout>
</ScrollView>

Expand Down
10 changes: 0 additions & 10 deletions MauiConditionView/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,9 @@ public int Count
OnPropertyChanged();
}
}

public MainPage()
{
InitializeComponent();
BindingContext = this;
}

private void OnCounterClicked(object sender, EventArgs e)
{
Count++;

CounterBtn.Text = Count == 1 ? $"Clicked {Count} time" : $"Clicked {Count} times";

SemanticScreenReader.Announce(CounterBtn.Text);
}
}
21 changes: 21 additions & 0 deletions MauiConditionView/NumericUpDown.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>

<HorizontalStackLayout xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiConditionView.NumericUpDown">
<Button Text="-"
Clicked="DecrementClicked"
Margin="0,0,5,0"
HeightRequest="40"
WidthRequest="40" />

<Label x:Name="ValueControl"
VerticalOptions="Center"
HorizontalOptions="Center"/>

<Button Text="+"
Clicked="IncrementClicked"
Margin="5,0,0,0"
HeightRequest="40"
WidthRequest="40" />
</HorizontalStackLayout>
59 changes: 59 additions & 0 deletions MauiConditionView/NumericUpDown.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
namespace MauiConditionView;

using System.Globalization;

public partial class NumericUpDown
{
public NumericUpDown()
{
InitializeComponent();
ValueControl.Text = Value.ToString(CultureInfo.CurrentCulture);
}

public static readonly BindableProperty ValueProperty = BindableProperty.Create(nameof(Value), typeof(double), typeof(NumericUpDown), 0.0, BindingMode.TwoWay);
public static readonly BindableProperty MinimumProperty = BindableProperty.Create(nameof(Minimum), typeof(double), typeof(NumericUpDown), 0.0);
public static readonly BindableProperty MaximumProperty = BindableProperty.Create(nameof(Maximum), typeof(double), typeof(NumericUpDown), 100.0);
public static readonly BindableProperty IncrementProperty = BindableProperty.Create(nameof(Increment), typeof(double), typeof(NumericUpDown), 1.0);

public double Value
{
get => (double)GetValue(ValueProperty);
set
{
var newValue = Math.Clamp(value, Minimum, Maximum);
if (!newValue.Equals((double)GetValue(ValueProperty)))
{
SetValue(ValueProperty, newValue);
ValueControl.Text = newValue.ToString(CultureInfo.CurrentCulture);
}
}
}

public double Minimum
{
get => (double)GetValue(MinimumProperty);
set => SetValue(MinimumProperty, value);
}

public double Maximum
{
get => (double)GetValue(MaximumProperty);
set => SetValue(MaximumProperty, value);
}

public double Increment
{
get => (double)GetValue(IncrementProperty);
set => SetValue(IncrementProperty, value);
}

private void IncrementClicked(object sender, EventArgs e)
{
Value += Increment;
}

private void DecrementClicked(object sender, EventArgs e)
{
Value -= Increment;
}
}
7 changes: 7 additions & 0 deletions MauiConditionView/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# .NET MAUI ConditionView

[![Buy Me A Coffee](https://ik.imagekit.io/VladislavAntonyuk/vladislavantonyuk/misc/bmc-button.png)](https://www.buymeacoffee.com/vlad.antonyuk)

Article: https://vladislavantonyuk.github.io/articles/Building-Dynamic-UI-with-Decision-Logic-in-.NET-MAUI-XAML

[![Stand With Ukraine](https://img.shields.io/badge/made_in-ukraine-ffd700.svg?labelColor=0057b7)](https://stand-with-ukraine.pp.ua)
7 changes: 7 additions & 0 deletions MauiConditionView/README.mdpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# .NET MAUI ConditionView

!INCLUDE "./md/header.mdpp"

Article: https://vladislavantonyuk.github.io/articles/Building-Dynamic-UI-with-Decision-Logic-in-.NET-MAUI-XAML

!INCLUDE "./md/footer.mdpp"
46 changes: 46 additions & 0 deletions MauiConditionView/SwitchCaseView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace MauiConditionView;

public class SwitchCaseView<T> : ContentView
where T : notnull
{
public static readonly BindableProperty ConditionsProperty = BindableProperty.Create(nameof(Conditions), typeof(ICollection<CaseView<T>>), typeof(ConditionView), new List<CaseView<T>>(), propertyChanged:SwitchChanged);
public static readonly BindableProperty DefaultProperty = BindableProperty.Create(nameof(Default), typeof(View), typeof(ConditionView), propertyChanged: SwitchChanged);
public static readonly BindableProperty SwitchProperty = BindableProperty.Create(nameof(Switch), typeof(T), typeof(ConditionView), propertyChanged: SwitchChanged);

private static void SwitchChanged(BindableObject bindable, object oldvalue, object newvalue)
{
var switchCaseView = (SwitchCaseView<T>)bindable;
switchCaseView.Content = switchCaseView.Conditions
.Where(x => x.Case.Equals(switchCaseView.Switch))
.Select(x => x.Content)
.SingleOrDefault(switchCaseView.Default);
}

public T Switch
{
get => (T)GetValue(SwitchProperty);
set => SetValue(SwitchProperty, value);
}

public View? Default
{
get => (View?)GetValue(DefaultProperty);
set => SetValue(DefaultProperty, value);
}

public ICollection<CaseView<T>> Conditions
{
get => (ICollection<CaseView<T>>)GetValue(ConditionsProperty);
set => SetValue(ConditionsProperty, value);
}
}

public class CaseView<T> : ContentView
{
public static readonly BindableProperty CaseProperty = BindableProperty.Create(nameof(Case), typeof(T), typeof(CaseView<T>));
public T Case
{
get => (T)GetValue(CaseProperty);
set => SetValue(CaseProperty, value);
}
}
Loading

0 comments on commit c82e7f8

Please sign in to comment.