xamarin.forms中使用的Listview项

帕勃罗·帕多(Pablo Pardo)

我正在尝试在Xamarin.Forms和XAML中开发一个应用程序,而当我点击LV时,我需要捕获一个事件。我希望应用程序执行的操作是,当用户点击列表中的项目时,更改单元格(或单元格内的StackLayout)的颜色。我正在尝试在listview上实现OnItemTapped属性,但是当我按下Item时什么也没有发生。我的代码很简单:

型号:具有2种属性,名称和颜色的产品。

public class Product: INotifyPropertyChanged
    {
        #region INotifyPropertyChanged implementation

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public Product ()
        {
        }

        public string Name { get; set; }
        public bool Buy { get; set; }
        public  Color micolor { get; set; }

        //Método para actualizar los cambios
        protected virtual void OnPropertyChanged (string propertyName)
        {
            if (PropertyChanged == null)
                return;

            PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
        }
    }

XAML:仅包含两个绑定的列表:标签中的名称和SL中的颜色

<?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="pruebalv.MyPage1">
    <ContentPage.Content>
     <ListView  x:Name="listView"
     ItemTapped="OmItemTapped"
     >

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout 
                        Orientation="Vertical"
                        BackgroundColor ="{Binding micolor}">
                                <Label Text="{Binding Name}"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

在.cs代码后面:我创建产品(这只是一个示例),并实现所点击的项目。轻按项目时,我想更改所按项目的color属性。

public void OmItemTapped (object o, ItemTappedEventArgs e)
        {
            var dataItem = e.Item as Product;
            dataItem.micolor  = Color.Green;
            dataItem.Name  = "Tapped!!";

        }

创建列表时得到的结果是,当我点击一个项目时什么也没有发生。

我想念什么吗?正如我对教程所了解的那样,只有使用ListView的“ itemtapped”绳索和产品上的InotifyProperty ...,我才能看到结果。这是正确的吗?谢谢!

Arkadiusz K

Xamarin.Forms MVVM基础结构中的重要部分是,当您更改属性值时,需要显式调用PropertyChanged。

因此,您需要修改ViewModel代码,以便PropertyChanged在属性更改时引发事件。请参见以下示例。

    private string name;
    public string Name 
    { 
        get { return name; }
        set 
        {
           name = value;
           OnPropertyChanged("Name");
        }
    }

而且您必须对其他属性执行相同的操作

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Xamarin Forms ListView数据绑定

在Xamarin.Forms中使用Thread.Sleep

xamarin.forms ListView ItemSelected

在Xamarin Forms中使用自定义地图样式?

Xamarin.Forms ListView项删除具有旧值

如何在Xamarin.Forms中将CommandParameter设置为ListView项本身

检查用户是否在Xamarin.Forms中使用iPhone或iPad

在Xamarin表单中使用UIDocumentPickerViewController作为依赖项服务

IdleTimerDisabled无法在Xamarin Forms IOS中使用

Xamarin.Forms。优化ListView

如何使用SQLite在Xamarin.Forms中实现MVVM以填充需要在每行中使用INotifyPropertyChanged / Icommand的Listview

Xamarin Forms AutomationId在ListView项上

ListView Xamarin.forms使用ImageResource从ItemSource动态加载图像

Xamarin.Forms使用ReactiveUI检测空的ListView以显示Label

在Xamarin.Forms中使用StructureMap

如何创建要在Xamarin.Forms XML中使用的常量

在Xamarin.Forms中使用SqlConnection?

在Xamarin.Forms中使用RadioButton

我可以在Xamarin.Forms中的“运行”上对ListView项重新排序吗?

使用Xamarin.Forms更新ListView

Xamarin.Forms上的更改页面时如何禁用ListView重新加载项?

Xamarin Forms:访问数据绑定ListView项的View?

如何在 Xamarin Forms 的 Prism 中使用 EventToCommandBehavior 获取 ItemTappedEventArgs

Xamarin Forms - 使用 SQLite

在 Xamarin.Forms 中使用 Syncfusion 的分页数据网格

Xamarin Forms Listview 中的 TapGestureRecognition

单击 Xamarin.Forms 中的 ListView 项后完整显示详细信息

Xamarin.Forms - 在 ResourceDictionary 中使用 StaticResource

如何在 Xamarin Forms 中使用 MVVM 在 ListView 中实现增量加载(ItemAppearing)项目?