首页 > 解决方案 > View.ContextActions 未触发 Clicked 后面的代码

问题描述

问题:

在遵循几个示例之后,,我无法让我的 ContextActions 连接到我的代码背后。

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

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

设置:

我只是在使用我在网上找到的一个基本示例。它不是 MVVM。我有 ListItems_Refresh 操作设置和工作。

本准则的目标:

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

我很想知道如何解决这个问题,或者我是否有明显的错字。提前致谢

代码:

XAML

<?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>

代码背后

    private void MenuItem_Clicked(object sender, EventArgs e)
    {
        Console.WriteLine("button was clicked");
        System.Diagnostics.Process.Start("http://google.com");
    }


    protected void ListItems_Refreshing(object sender, EventArgs e)
    {
        Console.WriteLine("refresh triggered");

        //doRefresh(); 
        //my method for repopulating the list ImageListView.EndRefresh(); 
        //this is a very important step. It will refresh forever without triggering it }
    }

标签: c#xamlxamarin

解决方案


如果您想在单击 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;
 }

推荐阅读