Skip to content

Commit eb36931

Browse files
committed
#37 Bindable Margins for UWP. Updated sample app
1 parent 8948efa commit eb36931

File tree

6 files changed

+176
-40
lines changed

6 files changed

+176
-40
lines changed

Source/Plugin.Badge.Abstractions/TabBadge.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ public static Thickness GetDefaultMargins()
8282
{
8383
case Device.Android:
8484
return new Thickness(-10, -5);
85-
case Device.iOS:
8685
case Device.Windows:
8786
return new Thickness(0);
87+
case Device.iOS:
88+
return new Thickness(0);
8889
}
8990

9091
return new Thickness(0);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Plugin.Badge.Abstractions;
2+
using System;
3+
using Xamarin.Forms;
4+
5+
namespace Plugin.Badge.UWP
6+
{
7+
public class BadgeMarginConverter : Windows.UI.Xaml.Data.IValueConverter
8+
{
9+
10+
public object Convert(object value, Type targetType, object parameter, string language)
11+
{
12+
var element = value as Element;
13+
if (element != null)
14+
{
15+
var margin = TabBadge.GetBadgeMargin(element);
16+
return new Windows.UI.Xaml.Thickness(margin.Left, margin.Top, margin.Right, margin.Bottom);
17+
}
18+
19+
return null;
20+
}
21+
22+
public object ConvertBack(object value, Type targetType, object parameter, string language)
23+
{
24+
throw new NotImplementedException();
25+
}
26+
}
27+
}

Source/Plugin.Badge.UWP/HeaderTemplate.xaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<local:TabIconConverter x:Key="TabIconConverter" />
2020
<local:NullToVisibilityConverter x:Key="NullToVisibilityConverter" />
2121
<local:BadgePositionConverter x:Key="BadgePositionConverter" />
22+
<local:BadgeMarginConverter x:Key="BadgeMarginConverter" />
2223
<DataTemplate x:Key="HeaderTemplate">
2324
<RelativePanel>
2425
<Image x:Name="icon"
@@ -34,6 +35,7 @@
3435
Text="{Binding Title}"
3536
Margin="10,0"
3637
Visibility="{Binding ElementName=icon, Path=Source, Converter={StaticResource NullToVisibilityConverter}}" />
38+
<!---->
3739
<Grid CornerRadius="11"
3840
Visibility="{Binding Converter={StaticResource BadgeVisibilityConverter}}"
3941
Background="{Binding Converter={StaticResource BadgeColorConverter}}"
@@ -43,7 +45,8 @@
4345
RelativePanel.AlignLeftWithPanel="{Binding Converter={StaticResource BadgePositionConverter}, ConverterParameter=AlignLeftWithPanel}"
4446
RelativePanel.AlignHorizontalCenterWithPanel="{Binding Converter={StaticResource BadgePositionConverter}, ConverterParameter=AlignHorizontalCenterWithPanel}"
4547
RelativePanel.AlignVerticalCenterWithPanel="{Binding Converter={StaticResource BadgePositionConverter}, ConverterParameter=AlignVerticalCenterWithPanel}"
46-
Padding="2.5,0.5,3,1.5">
48+
Padding="2.5,0.5,3,1.5"
49+
Margin="{Binding Converter={StaticResource BadgeMarginConverter}}">
4750
<TextBlock Text="99"
4851
Opacity="0"
4952
FontSize="{Binding Converter={StaticResource BadgeFontSizeConverter}}" />

Source/Plugin.Badge.UWP/Plugin.Badge.UWP.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
</PropertyGroup>
108108
<ItemGroup>
109109
<Compile Include="BadgedTabbedPageRenderer.cs" />
110+
<Compile Include="Converters\BadgeMarginConverter.cs" />
110111
<Compile Include="Converters\BadgePositionConverter.cs" />
111112
<Compile Include="Converters\BadgeColorConverter.cs" />
112113
<Compile Include="Converters\BadgeFontSizeConverter.cs" />

Source/Sample/Plugin.Badge.Sample/Plugin.Badge.Sample.cs

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -102,55 +102,108 @@ private ContentPage CreateTab1()
102102
new Label
103103
{
104104
HorizontalTextAlignment = TextAlignment.Center,
105-
Text = "Welcome to Xamarin Forms Tab1!"
105+
Text = "Xamarin Forms Tab Badge Sample",
106+
FontSize = 14,
107+
FontAttributes = FontAttributes.Bold
106108
},
107109
}
108110
};
109111

110-
var buttonIncrement = new Button { Text = "Increment" };
111-
buttonIncrement.SetBinding(Button.CommandProperty, "IncrementCommand");
112-
var buttonDecrement = new Button { Text = "Decrement" };
113-
buttonDecrement.SetBinding(Button.CommandProperty, "DecrementCommand");
114-
115-
tab1Layout.Children.Add(buttonIncrement);
116-
tab1Layout.Children.Add(buttonDecrement);
112+
var stepper = new Stepper
113+
{
114+
Increment = 1,
115+
Maximum = 111,
116+
Minimum = 0,
117+
VerticalOptions = LayoutOptions.Center
118+
};
119+
120+
stepper.SetBinding(Stepper.ValueProperty, nameof(Tab1ViewModel.CountValue), BindingMode.TwoWay);
121+
122+
var grid = new Grid();
123+
grid.ColumnDefinitions.Add(new ColumnDefinition());
124+
grid.ColumnDefinitions.Add(new ColumnDefinition());
125+
grid.Children.Add(new Label { Text = "Increment / Decrement Value: ", HorizontalTextAlignment = TextAlignment.End, VerticalTextAlignment = TextAlignment.Center }, 0, 0);
126+
grid.Children.Add(stepper, 1, 0);
127+
tab1Layout.Children.Add(grid);
117128

118129
var buttonChangeColor = new Button { Text = "Change Color" };
119-
buttonChangeColor.SetBinding(Button.CommandProperty, "ChangeColorCommand");
130+
buttonChangeColor.SetBinding(Button.CommandProperty, nameof(Tab1ViewModel.ChangeColorCommand));
120131
tab1Layout.Children.Add(buttonChangeColor);
121132

122133
var buttonChangeTextColor = new Button { Text = "Change Text Color" };
123-
buttonChangeTextColor.SetBinding(Button.CommandProperty, "ChangeTextColorCommand");
134+
buttonChangeTextColor.SetBinding(Button.CommandProperty, nameof(Tab1ViewModel.ChangeTextColorCommand));
124135
tab1Layout.Children.Add(buttonChangeTextColor);
125136

126137
var buttonChangeFontAttributes = new Button { Text = "Change Font Attributes" };
127-
buttonChangeFontAttributes.SetBinding(Button.CommandProperty, "ChangeFontAttributesCommand");
138+
buttonChangeFontAttributes.SetBinding(Button.CommandProperty, nameof(Tab1ViewModel.ChangeFontAttributesCommand));
128139
tab1Layout.Children.Add(buttonChangeFontAttributes);
129140

130141
var buttonChangePosition = new Button { Text = "Change Position" };
131-
buttonChangePosition.SetBinding(Button.CommandProperty, "ChangePositionCommand");
142+
buttonChangePosition.SetBinding(Button.CommandProperty, nameof(Tab1ViewModel.ChangePositionCommand));
132143
tab1Layout.Children.Add(buttonChangePosition);
133144

134145
var buttonAddTab = new Button() { Text = "Add tab" };
135146
buttonAddTab.Clicked += ButtonAddTab_Clicked;
136-
tab1Layout.Children.Add(buttonAddTab);
137-
138-
139147
var buttonRemoveTab = new Button() { Text = "Remove tab" };
140-
buttonRemoveTab.Clicked += ButtonRemoveTab_Clicked; ;
141-
tab1Layout.Children.Add(buttonRemoveTab);
148+
buttonRemoveTab.Clicked += ButtonRemoveTab_Clicked;
149+
150+
grid = new Grid();
151+
grid.ColumnDefinitions.Add(new ColumnDefinition());
152+
grid.ColumnDefinitions.Add(new ColumnDefinition());
153+
grid.Children.Add(buttonAddTab, 0, 0);
154+
grid.Children.Add(buttonRemoveTab, 1, 0);
155+
tab1Layout.Children.Add(grid);
156+
157+
grid = new Grid { RowSpacing = 0 };
158+
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
159+
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
160+
grid.ColumnDefinitions.Add(new ColumnDefinition());
161+
grid.ColumnDefinitions.Add(new ColumnDefinition());
162+
grid.ColumnDefinitions.Add(new ColumnDefinition());
163+
grid.ColumnDefinitions.Add(new ColumnDefinition());
164+
165+
var leftStepper = new Stepper { Increment = 1, Minimum = -50, Maximum = 50 };
166+
leftStepper.SetBinding(Stepper.ValueProperty, nameof(Tab1ViewModel.MarginLeft), BindingMode.TwoWay);
167+
var leftMarginLabel = new Label();
168+
leftMarginLabel.SetBinding(Label.TextProperty, nameof(Tab1ViewModel.MarginLeft), stringFormat: "Left: {0}");
169+
grid.Children.Add(leftMarginLabel, 0, 0);
170+
grid.Children.Add(leftStepper, 0, 1);
171+
172+
var topStepper = new Stepper { Increment = 1, Minimum = -50, Maximum = 50 };
173+
topStepper.SetBinding(Stepper.ValueProperty, nameof(Tab1ViewModel.MarginTop), BindingMode.TwoWay);
174+
var topMarginLabel = new Label();
175+
topMarginLabel.SetBinding(Label.TextProperty, nameof(Tab1ViewModel.MarginTop), stringFormat: "Top: {0}");
176+
grid.Children.Add(topMarginLabel, 1, 0);
177+
grid.Children.Add(topStepper, 1, 1);
178+
179+
var rightStepper = new Stepper { Increment = 1, Minimum = -50, Maximum = 50 };
180+
rightStepper.SetBinding(Stepper.ValueProperty, nameof(Tab1ViewModel.MarginRight), BindingMode.TwoWay);
181+
var rightMarginLabel = new Label();
182+
rightMarginLabel.SetBinding(Label.TextProperty, nameof(Tab1ViewModel.MarginRight), stringFormat: "Right: {0}");
183+
grid.Children.Add(rightMarginLabel, 2, 0);
184+
grid.Children.Add(rightStepper, 2, 1);
185+
186+
var bottomStepper = new Stepper { Increment = 1, Minimum = -50, Maximum = 50 };
187+
bottomStepper.SetBinding(Stepper.ValueProperty, nameof(Tab1ViewModel.MarginBottom), BindingMode.TwoWay);
188+
var bottomMarginLabel = new Label();
189+
bottomMarginLabel.SetBinding(Label.TextProperty, nameof(Tab1ViewModel.MarginBottom), stringFormat: "Bottom: {0}");
190+
grid.Children.Add(bottomMarginLabel, 3, 0);
191+
grid.Children.Add(bottomStepper, 3, 1);
192+
193+
tab1Layout.Children.Add(grid);
142194

143195
var tab1 = new ContentPage
144196
{
145197
Title = "Tab1",
146198
Content = tab1Layout
147199
};
148200

149-
tab1.SetBinding(TabBadge.BadgeTextProperty, new Binding("Count"));
150-
tab1.SetBinding(TabBadge.BadgeColorProperty, new Binding("BadgeColor"));
151-
tab1.SetBinding(TabBadge.BadgeTextColorProperty, new Binding("BadgeTextColor"));
152-
tab1.SetBinding(TabBadge.BadgeFontProperty, new Binding("BadgeFont"));
153-
tab1.SetBinding(TabBadge.BadgePositionProperty, new Binding("Position"));
201+
tab1.SetBinding(TabBadge.BadgeTextProperty, nameof(Tab1ViewModel.Count));
202+
tab1.SetBinding(TabBadge.BadgeColorProperty, nameof(Tab1ViewModel.BadgeColor));
203+
tab1.SetBinding(TabBadge.BadgeTextColorProperty, nameof(Tab1ViewModel.BadgeTextColor));
204+
tab1.SetBinding(TabBadge.BadgeFontProperty, nameof(Tab1ViewModel.BadgeFont));
205+
tab1.SetBinding(TabBadge.BadgePositionProperty, nameof(Tab1ViewModel.Position));
206+
tab1.SetBinding(TabBadge.BadgeMarginProperty, nameof(Tab1ViewModel.Margin));
154207

155208
tab1.BindingContext = new Tab1ViewModel();
156209
return tab1;
@@ -205,3 +258,4 @@ protected override void OnResume()
205258
}
206259
}
207260
}
261+

Source/Sample/Plugin.Badge.Sample/ViewModels/Tab1ViewModel.cs

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class Tab1ViewModel : INotifyPropertyChanged
4343
};
4444

4545
private static readonly List<BadgePosition> Positions = Enum.GetValues(typeof(BadgePosition)).Cast<BadgePosition>().ToList();
46-
46+
4747
public Color BadgeColor { get; private set; }
4848
public Color BadgeTextColor { get; private set; }
4949

@@ -67,20 +67,6 @@ public class Tab1ViewModel : INotifyPropertyChanged
6767
RaisePropertyChanged(nameof(BadgeTextColor));
6868
});
6969

70-
public ICommand IncrementCommand => new Command((obj) =>
71-
{
72-
_count++;
73-
RaisePropertyChanged(nameof(Count));
74-
});
75-
76-
public ICommand DecrementCommand => new Command((obj) =>
77-
{
78-
_count--;
79-
if (_count < 0)
80-
_count = 0;
81-
RaisePropertyChanged(nameof(Count));
82-
});
83-
8470
public ICommand ChangeFontAttributesCommand => new Command((obj) =>
8571
{
8672
_fontIndex++;
@@ -99,17 +85,81 @@ public class Tab1ViewModel : INotifyPropertyChanged
9985
RaisePropertyChanged(nameof(Position));
10086
});
10187

102-
private int _count = 0;
10388
private int _color = 0;
89+
private int _count = 0;
10490
private int _textColor = 0;
10591
private int _fontIndex = 0;
10692
private int _positionIndex = 0;
10793

10894
public string Count => _count <= 0 ? string.Empty : _count.ToString();
10995

96+
public int CountValue
97+
{
98+
get { return _count; }
99+
set
100+
{
101+
if (_count == value)
102+
return;
103+
104+
_count = value;
105+
RaisePropertyChanged(nameof(CountValue));
106+
RaisePropertyChanged(nameof(Count));
107+
}
108+
}
109+
110110
public Font BadgeFont { get; private set; }
111111
public BadgePosition Position { get; private set; }
112112

113+
private int _marginLeft = (int)TabBadge.GetDefaultMargins().Left;
114+
public int MarginLeft
115+
{
116+
get { return _marginLeft; }
117+
set
118+
{
119+
_marginLeft = value;
120+
RaisePropertyChanged(nameof(MarginLeft));
121+
RaisePropertyChanged(nameof(Margin));
122+
}
123+
}
124+
125+
private int _marginTop = (int)TabBadge.GetDefaultMargins().Top;
126+
public int MarginTop
127+
{
128+
get { return _marginTop; }
129+
set
130+
{
131+
_marginTop = value;
132+
RaisePropertyChanged(nameof(MarginTop));
133+
RaisePropertyChanged(nameof(Margin));
134+
}
135+
}
136+
137+
private int _marginRight = (int)TabBadge.GetDefaultMargins().Right;
138+
public int MarginRight
139+
{
140+
get { return _marginRight; }
141+
set
142+
{
143+
_marginRight = value;
144+
RaisePropertyChanged(nameof(MarginRight));
145+
RaisePropertyChanged(nameof(Margin));
146+
}
147+
}
148+
149+
private int _marginBottom = (int)TabBadge.GetDefaultMargins().Bottom;
150+
public int MarginBottom
151+
{
152+
get { return _marginBottom; }
153+
set
154+
{
155+
_marginBottom = value;
156+
RaisePropertyChanged(nameof(MarginBottom));
157+
RaisePropertyChanged(nameof(Margin));
158+
}
159+
}
160+
161+
public Thickness Margin => new Thickness(_marginLeft, _marginTop, _marginRight, _marginBottom);
162+
113163
public event PropertyChangedEventHandler PropertyChanged;
114164

115165
protected virtual void RaisePropertyChanged(string propertyName)

0 commit comments

Comments
 (0)