Skip to content

Commit 1388a88

Browse files
committed
Add a way to change the culture of the calendar in playground page of sample app
1 parent 2094107 commit 1388a88

File tree

6 files changed

+125
-2
lines changed

6 files changed

+125
-2
lines changed

XCalendarFormsSample/XCalendarFormsSample/App.xaml.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Xamarin.Forms;
1+
using System.Globalization;
2+
using Xamarin.Forms;
23
using XCalendarFormsSample.Views;
34

45
namespace XCalendarFormsSample

XCalendarFormsSample/XCalendarFormsSample/ViewModels/PlaygroundViewModel.cs

+29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using System.Linq;
45
using System.Windows.Input;
56
using Xamarin.Forms;
@@ -46,6 +47,7 @@ public class PlaygroundViewModel : BaseViewModel
4647
public bool DayAutoSetStyleBasedOnDayState { get; set; } = true;
4748
public int ForwardsNavigationAmount { get; set; } = 1;
4849
public int BackwardsNavigationAmount { get; set; } = -1;
50+
public string TargetCultureCode { get; set; } = CultureInfo.CurrentCulture?.Name ?? CultureInfo.DefaultThreadCurrentCulture?.Name ?? CultureInfo.CurrentUICulture?.Name ?? CultureInfo.DefaultThreadCurrentUICulture?.Name ?? "en";
4951
public Color CalendarBackgroundColor { get; set; } = (Color)Application.Current.Resources["CalendarBackgroundColor"];
5052
public Color NavigationBackgroundColor { get; set; } = (Color)Application.Current.Resources["CalendarPrimaryColor"];
5153
public Color NavigationTextColor { get; set; } = (Color)Application.Current.Resources["CalendarPrimaryTextColor"];
@@ -89,6 +91,7 @@ public class PlaygroundViewModel : BaseViewModel
8991
public ICommand NavigateCalendarCommand { get; set; }
9092
public ICommand ChangeDateSelectionCommand { get; set; }
9193
public ICommand ChangeCalendarVisibilityCommand { get; set; }
94+
public ICommand UpdateCurrentCultureCommand { get; set; }
9295
#endregion
9396

9497
#region Constructors
@@ -119,6 +122,8 @@ public PlaygroundViewModel()
119122
NavigateCalendarCommand = new Command<int>(NavigateCalendar);
120123
ChangeDateSelectionCommand = new Command<DateTime>(ChangeDateSelection);
121124
ChangeCalendarVisibilityCommand = new Command<bool>(ChangeCalendarVisibility);
125+
UpdateCurrentCultureCommand = new Command(UpdateCurrentCulture);
126+
UpdateCurrentCulture();
122127
}
123128
#endregion
124129

@@ -187,6 +192,29 @@ public void ChangeCalendarVisibility(bool isVisible)
187192
{
188193
CalendarIsVisible = isVisible;
189194
}
195+
public async void UpdateCurrentCulture()
196+
{
197+
try
198+
{
199+
//Set DefaultThreadCurrentCulture because CurrentCulture gets automatically reset when changed.
200+
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(TargetCultureCode);
201+
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(TargetCultureCode);
202+
203+
//This causes the binding converters (which use the current culture) to update.
204+
//Day Names
205+
var oldDayNamesOlder = Calendar.DayNamesOrder.ToList();
206+
Calendar.DayNamesOrder.ReplaceRange(new List<DayOfWeek>() { DayOfWeek.Monday });
207+
Calendar.DayNamesOrder.ReplaceRange(oldDayNamesOlder);
208+
209+
//NavigationView Title
210+
NavigateCalendar(1);
211+
NavigateCalendar(-1);
212+
}
213+
catch
214+
{
215+
await Shell.Current.DisplayAlert("Invalid Culture Code", "The specified culture code was invalid.", "OK");
216+
}
217+
}
190218
public async void ShowCustomDayNamesOrderDialog()
191219
{
192220
IEnumerable<DayOfWeek> newCustomDayNamesOrder = await PopupHelper.ShowConstructListDialogAsync(Calendar.CustomDayNamesOrder ?? new ObservableRangeCollection<DayOfWeek>(), Calendar.StartOfWeek.GetWeekAsFirst());
@@ -291,6 +319,7 @@ public async void ShowDayInvalidTextColorDialog()
291319
{
292320
DayInvalidTextColor = await PopupHelper.ShowColorDialogAsync(DayInvalidTextColor);
293321
}
322+
294323
#endregion
295324
}
296325
}

XCalendarFormsSample/XCalendarFormsSample/Views/PlaygroundPage.xaml

+31
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,37 @@
471471
</Frame>
472472
</Grid>
473473

474+
<Grid ColumnDefinitions="*, *, *">
475+
<Label
476+
Grid.Column="0"
477+
FontSize="{StaticResource SmallFontSize}"
478+
HorizontalTextAlignment="Center"
479+
Text="Culture*"
480+
VerticalTextAlignment="Center"/>
481+
482+
<Grid
483+
Grid.Column="2"
484+
ColumnDefinitions="auto, auto"
485+
ColumnSpacing="5"
486+
HorizontalOptions="Center"
487+
VerticalOptions="Center">
488+
<Editor
489+
Grid.Column="0"
490+
Keyboard="Text"
491+
Text="{Binding TargetCultureCode}"
492+
VerticalOptions="Center"
493+
WidthRequest="75"/>
494+
495+
<Button
496+
Grid.Column="1"
497+
BackgroundColor="{StaticResource PrimaryColor}"
498+
Command="{Binding UpdateCurrentCultureCommand}"
499+
Text="Update"
500+
TextColor="{StaticResource PrimaryTextColor}"
501+
VerticalOptions="Center"/>
502+
</Grid>
503+
</Grid>
504+
474505
<Label
475506
BackgroundColor="{StaticResource PrimaryColor}"
476507
HorizontalTextAlignment="Center"

XCalendarMauiSample/ViewModels/PlaygroundViewModel.cs

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Windows.Input;
1+
using System.Globalization;
2+
using System.Windows.Input;
23
using XCalendar.Core.Collections;
34
using XCalendar.Core.Enums;
45
using XCalendar.Core.Extensions;
@@ -42,6 +43,7 @@ public class PlaygroundViewModel : BaseViewModel
4243
public bool DayAutoSetStyleBasedOnDayState { get; set; } = true;
4344
public int ForwardsNavigationAmount { get; set; } = 1;
4445
public int BackwardsNavigationAmount { get; set; } = -1;
46+
public string TargetCultureCode { get; set; } = CultureInfo.CurrentCulture?.Name ?? CultureInfo.DefaultThreadCurrentCulture?.Name ?? CultureInfo.CurrentUICulture?.Name ?? CultureInfo.DefaultThreadCurrentUICulture?.Name ?? "en";
4547
public Color CalendarBackgroundColor { get; set; } = (Color)Application.Current.Resources["CalendarBackgroundColor"];
4648
public Color NavigationBackgroundColor { get; set; } = (Color)Application.Current.Resources["CalendarPrimaryColor"];
4749
public Color NavigationTextColor { get; set; } = (Color)Application.Current.Resources["CalendarPrimaryTextColor"];
@@ -85,6 +87,7 @@ public class PlaygroundViewModel : BaseViewModel
8587
public ICommand NavigateCalendarCommand { get; set; }
8688
public ICommand ChangeDateSelectionCommand { get; set; }
8789
public ICommand ChangeCalendarVisibilityCommand { get; set; }
90+
public ICommand UpdateCurrentCultureCommand { get; set; }
8891
#endregion
8992

9093
#region Constructors
@@ -115,6 +118,8 @@ public PlaygroundViewModel()
115118
NavigateCalendarCommand = new Command<int>(NavigateCalendar);
116119
ChangeDateSelectionCommand = new Command<DateTime>(ChangeDateSelection);
117120
ChangeCalendarVisibilityCommand = new Command<bool>(ChangeCalendarVisibility);
121+
UpdateCurrentCultureCommand = new Command(UpdateCurrentCulture);
122+
UpdateCurrentCulture();
118123
}
119124
#endregion
120125

@@ -183,6 +188,29 @@ public void ChangeCalendarVisibility(bool isVisible)
183188
{
184189
CalendarIsVisible = isVisible;
185190
}
191+
public async void UpdateCurrentCulture()
192+
{
193+
try
194+
{
195+
//Set DefaultThreadCurrentCulture because CurrentCulture gets automatically reset when changed.
196+
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(TargetCultureCode);
197+
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(TargetCultureCode);
198+
199+
//This causes the binding converters (which use the current culture) to update.
200+
//Day Names
201+
var oldDayNamesOlder = Calendar.DayNamesOrder.ToList();
202+
Calendar.DayNamesOrder.ReplaceRange(new List<DayOfWeek>() { DayOfWeek.Monday });
203+
Calendar.DayNamesOrder.ReplaceRange(oldDayNamesOlder);
204+
205+
//NavigationView Title
206+
NavigateCalendar(1);
207+
NavigateCalendar(-1);
208+
}
209+
catch
210+
{
211+
await Shell.Current.DisplayAlert("Invalid Culture Code", "The specified culture code was invalid.", "OK");
212+
}
213+
}
186214
public async void ShowCustomDayNamesOrderDialog()
187215
{
188216
IEnumerable<DayOfWeek> newCustomDayNamesOrder = await PopupHelper.ShowConstructListDialogPopupAsync(Calendar.CustomDayNamesOrder ?? new ObservableRangeCollection<DayOfWeek>(), Calendar.StartOfWeek.GetWeekAsFirst());

XCalendarMauiSample/Views/PlaygroundPage.xaml

+30
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,36 @@
471471
</Border>
472472
</Grid>
473473

474+
<Grid ColumnDefinitions="*, *">
475+
<Label
476+
Grid.Column="0"
477+
FontSize="{StaticResource SmallFontSize}"
478+
HorizontalTextAlignment="Center"
479+
Text="Culture*"
480+
VerticalTextAlignment="Center"/>
481+
482+
<Grid
483+
Grid.Column="2"
484+
ColumnDefinitions="auto, auto"
485+
ColumnSpacing="5"
486+
HorizontalOptions="Center">
487+
<Editor
488+
Grid.Column="0"
489+
Keyboard="Text"
490+
Text="{Binding TargetCultureCode}"
491+
VerticalOptions="Center"
492+
WidthRequest="75"/>
493+
494+
<Button
495+
Grid.Column="1"
496+
BackgroundColor="{StaticResource PrimaryColor}"
497+
Command="{Binding UpdateCurrentCultureCommand}"
498+
Text="Update"
499+
TextColor="{StaticResource PrimaryTextColor}"
500+
VerticalOptions="Center"/>
501+
</Grid>
502+
</Grid>
503+
474504
<Label
475505
BackgroundColor="{StaticResource PrimaryColor}"
476506
HorizontalTextAlignment="Center"

XCalendarMauiSample/XCalendarMauiSample.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
<EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
3535
</PropertyGroup>
3636

37+
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-android|AnyCPU'">
38+
<EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
39+
</PropertyGroup>
40+
3741
<ItemGroup>
3842
<!-- App Icon -->
3943
<MauiIcon Include="Resources\appicon.svg" ForegroundFile="Resources\appiconfg.svg" Color="#512BD4" />

0 commit comments

Comments
 (0)