I have a ListView with Title
and Url
.
I want when users click on the url to open the current url with browser.
My ListView:
<StackLayout>
<ListView x:Name="lstNews" HasUnevenRows="True" ItemTapped="LoadNews">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid BackgroundColor="#454545">
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="2"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Text="{Binding TitleNews}" XAlign="Center" YAlign="Center" TextColor="#2bff00" FontAttributes="Bold" FontSize="Large" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"/>
<Label Grid.Row="1" Grid.Column="0" Text="{Binding UrlNews}" XAlign="Center" YAlign="Center" TextColor="White" FontAttributes="Bold" FontSize="Small" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"/>
<BoxView Color="#2bff00" HeightRequest="1" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"/>
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SmartCryptoWorld.Models;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace SmartCryptoWorld
{
public partial class MainPage : ContentPage
{
public List<NewsBody> NewsList = new List<NewsBody>()
{
};
public MainPage()
{
InitializeComponent();
}
private async Task GetAPI()
{
var client = new HttpClient();
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("https://crypto-news-live.p.rapidapi.com/news/coindesk"),
Headers =
{
{ "x-rapidapi-host", "crypto-news-live.p.rapidapi.com" },
{ "x-rapidapi-key", "51569aba99mshf9e839fcfce791bp16c0dbjsn9ced6dba7472" },
},
};
using (var response = await client.SendAsync(request))
{
var news = new News();
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
var newsBody = JsonConvert.DeserializeObject<List<NewsBody>>(body);
news.CryptoNews = newsBody;
lstNews.ItemsSource = newsBody;
newsBody = NewsList;
}
}
void ButtonInfo(System.Object sender, System.EventArgs e)
{
_ = GetAPI();
}
void LoadNews(System.Object sender, Xamarin.Forms.ItemTappedEventArgs e)
{
foreach (var item in NewsList)
{
Launcher.OpenAsync(item.UrlNews);
}
}
}
}
I want to tap on the Label on grid.row 1 "UrlNews" to be tapped and open the current url.
CodePudding user response:
use ItemTapped
<ListView ItemTapped="LoadNews" ... />
in your code behind
protected void LoadNews(object sender, ItemTappedEventArgs args)
{
var item = (MyItemClass)args.Item;
Launcher.OpenAsync(item.UrlNews);
}