C# 为什么此ListView不与ObservableCollection元素绑定?

C# 为什么此ListView不与ObservableCollection元素绑定?,c#,visual-studio,listview,xamarin,observablecollection,C#,Visual Studio,Listview,Xamarin,Observablecollection,我试图用可观察的收集数据绑定ListView,但它仍然是空的。我有一些断点,可以看到ListaProfissionais(可观察的集合)已正确填充。下面您可以看到my.cs和.xaml代码(重要的部分)。有人看到这里出了什么问题吗 using System.ComponentModel; public class Profissionais : INotifyPropertyChanged { private string id; public string Id {

我试图用可观察的收集数据绑定ListView,但它仍然是空的。我有一些断点,可以看到ListaProfissionais(可观察的集合)已正确填充。下面您可以看到my.cs和.xaml代码(重要的部分)。有人看到这里出了什么问题吗


using System.ComponentModel;

public class Profissionais : INotifyPropertyChanged
{
    private string id;
    public string Id
    {
        get { return this.id; }
        set
        {
            if (this.id != value)
            {
                this.id = value;
                this.NotifyPropertyChanged("Id");
            }
        }
    }

    private string categoria;
    public string Categoria
    {
        get { return this.categoria; }
        set
        {
            if (this.categoria != value)
            {
                this.categoria = value;
                this.NotifyPropertyChanged("Categoria");
            }
        }
    }

    private string titulo;
    public string Titulo
    {
        get { return this.titulo; }
        set
        {
            if (this.titulo != value)
            {
                this.titulo = value;
                this.NotifyPropertyChanged("Titulo");
            }
        }
    }

    private string fotoperfil;
    public string FotoPerfil
    {
        get { return this.fotoperfil; }
        set
        {
            if (this.fotoperfil != value)
            {
                this.fotoperfil = value;
                this.NotifyPropertyChanged("FotoPerfil");
            }
        }
    }

    private string endereco;
    public string Endereco
    {
        get { return this.endereco; }
        set
        {
            if (this.endereco != value)
            {
                this.endereco = value;
                this.NotifyPropertyChanged("Endereco");
            }
        }
    }

    private string distancia;
    public string Distancia
    {
        get { return this.distancia; }
        set
        {
            if (this.distancia != value)
            {
                this.distancia = value;
                this.NotifyPropertyChanged("Distancia");
            }
        }
    }

    private string sexo;
    public string Sexo
    {
        get { return this.sexo; }
        set
        {
            if (this.sexo != value)
            {
                this.sexo = value;
                this.NotifyPropertyChanged("Sexo");
            }
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }

}



public static ObservableCollection<Profissionais> ListaProfissionais = new ObservableCollection<Profissionais>();


try
{
   using (WebClient client = new WebClient())
   {
      Uri uri = new Uri("xxxx.xxxxx.php");
      byte[] retorno = await client.UploadValuesTaskAsync(uri, DataSend);
      string resultado = System.Text.Encoding.UTF8.GetString(retorno);
      ListaProfissionais = JsonConvert.DeserializeObject<ObservableCollection<Profissionais>>(resultado);

   }
}
catch
{
  throw;
}
finally
{
  ListViewProfissionais.ItemsSource = ListaProfissionais; 
  // crashes here but I can see that ItemsSource has the 4 elements as expected
}


使用系统组件模型;
公共类利润:INotifyPropertyChanged
{
私有字符串id;
公共字符串Id
{
获取{返回this.id;}
设置
{
if(this.id!=值)
{
this.id=值;
本协议。NotifyPropertyChanged(“Id”);
}
}
}
私有字符串范畴;
公共字符串分类
{
获取{返回this.categoria;}
设置
{
if(this.categoria!=值)
{
this.categoria=值;
本协议。NotifyPropertyChanged(“类别”);
}
}
}
私有字符串titulo;
公共字符串Titulo
{
获取{返回this.titulo;}
设置
{
if(this.titulo!=值)
{
this.titulo=值;
本协议。NotifyPropertyChanged(“Titulo”);
}
}
}
私有字符串fotoperfil;
公共字符串FotoPerfil
{
获取{返回this.fotoperfil;}
设置
{
if(this.fotoperfil!=值)
{
this.fotoperfil=值;
本条。NotifyPropertyChanged(“FotoPerfil”);
}
}
}
私人字符串endereco;
公共字符串Endereco
{
获取{返回this.endereco;}
设置
{
if(this.endereco!=值)
{
this.endereco=值;
本协议。NotifyPropertyChanged(“Endereco”);
}
}
}
私人字符串距离;
公共字符串距离
{
获取{返回this.distance;}
设置
{
if(this.distance!=值)
{
这个。距离=值;
本条。NotifyPropertyChanged(“距离”);
}
}
}
私人字符串sexo;
公共字符串Sexo
{
获取{返回this.sexo;}
设置
{
if(this.sexo!=值)
{
this.sexo=值;
本条。NotifyPropertyChanged(“Sexo”);
}
}
}
公共事件属性更改事件处理程序属性更改;
public void NotifyPropertyChanged(字符串propName)
{
if(this.PropertyChanged!=null)
this.PropertyChanged(this,newpropertyChangedEventArgs(propName));
}
}
public static observedcollection ListaProfissionais=新observedcollection();
尝试
{
使用(WebClient=newWebClient())
{
Uri=新的Uri(“xxxx.xxxxx.php”);
byte[]returno=wait client.UploadValuesTaskAsync(uri,DataSend);
stringresultado=System.Text.Encoding.UTF8.GetString(returno);
listaproficationais=JsonConvert.DeserializeObject(resultado);
}
}
抓住
{
投掷;
}
最后
{
ListViewProfissionais.ItemsSource=ListaProfissionais;
//此处崩溃,但我可以看到ItemsSource具有预期的4个元素
}
我的XAML:


<ListView x:Name="ListViewProfissionais" x:FieldModifier="public static" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" VerticalScrollBarVisibility="Never" HorizontalScrollBarVisibility="Never" BackgroundColor="Transparent" SeparatorColor="Transparent">
      <ListView.ItemTemplate>
         <DataTemplate>
            <Frame HasShadow="True" CornerRadius="5" HeightRequest="215" Margin="8" Padding="4" BackgroundColor="White">
               <Grid>
                 <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                 </Grid.ColumnDefinitions>

                 <Grid.RowDefinitions>
                    <RowDefinition Height="15"></RowDefinition>
                    <RowDefinition Height="25"></RowDefinition>
                    <RowDefinition Height="105"></RowDefinition>
                    <RowDefinition Height="25"></RowDefinition>
                    <RowDefinition Height="25"></RowDefinition>
                 </Grid.RowDefinitions>

                 <Label Text="{Binding Categoria}" TextColor="#8e8e8e" FontSize="10" Grid.Column="0" Grid.Row="0" VerticalOptions="StartAndExpand" HorizontalOptions="StartAndExpand" Margin="2,0,0,0"></Label>
                 <Label Text="{Binding Titulo}" FontAttributes="Bold" TextColor="#337760" FontSize="14" Grid.Column="0" Grid.Row="1" VerticalOptions="StartAndExpand" HorizontalOptions="StartAndExpand" Margin="2,0,0,0"></Label>

                <Frame Grid.Column="0" Grid.Row="2" BackgroundColor="Transparent" CornerRadius="4" Padding="0" Margin="0">
                <Image Source="{Binding FotoPerfil}" HorizontalOptions="FillAndExpand" Aspect="AspectFill" VerticalOptions="FillAndExpand" />
                 </Frame>

                 <Label Text="{Binding Endereco}" TextColor="#8e8e8e" FontSize="10" Grid.Column="0" Grid.Row="3" VerticalOptions="EndAndExpand" HorizontalOptions="StartAndExpand"></Label>
                  <Label Text="&#xf3c5;" Grid.Column="0" Grid.Row="4" FontSize="10" HorizontalOptions="StartAndExpand" TextColor="#ff9000" Padding="0,6,0,0">
                   <Label.FontFamily>
                     <OnPlatform x:TypeArguments="x:String" Android="Font-Awesome-Free-Solid.otf#FontAwesome5Free-Solid" iOS="FontAwesome5Free-Solid" />
                   </Label.FontFamily>
                  </Label>
                  <Label Text="{Binding Distancia}" TextColor="#ff9000" FontSize="10" Grid.Column="0" Grid.Row="4" VerticalOptions="Center" HorizontalOptions="StartAndExpand" Margin="10,0,0,0"></Label>
               </Grid>
            </Frame>
         </DataTemplate>
       </ListView.ItemTemplate>
  </ListView>


它会崩溃,并显示消息“System.InvalidCastException:指定的强制转换无效。”。见下文:

System.InvalidCastException: Specified cast is not valid.
  at at (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr)
  at Xamarin.Forms.Internals.TemplatedItemsList`2[TView,TItem].ActivateContent (System.Int32 index, System.Object item) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:534
  at Xamarin.Forms.Internals.TemplatedItemsList`2[TView,TItem].CreateContent (System.Int32 index, System.Object item, System.Boolean insert) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:543
  at Xamarin.Forms.Internals.TemplatedItemsList`2[TView,TItem].GetOrCreateContent (System.Int32 index, System.Object item) [0x00023] in D:\a\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:602
  at Xamarin.Forms.Internals.TemplatedItemsList`2[TView,TItem].get_Item (System.Int32 index) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:337
  at Xamarin.Forms.Platform.Android.ListViewAdapter.GetCellsFromPosition (System.Int32 position, System.Int32 take) [0x0003b] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:539
  at Xamarin.Forms.Platform.Android.ListViewAdapter.GetCellForPosition (System.Int32 position) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:454
  at Xamarin.Forms.Platform.Android.ListViewAdapter.GetView (System.Int32 position, Android.Views.View convertView, Android.Views.ViewGroup parent) [0x0006d] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:225
  at Android.Widget.BaseAdapter.n_GetView_ILandroid_view_View_Landroid_view_ViewGroup_ (System.IntPtr jnienv, System.IntPtr native__this, System.Int32 position, System.IntPtr native_convertView, System.IntPtr native_parent) [0x0001a] in <11f101b564894ca7af6c482ddc51c698>:0
  at at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.74(intptr,intptr,int,intptr,intptr)
System.InvalidCastException:指定的强制转换无效。
at(包装器castclass)System.Object.\uuuuCastClass\u和\uCache(Object,intptr,intptr)
位于D:\a\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:534中的Xamarin.Forms.Internals.TemplatedItemsList`2[TView,TItem].ActivateContent(System.Int32索引,System.Object项)[0x00000]
位于D:\a\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:543中的Xamarin.Forms.Internals.TemplatedItemsList`2[TView,TItem].CreateContent(System.Int32索引,System.Object项,System.Boolean插入)[0x00000]
位于D:\a\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:602中的Xamarin.Forms.Internals.TemplatedItemsList`2[TView,TItem].GetOrCreateContent(System.Int32索引,System.Object项)[0x00023]
在Xamarin.Forms.internal.TemplatedItemsList`2[TView,TItem].get_项(System.Int32索引)[0x00000]中的D:\a\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:337
位于D:\a\1\s\Xamarin.Forms.Platform.Android.ListViewAdapter.GetCellsFromPosition(System.Int32 position,System.Int32 take)[0x0003b]中的Xamarin.Forms.Platform.Android\renders\ListViewAdapter.cs:539
位于D:\a\1\s\Xamarin.Forms.Platform.Android.ListViewAdapter.GetCellForPosition(System.Int32位置)[0x00000]中的Xamarin.Forms.Platform.Android\renders\ListViewAdapter.cs:454
位于D:\a\1\s\Xamarin.Forms.Platform.Android.ListViewAdapter.GetView中的Xamarin.Forms.Platform.Android.ListViewAdapter.GetView(System.Int32位置,Android.Views.View转换视图,Android.Views.ViewGroup父级)[0x0006d]
在Android.Widget.BaseAdapter.n_GetView_ILandroid_view_view_view_Landroid_view_view_组(System.IntPtr jnienv,System.IntPtr native__此,System.Int32位置,System.IntPtr native_convertView,System.IntPtr native_parent)[0x0001a]in:0
at(包装器动态方法)Android.Runtime.DynamicMethodNameCounter.74(intptr,intptr,int,intptr,intptr)

我认为您必须通知属性已更改,例如,为您的类实现INotifyPropertyChanged接口。