Skip to content

Commit

Permalink
Added virtual Initialize method for TinyApplication.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhindrik committed Mar 12, 2023
1 parent 7b27ad2 commit c00d91e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 7 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ public partial class App : TinyApplication
}
```

If you want to run code asynchronous when the app starts you can override the **Initialize** method of **TinyApplication**.

```csharp
public App()
{
InitializeComponent();

MainPage = new AppShell();
}

protected override async Task Initialize()
{

}
```

### TinyView
TinyMvvm has a base view called **TinyView** that you need to use if you want to use the overides from **TinyViewModel**. TinyView has ContentPage as it's base class so you can use it exactly as a normal ContentPage.

Expand Down
7 changes: 7 additions & 0 deletions src/TinyMvvm.Maui/TinyApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ event EventHandler ITinyApplication.ApplicationSleep
public TinyApplication()
{
ApplicationResolver.Current = this;

TinyDispatcher.BeginInvokeOnMainThread(async () => await Initialize());
}

protected virtual Task Initialize()
{
return Task.CompletedTask;
}

protected override void OnResume()
Expand Down
3 changes: 1 addition & 2 deletions src/TinyMvvm.Maui/TinyDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
namespace TinyMvvm.Maui;
namespace TinyMvvm;

internal static class TinyDispatcher
{
Expand Down
4 changes: 1 addition & 3 deletions src/TinyMvvm.Maui/TinyView.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using TinyMvvm.Maui;

namespace TinyMvvm;
namespace TinyMvvm;

public abstract class TinyView : ContentPage
{
Expand Down
1 change: 0 additions & 1 deletion src/TinyMvvm.Maui/TinyViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CommunityToolkit.Mvvm.ComponentModel;
using TinyMvvm.Maui;

namespace TinyMvvm;

Expand Down
12 changes: 12 additions & 0 deletions src/TinyMvvm.Sample/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,17 @@ public App()

MainPage = new AppShell();
}

protected override async Task Initialize()
{
await base.Initialize();


//To test that it not hangs the application.
for(int i = 0; i < 100; i++)
{
await Task.Delay(1000);
}
}
}

2 changes: 1 addition & 1 deletion src/TinyMvvm.Sample/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public MainViewModel(ICityService cityService)
private ICommand show;
public ICommand Show => show ??= new RelayCommand<City>(async (city) =>
{
await Navigation.NavigateTo($"{nameof(DetailsViewModel)}", city.Country);
await Navigation.NavigateTo($"{nameof(DetailsViewModel)}", city.Name);
});

[ObservableProperty]
Expand Down

0 comments on commit c00d91e

Please sign in to comment.