Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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
Xamarin 可观测集合中的动态记录_Xamarin_Xamarin.forms_Xamarin.android - Fatal编程技术网

Xamarin 可观测集合中的动态记录

Xamarin 可观测集合中的动态记录,xamarin,xamarin.forms,xamarin.android,Xamarin,Xamarin.forms,Xamarin.android,这是我的模型 public class QMSRejection { public string Id { get; set; } public string CardTypeIcon { get; set; } public string Date { get; set; } public string ShiftMasterId { get; set; } } 这是我的viewmodel,其中有一个函数调用一个api,该api包含日期、Id、进程等字段

这是我的模型

public class QMSRejection
{
    public string Id { get; set; }

    public string CardTypeIcon { get; set; }

    public string Date { get; set; }

    public string ShiftMasterId { get; set; }
}
这是我的viewmodel,其中有一个函数调用一个api,该api包含日期、Id、进程等字段

public class RejectionDetailsViewModel : BaseViewModel
{
    public ObservableCollection<QMSRejection> RejDetails { get; set; }

    public RejectionDetailsViewModel()
    {
        Function();
    }

    public async void Function()
    {        
        string url = AppSettingsManager.Settings["RejectionUrl"] + "GetList?strkey=30/08/2020";// + DateTime.Now.ToShortDateString();
        var data = await url.GetJsonAsync<List<QMSRejection>>();

        this.RejDetails = new ObservableCollection<QMSRejection>();
        {
            foreach (QMSRejection qms in data)
            {
                this.RejDetails.Add(new QMSRejection()
                {
                    Process = qms.Process,
                    ProductionReferenceId = qms.ProductionReferenceId,
                    Shift = qms.Shift,
                    BackgroundGradientStart = "#d54381",
                    BackgroundGradientEnd = "#7644ad",
                    CardTypeIcon = "Card.png"

                });
            }
        }
    }
}
公共类拒绝详细信息视图模型:BaseViewModel
{
公共可观测集合详细信息{get;set;}
公共拒绝详细信息查看模型()
{
函数();
}
公共异步void函数()
{        
字符串url=AppSettingsManager.Settings[“RejectionUrl”]+“GetList?strkey=30/08/2020”//+DateTime.Now.ToShortDateString();
var data=await url.GetJsonAsync();
this.RejDetails=新的ObservableCollection();
{
foreach(数据中的qms映射qms)
{
this.RejDetails.Add(新的QMSRejection()
{
过程=质量管理体系过程,
ProductionReferenceId=qms.ProductionReferenceId,
班次=质量管理体系班次,
BackgroundGradientStart=“#d54381”,
BackgroundGradientEnd=“#7644ad”,
CardTypeIcon=“Card.png”
});
}
}
}
}
这个视图模型在xaml中被调用,我必须在itemtemplate中显示这些记录并调用数据模板

我在viewmodel中接收到错误的强制类型转换,我不确定我的方式是否正确,因为我必须显示所有记录!假设100条记录,那么在可观察的集合中有100条动态记录

我打电话给xaml的方式如下:

  <ContentPage.Content>
        <Grid RowSpacing="0" ColumnSpacing="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <!--  ListView displaying saved cards  -->
            <ListView:SfListView x:Name="myCards" AutoFitMode="Height" IsScrollBarVisible="False" ItemSpacing="16,24,16,0" 
                                 SelectionGesture="Tap" SelectionBackgroundColor="Transparent" 
                                 SelectionMode="Single" AllowSwiping="True" 
                               ItemTapped="MyCards_tapped" ItemsSource="{Binding RejDetails}">
                <ListView:SfListView.ItemTemplate>
                    <DataTemplate>
                        <cards:SfCardView CornerRadius="4" HasShadow="True" HeightRequest="150" WidthRequest="300" 
                                              HorizontalOptions="CenterAndExpand">
                            <cards:SfCardView.Content>
                                <Grid>
                                    <gradient:SfGradientView>
                                        <gradient:SfGradientView.BackgroundBrush>
                                            <gradient:SfLinearGradientBrush>
                                                <gradient:SfLinearGradientBrush.GradientStops>
                                                    <gradient:SfGradientStop Color="{Binding BackgroundGradientStart}" 
                                                       Offset="0.0"/>
                                                    <gradient:SfGradientStop Color="{Binding BackgroundGradientEnd}" 
                                                       Offset="1.0"/>
                                                </gradient:SfLinearGradientBrush.GradientStops>
                                            </gradient:SfLinearGradientBrush>
                                        </gradient:SfGradientView.BackgroundBrush>
                                    </gradient:SfGradientView>

                                    <Grid RowSpacing="27" Margin="16,20" >
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                        </Grid.RowDefinitions>

                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>

                                        <!-- Process -->
                                        <Label Grid.Row="0" Grid.ColumnSpan="1" HorizontalOptions="Start"  
                                           VerticalOptions="Center" 
                                           Style="{StaticResource CardLabel}" 
                                           Margin="0,3" Text="{Binding Process}"  />

                                  
                                        <!-- ProductionReference Id -->
                                        <Label Grid.Row="0" Grid.ColumnSpan="3" Text="{Binding ProductionReferenceId}" 
                                         FontSize="16" HorizontalOptions="End"
                                        FontFamily="{StaticResource Montserrat-Medium}" 
                                        TextColor="{DynamicResource Gray-White}"
                                        LineHeight="{OnPlatform Default=-1, Android=1.5}" />

                                        <Grid Grid.Row="1" Grid.ColumnSpan="3">
                                            <!--  Shift  -->
                                            <StackLayout Grid.Column="0" Spacing="0">
                                                <Label Text="{Binding Shift}" Style="{StaticResource CardLabel}" />
                                            </StackLayout>

                                           </Grid>
                                    </Grid>
                                </Grid>
                            </cards:SfCardView.Content>
                        </cards:SfCardView>
                    </DataTemplate>
                </ListView:SfListView.ItemTemplate>
            </ListView:SfListView>

数据的类型为
列表
,您正试图将其强制转换为
QMSRejection
实例

您应该像这样实例化集合:

this.RejDetails = new ObservableCollection<QMSRejection>();
您的属性RejDetails应使用INotifyPropertyChanged接口:

 private ObservableCollection rejDetails = null;
 public ObservableCollection RejDetails 
 { 
     get {return rejDetails;}
     set 
     {
         if(rejDetails != value)
         {
            rejDetails = value;
            OnPropertyChanged();
         }
     } 
}
以下是如何实现该接口:

数据是一个QMSRejection列表,您正试图将其强制转换为QMSRejection实例。因此,我必须做些什么才能获取所有变量并将其传递到可观察的Collectionary中?您是否收到任何运行时消息,其中提到未找到绑定?我如您所述执行了操作,但我不确定它为什么不显示!我已经更新了问题看看!您是否设置了绑定上下文?RejDetails是一个实现INotifyPropertyChanged的属性?请参阅我的更新我所做的一切我绑定了itemsource并设置了IndividualDaul元素的绑定!我是xamarin的新手,所以我不确定我是否需要设置INotify属性和所有内容?您必须告诉视图将在哪里选择值。因此在您的视图中:this.BindingContext=yourViewmodel;您的RejDetails需要是一个使用INotifyPropertyChanged通知的属性,因为视图必须知道您何时创建了列表的新实例。我是这样做的。您能告诉我如何才能做到INotify一个吗?就像我必须在viewmodel中这样做一样,方法是什么?
 private ObservableCollection rejDetails = null;
 public ObservableCollection RejDetails 
 { 
     get {return rejDetails;}
     set 
     {
         if(rejDetails != value)
         {
            rejDetails = value;
            OnPropertyChanged();
         }
     } 
}