Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.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# 使用ObservableCollection和InotifyProperty的正确方法已更改_C#_Listview_Xamarin_Xamarin.forms - Fatal编程技术网

C# 使用ObservableCollection和InotifyProperty的正确方法已更改

C# 使用ObservableCollection和InotifyProperty的正确方法已更改,c#,listview,xamarin,xamarin.forms,C#,Listview,Xamarin,Xamarin.forms,我有一个ListView,我想根据接收到的字符串用许多项填充它。我目前有一个列表视图,其中我“手动”添加了一些条目,以尝试熟悉INotifyPropertyChanged和ObservableCollection的概念。我想做的是有一个图像,当点击ListView项时会发生变化(仅针对特定点击的ListView项)。目前,我只能通过让图像充当ImageButton(图像点击事件)来让它工作。在ListView中同时使用这两个属性的正确方法是什么 我只与Xamarin和C#合作了不到两周,因此如果

我有一个ListView,我想根据接收到的字符串用许多项填充它。我目前有一个列表视图,其中我“手动”添加了一些条目,以尝试熟悉INotifyPropertyChangedObservableCollection的概念。我想做的是有一个图像,当点击ListView项时会发生变化(仅针对特定点击的ListView项)。目前,我只能通过让图像充当ImageButton(图像点击事件)来让它工作。在ListView中同时使用这两个属性的正确方法是什么

我只与Xamarin和C#合作了不到两周,因此如果答案或建议非常直截了当,我表示歉意。如有任何帮助/建议或示例,将不胜感激。提前谢谢

我的继电器类别(其NotifyProperty已更改)如下所示:

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Runtime.CompilerServices;
 using System.Text;


 namespace Socket.Models
 {


    public class Relays : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged([CallerMemberName] string name =     
           "")
    {
        PropertyChanged?.Invoke(this, new         
        PropertyChangedEventArgs(name));
    }

    //public string ImageUrl { get; set; }    // The source of the 
                                             Image in the Listview 

    public int ID { get; set; }

    private string _Name;

    public string Name                      // The name of the item in 
                                               the ListView
    {
        get
        {
            return _Name;
        }
        set
        {
            if (_Name != value)
            {
                _Name = value;

                OnPropertyChanged();
            }
        }
    }

    private string _ImageUrl;

    public string ImageUrl                      // The name of the 
                                         Image in the ListView
    {
        get
        {
            return _ImageUrl;
        }
        set
        {
            if (_ImageUrl != value)
            {
                _ImageUrl = value;

                OnPropertyChanged();
               }
           }
       }

   }

 }
 using Socket.Models;
 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.ComponentModel;
 using System.Linq;
 using System.Runtime.CompilerServices;
 using System.Text;
 using System.Threading.Tasks;
 using Xamarin.Forms;
 using Xamarin.Forms.Xaml;

 namespace Socket
 {
     [XamlCompilation(XamlCompilationOptions.Compile)]

     public partial class RelayControl : ContentPage
     {
         public Boolean RelayOn=false;                   // Used to                
                                                    check the State
         public string RelayName;                        // Used to 
                                               test the name change
         public string Images;                           // Used to 
                                                   change the Image
         public int RelayID;

         public RelayControl()
         {
             InitializeComponent ();
             Images= "ryno.jpg";
             RelayName = "Cool";
             loadSampleData();
             this.BindingContext = new Relays();         // Binding the 
                                                    Listview items

             var neg = lstView.BindingContext as Relays;

           //RelayOn = neg.isOn;                        // Assign the 
                            Binding OnProperty to the Boolean Variable
             RelayName = neg.Name;

             Images = neg.ImageUrl; 

             RelayID = neg.ID;
         }

         private void loadSampleData()
         {
        // Create sample data

             ObservableCollection<Relays> listRelays = new      
                               ObservableCollection<Relays>();

             listRelays.Add(new Relays { Name = "Relay " + 0, ImageUrl 
                                              = "ryno.jpg", ID = 0 });
             listRelays.Add(new Relays { Name = "Relay " + 1, ImageUrl 
                                    ="ryno.jpg", ID = 1 }); // num = i
             listRelays.Add(new Relays { Name = RelayName, ImageUrl = 
                                                    Images, ID = 2 });

             lstView.ItemsSource = listRelays;                 
         }

         public class MyListItemEventArgs : EventArgs
         {
             public Relays MyItem { get; set; }

             public MyListItemEventArgs(Relays item)
             {
                 this.MyItem = item;
             }
         }

         private void ImageCell_Tapped(object sender, 
                                      MyListItemEventArgs e)
         {
             var item = e.MyItem;

             if (item == null) return;

             if (item.ID == 2)
             {
                 if (RelayOn == true)
                 {
                     RelayName = "On";
                     Images = "on.jpg";
                     RelayOn = false;
                 }
                 else
                 {
                     RelayName = "OFF";
                     Images = "off.jpg";
                     RelayOn = true;
                 }
             }

             loadSampleData();

         }
 <?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="Socket.RelayControl"
              Title="Relay Control Page">

     <ContentPage.Content>
         <StackLayout Padding="10,0,0,10" Margin="2">
             <!--<SearchBar x:Name="MainSearchBar"                
             SearchButtonPressed="SearchBar_SearchButtonPressed"> 
             </SearchBar>-->
             <ListView x:Name="lstView" SelectionMode="None" 
              RowHeight="50" >
                 <ListView.ItemTemplate>
                     <DataTemplate>
                         <ViewCell >
                             <StackLayout Orientation="Vertical">
                                 <StackLayout Orientation="Horizontal">

                                <Label Text="{Binding Name,      
                                    Mode=OneWay}"
                                    x:Name="Received" 
                                    TextColor="#f35e20" 
                                    VerticalTextAlignment="Center"/>

                                <Image Source="{Binding ImageUrl, 
                                 Mode=OneWay}" Aspect="AspectFit"  
                                 HeightRequest="50" 
                                 WidthRequest="110" 
                                 HorizontalOptions="EndAndExpand" 
                                 VerticalOptions="Center">

                                    <Image.GestureRecognizers>
                                        <TapGestureRecognizer  
                                        CommandParameter="{Binding .}" 
                                        Tapped="ImageCell_Tapped" />
                                    </Image.GestureRecognizers>

                                </Image>
                                <!--LabelText="{Binding Name}" 
                                TextColor="DarkGoldenrod" ImageSource=" 
                                {Binding ImageUrl}" 
                               Tapped="ImageCell_Tapped">-->

                                 </StackLayout>
                             </StackLayout>
                         </ViewCell>
                     </DataTemplate>
                 </ListView.ItemTemplate>
             </ListView>

         </StackLayout>
     </ContentPage.Content>
 </ContentPage>
RelayControl.xaml.cs类(具有ObservableCollection)如下所示:

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Runtime.CompilerServices;
 using System.Text;


 namespace Socket.Models
 {


    public class Relays : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged([CallerMemberName] string name =     
           "")
    {
        PropertyChanged?.Invoke(this, new         
        PropertyChangedEventArgs(name));
    }

    //public string ImageUrl { get; set; }    // The source of the 
                                             Image in the Listview 

    public int ID { get; set; }

    private string _Name;

    public string Name                      // The name of the item in 
                                               the ListView
    {
        get
        {
            return _Name;
        }
        set
        {
            if (_Name != value)
            {
                _Name = value;

                OnPropertyChanged();
            }
        }
    }

    private string _ImageUrl;

    public string ImageUrl                      // The name of the 
                                         Image in the ListView
    {
        get
        {
            return _ImageUrl;
        }
        set
        {
            if (_ImageUrl != value)
            {
                _ImageUrl = value;

                OnPropertyChanged();
               }
           }
       }

   }

 }
 using Socket.Models;
 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.ComponentModel;
 using System.Linq;
 using System.Runtime.CompilerServices;
 using System.Text;
 using System.Threading.Tasks;
 using Xamarin.Forms;
 using Xamarin.Forms.Xaml;

 namespace Socket
 {
     [XamlCompilation(XamlCompilationOptions.Compile)]

     public partial class RelayControl : ContentPage
     {
         public Boolean RelayOn=false;                   // Used to                
                                                    check the State
         public string RelayName;                        // Used to 
                                               test the name change
         public string Images;                           // Used to 
                                                   change the Image
         public int RelayID;

         public RelayControl()
         {
             InitializeComponent ();
             Images= "ryno.jpg";
             RelayName = "Cool";
             loadSampleData();
             this.BindingContext = new Relays();         // Binding the 
                                                    Listview items

             var neg = lstView.BindingContext as Relays;

           //RelayOn = neg.isOn;                        // Assign the 
                            Binding OnProperty to the Boolean Variable
             RelayName = neg.Name;

             Images = neg.ImageUrl; 

             RelayID = neg.ID;
         }

         private void loadSampleData()
         {
        // Create sample data

             ObservableCollection<Relays> listRelays = new      
                               ObservableCollection<Relays>();

             listRelays.Add(new Relays { Name = "Relay " + 0, ImageUrl 
                                              = "ryno.jpg", ID = 0 });
             listRelays.Add(new Relays { Name = "Relay " + 1, ImageUrl 
                                    ="ryno.jpg", ID = 1 }); // num = i
             listRelays.Add(new Relays { Name = RelayName, ImageUrl = 
                                                    Images, ID = 2 });

             lstView.ItemsSource = listRelays;                 
         }

         public class MyListItemEventArgs : EventArgs
         {
             public Relays MyItem { get; set; }

             public MyListItemEventArgs(Relays item)
             {
                 this.MyItem = item;
             }
         }

         private void ImageCell_Tapped(object sender, 
                                      MyListItemEventArgs e)
         {
             var item = e.MyItem;

             if (item == null) return;

             if (item.ID == 2)
             {
                 if (RelayOn == true)
                 {
                     RelayName = "On";
                     Images = "on.jpg";
                     RelayOn = false;
                 }
                 else
                 {
                     RelayName = "OFF";
                     Images = "off.jpg";
                     RelayOn = true;
                 }
             }

             loadSampleData();

         }
 <?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="Socket.RelayControl"
              Title="Relay Control Page">

     <ContentPage.Content>
         <StackLayout Padding="10,0,0,10" Margin="2">
             <!--<SearchBar x:Name="MainSearchBar"                
             SearchButtonPressed="SearchBar_SearchButtonPressed"> 
             </SearchBar>-->
             <ListView x:Name="lstView" SelectionMode="None" 
              RowHeight="50" >
                 <ListView.ItemTemplate>
                     <DataTemplate>
                         <ViewCell >
                             <StackLayout Orientation="Vertical">
                                 <StackLayout Orientation="Horizontal">

                                <Label Text="{Binding Name,      
                                    Mode=OneWay}"
                                    x:Name="Received" 
                                    TextColor="#f35e20" 
                                    VerticalTextAlignment="Center"/>

                                <Image Source="{Binding ImageUrl, 
                                 Mode=OneWay}" Aspect="AspectFit"  
                                 HeightRequest="50" 
                                 WidthRequest="110" 
                                 HorizontalOptions="EndAndExpand" 
                                 VerticalOptions="Center">

                                    <Image.GestureRecognizers>
                                        <TapGestureRecognizer  
                                        CommandParameter="{Binding .}" 
                                        Tapped="ImageCell_Tapped" />
                                    </Image.GestureRecognizers>

                                </Image>
                                <!--LabelText="{Binding Name}" 
                                TextColor="DarkGoldenrod" ImageSource=" 
                                {Binding ImageUrl}" 
                               Tapped="ImageCell_Tapped">-->

                                 </StackLayout>
                             </StackLayout>
                         </ViewCell>
                     </DataTemplate>
                 </ListView.ItemTemplate>
             </ListView>

         </StackLayout>
     </ContentPage.Content>
 </ContentPage>
使用Socket.Models;
使用制度;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Linq;
使用System.Runtime.CompilerServices;
使用系统文本;
使用System.Threading.Tasks;
使用Xamarin.Forms;
使用Xamarin.Forms.Xaml;
命名空间套接字
{
[XamlCompilation(XamlCompilationOptions.Compile)]
公共部分类中继控制:ContentPage
{
public Boolean RelayOn=false;//用于
检查状态
公共字符串RelayName;//用于
测试名称更改
公共字符串图像;//用于
更改图像
公共关系;
公共继电器控制()
{
初始化组件();
Images=“ryno.jpg”;
RelayName=“酷”;
loadSampleData();
this.BindingContext=新继电器();//绑定
列表视图项
var neg=lstView.BindingContext作为中继;
//RelayOn=neg.isOn;//分配
将OnProperty绑定到布尔变量
RelayName=neg.Name;
Images=neg.ImageUrl;
RelayID=neg.ID;
}
私有void loadSampleData()
{
//创建示例数据
ObservableCollection列表继电器=新
可观察收集();
添加(新的中继{Name=“Relay”+0,ImageUrl
=“ryno.jpg”,ID=0});
添加(新的中继{Name=“Relay”+1,ImageUrl
=“ryno.jpg”,ID=1});//num=i
添加(新的中继{Name=RelayName,ImageUrl=
图像,ID=2});
lstView.ItemsSource=列表继电器;
}
公共类MyListItemEventArgs:EventArgs
{
公共项目{get;set;}
公共MyListItemEventArgs(继电器项目)
{
this.MyItem=项目;
}
}
私有void ImageCell_(对象发送器,
MyListItemEventArgs(e)
{
var item=e.MyItem;
如果(item==null)返回;
如果(item.ID==2)
{
if(RelayOn==true)
{
RelayName=“On”;
Images=“on.jpg”;
RelayOn=false;
}
其他的
{
RelayName=“关”;
Images=“off.jpg”;
RelayOn=true;
}
}
loadSampleData();
}
RelayControl.xaml如下所示:

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Runtime.CompilerServices;
 using System.Text;


 namespace Socket.Models
 {


    public class Relays : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged([CallerMemberName] string name =     
           "")
    {
        PropertyChanged?.Invoke(this, new         
        PropertyChangedEventArgs(name));
    }

    //public string ImageUrl { get; set; }    // The source of the 
                                             Image in the Listview 

    public int ID { get; set; }

    private string _Name;

    public string Name                      // The name of the item in 
                                               the ListView
    {
        get
        {
            return _Name;
        }
        set
        {
            if (_Name != value)
            {
                _Name = value;

                OnPropertyChanged();
            }
        }
    }

    private string _ImageUrl;

    public string ImageUrl                      // The name of the 
                                         Image in the ListView
    {
        get
        {
            return _ImageUrl;
        }
        set
        {
            if (_ImageUrl != value)
            {
                _ImageUrl = value;

                OnPropertyChanged();
               }
           }
       }

   }

 }
 using Socket.Models;
 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.ComponentModel;
 using System.Linq;
 using System.Runtime.CompilerServices;
 using System.Text;
 using System.Threading.Tasks;
 using Xamarin.Forms;
 using Xamarin.Forms.Xaml;

 namespace Socket
 {
     [XamlCompilation(XamlCompilationOptions.Compile)]

     public partial class RelayControl : ContentPage
     {
         public Boolean RelayOn=false;                   // Used to                
                                                    check the State
         public string RelayName;                        // Used to 
                                               test the name change
         public string Images;                           // Used to 
                                                   change the Image
         public int RelayID;

         public RelayControl()
         {
             InitializeComponent ();
             Images= "ryno.jpg";
             RelayName = "Cool";
             loadSampleData();
             this.BindingContext = new Relays();         // Binding the 
                                                    Listview items

             var neg = lstView.BindingContext as Relays;

           //RelayOn = neg.isOn;                        // Assign the 
                            Binding OnProperty to the Boolean Variable
             RelayName = neg.Name;

             Images = neg.ImageUrl; 

             RelayID = neg.ID;
         }

         private void loadSampleData()
         {
        // Create sample data

             ObservableCollection<Relays> listRelays = new      
                               ObservableCollection<Relays>();

             listRelays.Add(new Relays { Name = "Relay " + 0, ImageUrl 
                                              = "ryno.jpg", ID = 0 });
             listRelays.Add(new Relays { Name = "Relay " + 1, ImageUrl 
                                    ="ryno.jpg", ID = 1 }); // num = i
             listRelays.Add(new Relays { Name = RelayName, ImageUrl = 
                                                    Images, ID = 2 });

             lstView.ItemsSource = listRelays;                 
         }

         public class MyListItemEventArgs : EventArgs
         {
             public Relays MyItem { get; set; }

             public MyListItemEventArgs(Relays item)
             {
                 this.MyItem = item;
             }
         }

         private void ImageCell_Tapped(object sender, 
                                      MyListItemEventArgs e)
         {
             var item = e.MyItem;

             if (item == null) return;

             if (item.ID == 2)
             {
                 if (RelayOn == true)
                 {
                     RelayName = "On";
                     Images = "on.jpg";
                     RelayOn = false;
                 }
                 else
                 {
                     RelayName = "OFF";
                     Images = "off.jpg";
                     RelayOn = true;
                 }
             }

             loadSampleData();

         }
 <?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="Socket.RelayControl"
              Title="Relay Control Page">

     <ContentPage.Content>
         <StackLayout Padding="10,0,0,10" Margin="2">
             <!--<SearchBar x:Name="MainSearchBar"                
             SearchButtonPressed="SearchBar_SearchButtonPressed"> 
             </SearchBar>-->
             <ListView x:Name="lstView" SelectionMode="None" 
              RowHeight="50" >
                 <ListView.ItemTemplate>
                     <DataTemplate>
                         <ViewCell >
                             <StackLayout Orientation="Vertical">
                                 <StackLayout Orientation="Horizontal">

                                <Label Text="{Binding Name,      
                                    Mode=OneWay}"
                                    x:Name="Received" 
                                    TextColor="#f35e20" 
                                    VerticalTextAlignment="Center"/>

                                <Image Source="{Binding ImageUrl, 
                                 Mode=OneWay}" Aspect="AspectFit"  
                                 HeightRequest="50" 
                                 WidthRequest="110" 
                                 HorizontalOptions="EndAndExpand" 
                                 VerticalOptions="Center">

                                    <Image.GestureRecognizers>
                                        <TapGestureRecognizer  
                                        CommandParameter="{Binding .}" 
                                        Tapped="ImageCell_Tapped" />
                                    </Image.GestureRecognizers>

                                </Image>
                                <!--LabelText="{Binding Name}" 
                                TextColor="DarkGoldenrod" ImageSource=" 
                                {Binding ImageUrl}" 
                               Tapped="ImageCell_Tapped">-->

                                 </StackLayout>
                             </StackLayout>
                         </ViewCell>
                     </DataTemplate>
                 </ListView.ItemTemplate>
             </ListView>

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


您有很多选择。我想看看@snowCrabs,我会看一看;谢谢。