Xamarin Forms命令未激活

时间:2018-09-16 07:44:24

标签: c# visual-studio xamarin mvvm xamarin.forms

我在ListView的ViewCell内的Commmand出现问题。我的“列表”视图数据模板当前正在使用“绑定”从JSON字符串中获取多个对象数据,我感觉这阻止了在我的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"
             x:Class="MIApp.HomePage">
    <ContentPage.Content>
        <ScrollView>
            <StackLayout>
                <Label Text="News Articles"
                    VerticalOptions="CenterAndExpand" 
                    HorizontalOptions="CenterAndExpand" />
                <ListView x:Name="GetListView" HasUnevenRows="true">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>  
                                <StackLayout VerticalOptions="FillAndExpand" Margin="0,20,0,20">
                                    <StackLayout.GestureRecognizers>
                                        <TapGestureRecognizer
                                            Command="{Binding ArtCommand}"
                                            NumberOfTapsRequired="1"
                                            />
                                    </StackLayout.GestureRecognizers>
                                    <Label Text="{Binding strArticleTitle}"/>
                                    <Image Source="{Binding strArticlePhotoUrl}"/>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>

在后面的代码中,我将绑定上下文设置为此处的视图模型,并检索列表视图的数据。

[XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class HomePage : ContentPage
    {

        HomeVM viewModel;

        public HomePage()
        {
            InitializeComponent();

            viewModel = new HomeVM();
            BindingContext = viewModel;
        }



        protected async override void OnAppearing()
        {
            base.OnAppearing();

            HttpClient client = new HttpClient();
            string url = "https://example.net/api/Articles/GetArticles";
            var response = await client.GetAsync(url);
            if (response.IsSuccessStatusCode)
            {
                string res = "";
                using (HttpContent content = response.Content)
                {
                    Task<string> result = content.ReadAsStringAsync();
                    res = result.Result;
                    var ArticlesList = Articles.ArticlesItems.FromJson(res);
                    GetListView.ItemsSource = ArticlesList;
                }
            }
            else
            {
                await DisplayAlert("Connection Error", "Please Connect to the internet and try again", "Ok");
            }
        }

    }

有什么办法可以解决这个问题,我是Xamarin Forms的新手。

谢谢

瑞安

1 个答案:

答案 0 :(得分:1)

列表视图的DataTemplate上下文已绑定到源项目,这是源于命令的位置。

但是,如果您希望在页面的主视图模型中使用Command,则可以执行以下操作。

<ContentPage
   ...
   x:Name="This"

还有您的DataTemplate

<TapGestureRecognizer
     Command="{Binding Path=BindingContext.ArtCommand, Source={x:Reference This}}

但是您必须设置CommandParameter="{Binding}"才能获得项目

说了这么多,您还可以将ItemSelectedItemTapped与后面的代码一起使用,或使用EventToCommand行为