BinPickerPage.xaml.cs
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using IndustrialControl.Services;
using IndustrialControl.ViewModels;
namespace IndustrialControl.Pages;
public partial class BinPickerPage : ContentPage
{
private readonly TaskCompletionSource<BinInfo?> _tcs = new();
public BinPickerPage(string? preselectBinCode, BinPickerViewModel vm)
{
InitializeComponent();
BindingContext = vm;
vm.Initialize(preselectBinCode);
}
public static async Task<BinInfo?> ShowAsync(string? preselectBinCode)
{
var sp = Application.Current!.Handler!.MauiContext!.Services!;
var vm = sp.GetRequiredService<BinPickerViewModel>();
var page = new BinPickerPage(preselectBinCode, vm);
var nav = Shell.Current?.Navigation ?? Application.Current?.MainPage?.Navigation
?? throw new InvalidOperationException("Navigation not ready");
vm.SetCloseHandlers(
onPicked: async (bin) =>
{
page._tcs.TrySetResult(bin);
await MainThread.InvokeOnMainThreadAsync(async () =>
{
var nav = Shell.Current?.Navigation ?? Application.Current?.MainPage?.Navigation;
if (nav != null && nav.ModalStack.Count > 0 && nav.ModalStack.Last() is BinPickerPage)
await nav.PopModalAsync();
});
},
onCanceled: async () =>
{
page._tcs.TrySetResult(null);
await MainThread.InvokeOnMainThreadAsync(async () =>
{
var nav = Shell.Current?.Navigation ?? Application.Current?.MainPage?.Navigation;
if (nav != null && nav.ModalStack.Count > 0 && nav.ModalStack.Last() is BinPickerPage)
await nav.PopModalAsync();
});
});
await nav.PushModalAsync(page);
return await page._tcs.Task;
}
private async void OnCloseClicked(object? sender, EventArgs e)
{
if (BindingContext is BinPickerViewModel vm)
await vm.CancelAsync();
}
}