Skip to content

Commit a08acb5

Browse files
committed
Run OnParameterSet when navigating even if there are no parameters set, to align how it works in Blazor
1 parent 8f132bf commit a08acb5

File tree

6 files changed

+40
-3
lines changed

6 files changed

+40
-3
lines changed

src/TinyMvvm.Maui/TinyNavigation.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections;
2+
using System.Reflection.Metadata;
23

34
namespace TinyMvvm;
45

@@ -14,7 +15,12 @@ public TinyNavigation()
1415
/// <inheritdoc />
1516
public async Task NavigateTo(string key)
1617
{
17-
await Shell.Current.GoToAsync(key);
18+
var parameters = new Dictionary<string, object>
19+
{
20+
{ "tinyEmpty", string.Empty }
21+
};
22+
23+
await Shell.Current.GoToAsync(key, parameters);
1824

1925
}
2026

@@ -27,8 +33,10 @@ public async Task NavigateTo(string key, object parameter)
2733
return;
2834
}
2935

30-
var parameters = new Dictionary<string, object>();
31-
parameters.Add("tinyParameter", parameter);
36+
var parameters = new Dictionary<string, object>
37+
{
38+
{ "tinyParameter", parameter }
39+
};
3240

3341
await Shell.Current.GoToAsync(key, parameters);
3442
}

src/TinyMvvm.Maui/TinyViewModel.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public bool IsNotInitialized
115115
}
116116

117117
private const string TinyParameterKey = "tinyParameter";
118+
private const string TinyEmptyKey = "tinyEmpty";
118119
/// <inheritdoc />
119120
public async void ApplyQueryAttributes(IDictionary<string, object> query)
120121
{
@@ -123,6 +124,12 @@ public async void ApplyQueryAttributes(IDictionary<string, object> query)
123124
return;
124125
}
125126

127+
if(query.ContainsKey(TinyEmptyKey))
128+
{
129+
await RunOnParameterSet();
130+
return;
131+
}
132+
126133
if (query.ContainsKey(TinyParameterKey))
127134
{
128135
NavigationParameter = query[TinyParameterKey];
@@ -149,6 +156,11 @@ public async void ApplyQueryAttributes(IDictionary<string, object> query)
149156
QueryParameters = query;
150157
}
151158

159+
await RunOnParameterSet();
160+
}
161+
162+
private async Task RunOnParameterSet()
163+
{
152164
if (TinyDispatcher.IsMainThread)
153165
{
154166
await OnParameterSet();

src/TinyMvvm.Sample/MauiProgram.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static MauiApp CreateMauiApp()
3535
builder.Services.AddTransient<DetailsViewModel>();
3636

3737
Routing.RegisterRoute(nameof(DetailsViewModel), typeof(DetailsView));
38+
Routing.RegisterRoute(nameof(ListViewModel), typeof(ListView));
3839

3940
return builder.Build();
4041
}

src/TinyMvvm.Sample/ViewModels/ListViewModel.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public override async Task Initialize()
2626
IsBusy = false;
2727
}
2828

29+
public override async Task OnParameterSet()
30+
{
31+
await base.OnParameterSet();
32+
}
33+
2934
[ObservableProperty]
3035
private ObservableCollection<City> cities = new ObservableCollection<City>();
3136

src/TinyMvvm.Sample/ViewModels/MainViewModel.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ public MainViewModel(ICityService cityService)
3434
IsBusy = false;
3535
});
3636

37+
private ICommand showList;
38+
public ICommand ShowList => showList ??= new RelayCommand(async () =>
39+
{
40+
IsBusy = true;
41+
42+
await Navigation.NavigateTo(nameof(ListViewModel));
43+
44+
IsBusy = false;
45+
});
46+
3747
private ICommand show;
3848
public ICommand Show => show ??= new RelayCommand<City>(async (city) =>
3949
{

src/TinyMvvm.Sample/Views/MainView.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<VerticalStackLayout Spacing="10">
99
<Entry Text="{Binding Text}" Placeholder="Search city" />
1010
<Button Command="{Binding Search}" Text="Search" />
11+
<Button Command="{Binding ShowList}" Text="ShowList" />
1112
</VerticalStackLayout>
1213
<ActivityIndicator Grid.Row="1" HorizontalOptions="Center" VerticalOptions="Center" IsRunning="{Binding IsBusy}" IsVisible="{Binding IsBusy}" />
1314
<CollectionView Grid.Row="1" x:Name="CityList" ItemsSource="{Binding Cities}" IsVisible="{Binding IsNotBusy}">

0 commit comments

Comments
 (0)