-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainPage.xaml.cs
42 lines (36 loc) · 1.35 KB
/
MainPage.xaml.cs
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
namespace HackerNews;
public partial class MainPage : ContentPage
{
private readonly NewsViewModel _newsViewModel;
// Constructor accepting NewsViewModel via Dependency Injection
public MainPage(NewsViewModel newsViewModel)
{
InitializeComponent();
_newsViewModel = newsViewModel;
// Set the BindingContext for data binding in XAML
BindingContext = _newsViewModel;
}
protected override async void OnAppearing()
{
base.OnAppearing();
await _newsViewModel.RefreshAsync();
}
private async void NewsCollectionView_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Check if there is a selected item and access it directly by index
if (e.CurrentSelection.Count > 0 && e.CurrentSelection[0] is StoryModel storyModel)
{
// Clear selection
((CollectionView)sender).SelectedItem = null;
if (!string.IsNullOrEmpty(storyModel.Url))
{
var browserOptions = new BrowserLaunchOptions { LaunchMode = BrowserLaunchMode.SystemPreferred };
await Browser.Default.OpenAsync(storyModel.Url, browserOptions);
}
else
{
await DisplayAlert("Invalid Article", "ASK HN articles have no URL", "OK");
}
}
}
}