Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新ListView项中的值_C#_Xml_Listview_Xamarin_Xamarin.forms - Fatal编程技术网

C# 更新ListView项中的值

C# 更新ListView项中的值,c#,xml,listview,xamarin,xamarin.forms,C#,Xml,Listview,Xamarin,Xamarin.forms,我有一个帖子的列表视图,我需要在单击时更改类似按钮的图像源 我的XML是 <ListView x:Name="MessageView" HasUnevenRows="True"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayou

我有一个帖子的列表视图,我需要在单击时更改类似按钮的图像源

我的XML是

<ListView x:Name="MessageView" HasUnevenRows="True">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout BackgroundColor="White" Margin="10, 10, 10, 0" Padding="10, 10, 15, 10">
                            <Image Source="options_icon.png" HeightRequest="15" HorizontalOptions="End" Margin="0, 0, 10, 0">
                                <Image.GestureRecognizers>
                                    <TapGestureRecognizer Command="{Binding LikeClick}"/>
                                </Image.GestureRecognizers>
                            </Image>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
LikeMessage方法是

public static async Task LikeMessage(INavigation navigation, int id)
    {
*Web Request*


        if (page_result.Equals("liked"))
        {
           //Update source to "liked_icon.png"
        }

        if (page_result.Equals("unliked"))
        {
            //Update source to "like_icon.png"
        }
    }

首先为
元素命名

<StackLayout x:Name="Item" BackgroundColor="White" Margin="10, 10, 10, 0" Padding="10, 10, 15, 10">

其次,在您的列表中,点击手势如下所示

 <TapGestureRecognizer BindingContext="{Binding Source={x:Reference MessageView}, Path=BindingContext}"   Command="{Binding LikeClick}" CommandParameter="{Binding Source={x:Reference Item}, Path=BindingContext}"/>

第三,还要绑定图像源

     <Image Source="{Binding ImageSource}" HeightRequest="15" HorizontalOptions="End" Margin="0, 0, 10, 0">


第四,在viewmodel类中,将
图像源设置为要更改的源。

首先为
元素命名

<StackLayout x:Name="Item" BackgroundColor="White" Margin="10, 10, 10, 0" Padding="10, 10, 15, 10">

其次,在您的列表中,点击手势如下所示

 <TapGestureRecognizer BindingContext="{Binding Source={x:Reference MessageView}, Path=BindingContext}"   Command="{Binding LikeClick}" CommandParameter="{Binding Source={x:Reference Item}, Path=BindingContext}"/>

第三,还要绑定图像源

     <Image Source="{Binding ImageSource}" HeightRequest="15" HorizontalOptions="End" Margin="0, 0, 10, 0">


第四,在viewmodel类中,将
ImageSource
设置为要更改的源。

您需要将InotifyProperty更改为
MessageObject
类,并执行以下操作

更新你的班级

public string _Source;
public string Source
{
    get { return _Source; }
    set
    {
        _Source = value;
        OnPropertyChanged(nameof(Source));
    }
}
命令绑定应如下所示:

LikeClick = new Command(() => LikeMessage(navigation, message)),
绑定图像源

<Image Source="{Binding Source}" 

您需要将INotifyPropertyChanged实现到
MessageObject
类,并执行以下操作

更新你的班级

public string _Source;
public string Source
{
    get { return _Source; }
    set
    {
        _Source = value;
        OnPropertyChanged(nameof(Source));
    }
}
命令绑定应如下所示:

LikeClick = new Command(() => LikeMessage(navigation, message)),
绑定图像源

<Image Source="{Binding Source}" 

方法被触发了吗?方法被触发了吗?我尝试实现它(),但我不知道如何在LikePost方法中编辑它,我猜我应该通过LikeMessage构造函数传递变量,但我在XMLLikePost中找不到任何变量,假设它说LikeMessage typoHere,在这一行
CommandParameter=”{Binding Source={x:Reference Item},Path=BindingContext}“
x:Reference名称应该是
MessageLayout
而不是“Item”,例如`LikeClick=new命令(async(e)=>wait LikeMessage(e));`在方法中,您可以获得类似`var Item=(e as PostObject)`的列表视图对象;` e在哪里在LikeMessage Get from?中,我尝试实现它(),但我不知道如何在LikePost方法中编辑它,我猜我应该通过LikeMessage构造函数传递变量,但我在XMLLikePost中找不到任何变量,它应该说LikeMessage typoHere,在这一行
CommandParameter=“{Binding Source={x:Reference Item},Path=BindingContext}“
x:Reference名称应该是
MessageLayout
而不是“Item”,例如`likelick=new命令(async(e)=>wait LikeMessage(e));`在方法中,您可以获得类似`var Item=(e as PostObject)`的列表视图对象;` e在哪里在“OnPropertyChanged(nameof(Source));“我正在获取的OnPropertyChanged名称在当前上下文中不存在”的行上,在“OnPropertyChanged(nameof(Source));“我正在获取的OnPropertyChanged名称在当前上下文中不存在”的行上