Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.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# 选择每个ListViewItem并在通用应用程序中应用修改_C#_Win Universal App_Windows 10 Universal - Fatal编程技术网

C# 选择每个ListViewItem并在通用应用程序中应用修改

C# 选择每个ListViewItem并在通用应用程序中应用修改,c#,win-universal-app,windows-10-universal,C#,Win Universal App,Windows 10 Universal,我有一个显示这些信息的列表视图,如果我得到fav=1,我想为我的按钮显示一个不同的背景,如果我得到aime=1,则显示一个不同的图像: JSON格式: { success: 1, total: 2, locals: [ { id_local: "82", fav: 0, aime: 0, aimepas: 0, all_like: "2", all_dislike: "0", }, { id_local: "83", fav: 1, aime: 1, aimepas: 0, all_like:

我有一个显示这些信息的列表视图,如果我得到fav=1,我想为我的按钮显示一个不同的背景,如果我得到aime=1,则显示一个不同的图像: JSON格式:

{
success: 1,
total: 2,
locals: [
{
id_local: "82",
fav: 0,
aime: 0,
aimepas: 0,
all_like: "2",
all_dislike: "0",
},
{
id_local: "83",
fav: 1,
aime: 1,
aimepas: 0,
all_like: "5",
all_dislike: "0",
}
]
}
这是我的代码:

            Uri = "URL";
            var http = new HttpClient();
            http.MaxResponseContentBufferSize = Int32.MaxValue;
            var response = await http.GetStringAsync(Uri);
            var rootObject = JsonConvert.DeserializeObject<Project.Models.RootObject>(response);

            listme.ItemsSource = rootObject.locals;


                for (int i = 0; i < int.Parse(rootObject.total); i++) {

                    if (rootObject1.locals[i].fav == 1)
                    {
                        m_button.Background = new SolidColorBrush(Color.FromArgb(255, 251, 187, 9)); //color1 :Yellow
                    }

                    else
                    {
                        m_button.Background = new SolidColorBrush(Color.FromArgb(255, 178, 178, 178));//color2 :Gray
                    }

                    if (rootObject1.locals[i].aime == 1)
                    {
                        likeimage.Source = new BitmapImage(new Uri("ms-appx:///images/coueur_rouge.png", UriKind.Absolute)); //image1
                    }

                    else
                    {
                        likeimage.Source = new BitmapImage(new Uri("ms-appx:///images/like.png", UriKind.Absolute)); //image2
                    }

                }
Uri=“URL”;
var http=new-HttpClient();
http.MaxResponseContentBufferSize=Int32.MaxValue;
var response=wait http.GetStringAsync(Uri);
var rootObject=JsonConvert.DeserializeObject(响应);
listme.ItemsSource=rootObject.locals;
for(int i=0;i
这是我的xaml:

<ListView  x:Name="listme">
 <ListView.ItemTemplate >
   <DataTemplate >
     <Grid>
       ...
      <Button Background="Gray"  x:Name="m_button"/>
      <Button  Background="Gray" >
           <Image Source="images/like.png" x:Name="likeimage"/>
        </Button>
     </Grid>
   </DataTemplate >
 </ListView.ItemTemplate >
</ListView >

...
我得到的是2个listview项,没有任何更改 请提供我如何更正代码的帮助 谢谢你的帮助

更新:我使用过foreach,如下所示,但ListViewItems仍然存在问题:

listme.ItemsSource = rootObject1.locals;

                    foreach (var item in listme.Items.Cast<Locals>())
                    {

                        if (item.fav == 1)
                            {
                                m_button.Background = new SolidColorBrush(Color.FromArgb(255, 251, 187, 9)); //jaune
                            }

                            else
                            {
                                m_button.Background = new SolidColorBrush(Color.FromArgb(255, 178, 178, 178));//gris
                            }

                            if (item.aime == 1)
                            {
                                likeimage.Source = new BitmapImage(new Uri("ms-appx:///images/coueur_rouge.png", UriKind.Absolute));
                            }

                            else
                            {
                                likeimage.Source = new BitmapImage(new Uri("ms-appx:///images/like.png", UriKind.Absolute));
                            }

                    }
listme.ItemsSource=rootObject1.locals;
foreach(listme.Items.Cast()中的var item)
{
如果(item.fav==1)
{
m_button.Background=新的SolidColorBrush(Color.FromArgb(255,251,187,9));//jaune
}
其他的
{
m_button.Background=新的SolidColorBrush(Color.FromArgb(255178178178178));//gris
}
如果(item.aime==1)
{
likeimage.Source=新的位图图像(新的Uri(“ms”)-appx:///images/coueur_rouge.png“,UriKind.Absolute”);
}
其他的
{
likeimage.Source=新的位图图像(新的Uri(“ms”)-appx:///images/like.png“,UriKind.Absolute”);
}
}

例如,当我从索引为0的项目中选择按钮时,索引为1的项目将被修改,我不知道为什么会得到这个结果!!>_ 读了两遍之后,我明白了你想做什么。你的代码逻辑是错误的。您不能在listItems中使用x:名称,因为代码不知道如何切换一个名称。 您必须使用绑定

为列表项创建一个类

public class Item : INotifyPropertyChanged
{
    private Uri thumbnail;
    public Uri Thumbnail
    {
        get { return thumbnail; }
        set
        {
            thumbnail = value;
            NotifyPropertyChanged("Thumbnail");
        }
    }

    private SolidColorBrush buttonColor = new SolidColorBrush(Color.FromArgb(255, 178, 178, 178));
    public SolidColorBrush ButtonColor
    {
        get { return buttonColor; }
        set
        {
            buttonColor = value;
            NotifyPropertyChanged("ButtonColor");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
        }
    }
}
XAML


您可能需要使用IValueConverter来显示xaml中的值,但我认为这样做是可行的,否则您可能需要更改数据类型。

欢迎您。另外,我将编辑并添加一种方式,单击一个项目的按钮,并仅在该项目中执行。还有,我会修改一些代码,所以要小心。先生,来自web服务的数据呢?我应该设置listme.ItemsSource=rootObject.locals;在listme.ItemsSource=Locals;?之前;??你只设定一次。您可以从一开始就创建或更改rootObject.locals类型的局部变量。这意味着您将只执行listme.ItemsSource=rootObject.locals;加载列表不需要其他代码。
<ListView  x:Name="listme">
 <ListView.ItemTemplate >
   <DataTemplate >
     <Grid>
       ...
      <Button Background="{Binding ButtonColor} Tapped="Button_Tapped"/>
      <Button  Background="Gray" >
           <Image Source="{Binding Thumbnail}"/>
        </Button>
     </Grid>
   </DataTemplate >
 </ListView.ItemTemplate >
</ListView >
//This one outside
        ObservableCollection<Item> Locals = new ObservableCollection<Item>();

    //in method or constractor
    //foreach
    Item listItem = new Item();
    listItem.Thumbnail = new Uri("ms-appx:///images/coueur_rouge.png", UriKind.Absolute);
    listItem.ButtonColor = new SolidColorBrush(Color.FromArgb(255, 251, 187, 9));
    Locals.Add(listItem);
    //end foreach

    // then do 
    listme.ItemsSource = Locals;
private void Button_Tapped(object sender, TappedRoutedEventArgs e)
        {
            Item selectedItem = ((FrameworkElement)sender).DataContext as Item;
            SelectedItem.Thumbnail = null;//whatever you like
            SelectedItem.ButtonColor = null;//whatever you like
        }