View.ContextActions无法触发Clicked背后的代码

时间:2019-01-27 07:18:53

标签: c# xaml xamarin

问题:

在遵循了几个示例onetwothree之后,我无法使ContextActions连接到我的后台代码。

这是来自示例。 MenuItem_Clicked没有将XAML连接到后面的代码

 public async Task<Response> SaveAsync(PurchaseReturn purchaseReturn, List<string> serialNoList)
    {
        PurchaseReturnRepository rep = new PurchaseReturnRepository();

        decimal totalRate = 0;
        foreach (var serialNo in serialNoList)
        {
            var purchaseDetail = await PurchaseService.Instance.GetSinglePurchasedItemDetailAsync(serialNo);

            purchaseReturn.PurchaseDetails.Add(purchaseDetail);

            //Calculating total rate to update supplier balance
            totalRate += purchaseDetail.Rate;


        }

        var response = await rep.AddAsync(purchaseReturn);

        if (response.Success)
        {
            //Updating Supplier Balance
            response = await SupplierService.Instance.UpdataBalanceAsync(purchaseReturn.SupplierId, totalRate, false);
            response.Msg = "Items has beed returned and stock is updated";
        }

        return response;
    }

设置:

我正在处理一个我在网上找到的基本示例。它不是MVVM。我已经设置了ListItems_Refresh操作并可以正常工作。

代码目标:

当用户单击一行时,我要打开默认浏览器并转到该行的URL。

我很想知道如何解决此问题,或者是否有明显的错字。预先感谢

代码:

XAML

<ViewCell.ContextActions>
    <MenuItem Clicked="MenuItem_Clicked" Text="TEST" Command="{Binding .}"/>
</ViewCell.ContextActions>

背后的代码

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:HelloWorld"
         x:Class="HelloWorld.MainPage">
    <StackLayout Margin="20">
        <Label Text="60 Second Sports" FontAttributes="Bold" HorizontalOptions="Center"/>
          <ListView x:Name="rssList" IsPullToRefreshEnabled="True" Refreshing="ListItems_Refreshing">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.ContextActions>
                          <MenuItem Clicked="MenuItem_Clicked" Text="TEST" Command="{Binding .}"/>
                        </ViewCell.ContextActions>
                        <StackLayout>

                            <Label Text="{Binding Title}" Font="8"/>
                            <!--<Label Text="{Binding Description}" Font="6"/>-->
                            <Label Text="{Binding PublishedDate}" Font="5"/>
                            <Label Text="{Binding Link}" Font="5"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
   </StackLayout>
</ContentPage>

1 个答案:

答案 0 :(得分:1)

如果要在单击ListView项时打开浏览器,则可以只收听ItemTapped事件。

  

点击物品时会触发ItemTapped。

因此,在您的ListView中

<ListView x:Name="rssList" IsPullToRefreshEnabled="True" 
          ItemTapped="listItemTapped" 
          Refreshing="ListItems_Refreshing">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>

                            <Label Text="{Binding Title}" Font="8"/>
                            <!--<Label Text="{Binding Description}" Font="6"/>-->
                            <Label Text="{Binding PublishedDate}" Font="5"/>
                            <Label Text="{Binding Link}" Font="5"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

然后是侦听器方法。

 public void listItemTapped(object sender, ItemTappedEventArgs e)
 {
       Console.WriteLine("button was clicked");
       var item = (YourModel)e.Item;
 }