【问题标题】:How to open url's in ListView with OnItemTapped - Xamarin如何使用 OnItemTapped 在 ListView 中打开 url - Xamarin
【发布时间】:2022-01-20 05:57:07
【问题描述】:

我有一个带有 TitleUrl 的 ListView。

我想当用户点击url时用浏览器打开当前url。

我的列表视图:

  <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);
            }        
        }
    }
}

我想点击grid.row 1“UrlNews”上的标签并打开当前url。

【问题讨论】:

标签: c# xamarin


【解决方案1】:

使用ItemTapped

<ListView ItemTapped="LoadNews" ... />

在你的代码后面

protected void LoadNews(object sender, ItemTappedEventArgs args)
{
  var item = (MyItemClass)args.Item;
  Launcher.OpenAsync(item.UrlNews);
}

【讨论】:

  • 由于我的代码不起作用,您能检查一下我在 ListView 后面的代码吗?
  • 为什么当他们点击一个项目时你打开列表中的每一个项目?
  • 我该怎么做?
  • 您为什么要这样做?我不认为该设备真的支持一次打开多个网页的方法
  • 我只想打开每行点击链接Grid.Row=1时点击的链接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
  • 2015-10-30
  • 2020-06-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-18
相关资源
最近更新 更多