Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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# Xamarin:ObservableCollection没有';t自动更新列表视图_C#_Listview_Xamarin.forms - Fatal编程技术网

C# Xamarin:ObservableCollection没有';t自动更新列表视图

C# Xamarin:ObservableCollection没有';t自动更新列表视图,c#,listview,xamarin.forms,C#,Listview,Xamarin.forms,我有一个绑定到ViewModel中的ObservableCollection的ListView。 在初始化过程中,ListView项目将完美显示。 但是,当我试图在运行时更新ObservableCollection项的单个值时,listview中的链接项不会自动更新。仅当我滚动listView时,它才会更新。为什么会这样 代码如下: XAML <ListView.ItemTemplate> <DataTemplate>

我有一个绑定到ViewModel中的ObservableCollection的ListView。 在初始化过程中,ListView项目将完美显示。 但是,当我试图在运行时更新ObservableCollection项的单个值时,listview中的链接项不会自动更新。仅当我滚动listView时,它才会更新。为什么会这样

代码如下:

XAML

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Frame Style="{StaticResource frameListView}" >
                            <StackLayout Margin="-15">

                                <Label Text="{Binding Articolo.Descrizione}"
                                       Style="{StaticResource labelDescrizioneStyle}" />

                                    <StackLayout Orientation="Horizontal"
                                                 HorizontalOptions="End" VerticalOptions="Center">

                                        <Button x:Name="removeButton" 
                                                VerticalOptions="Center" HorizontalOptions="Center"
                                                Text="-"
                                                Font="20"
                                                WidthRequest="45" FontAttributes="Bold"
                                                Style="{StaticResource buttonNeutroStyle}"  
                                                Command="{Binding Source={x:Reference mieiAcquistiStack}, Path=BindingContext.AggiungiCommand}"                                                   
                                                CommandParameter="{Binding .}" />       

                                        <StackLayout HorizontalOptions="FillAndExpand"
                                                     VerticalOptions="FillAndExpand" >

                                            <Label Text="{Binding QtaEdit}" 
                                                   TextColor="Black"
                                                   FontSize="18" FontAttributes="Bold"
                                                   WidthRequest="40"
                                                   HorizontalTextAlignment="Center" VerticalTextAlignment="Center"
                                                   VerticalOptions="FillAndExpand"/>
                                        </StackLayout>

                                        <Button x:Name="addButton" 
                                                VerticalOptions="Center" HorizontalOptions="Center"
                                                Text="+"
                                                WidthRequest="45" FontAttributes="Bold"   
                                                Style="{StaticResource buttonNeutroStyle}"   
                                                Command="{Binding Source={x:Reference mieiAcquistiStack}, Path=BindingContext.AggiungiCommand}"   
                                                CommandParameter="{Binding .}" />
                                    </StackLayout>
                            </StackLayout>
                        </Frame>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>

视图模型

公共静态可观察收集获取者;
公众可观察的采集采集者
{
获取{return acquistiList;}
设置
{
if(获取者!=值)
{
获得者=价值;
OnPropertyChanged();
}
}
}
私人空间聚集(RigaStoricoModel prodotto,ParametriUM ParametriUM)
{
double esistenzaUMPredef=参数um.esistenzaUMPredef.GetValueOrDefault(0);
如果(esistenzaUMPredef>0)
{
双qtaMinima=parametriUM.qtaMinima.getValuerDefault(1);

如果(prodotto.QtaEdit+qtaMinima您是否希望获得如下结果

如果是这样,您应该像Silvermind所说的那样,在
RigaStoricoModel
中实现
INotifyPropertyChanged
接口

下面是
MyViewModel.cs
code

   public class RigaStoricoModel: INotifyPropertyChanged
    {

        private double _qtaEdit;
        public double QtaEdit
        {
            set
            {

                _qtaEdit = value;

                OnPropertyChanged("QtaEdit");

            }
            get => _qtaEdit;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
我使用
aggiungingcommand
进行测试(减少或增加)


与集合本身相关的更新由
Observablecollection
INotifyCollectionChanged
事件管理。与集合内对象相关的更新应实现
INotifyPropertyChanged
并正确绑定。因此,
RigaStoricoModel
是否实现
INotifyPropertyChanged
?向我们展示code.Thx。现在它可以工作了。我错过了实现
INotifyPropertyChanged
RigaStoricoModel
的机会。现在它可以工作了。我错过了实现
INotifyPropertyChanged
RigaStoricoModel
。非常感谢!
    public class MyViewModel
    {
        public static ObservableCollection<RigaStoricoModel> acquistiList;
        public ObservableCollection<RigaStoricoModel> AcquistiList
        {
            get { return acquistiList; }
            set
            {
                if (acquistiList != value)
                {
                    acquistiList = value;
                  //  OnPropertyChanged();
                }
            }
        }

        public ICommand AggiungiCommand { protected set; get; }
        public ICommand AggiungiCommand2 { protected set; get; }

        //  public int MyProperty { get; set; }

        public MyViewModel()
        {
            AcquistiList = new ObservableCollection<RigaStoricoModel>();
            AcquistiList.Add(new RigaStoricoModel() { QtaEdit=0.28 });
            AcquistiList.Add(new RigaStoricoModel() { QtaEdit = 0.38 });
            AcquistiList.Add(new RigaStoricoModel() { QtaEdit = 0.48 });
            AcquistiList.Add(new RigaStoricoModel() { QtaEdit = 0.58 });
            AcquistiList.Add(new RigaStoricoModel() { QtaEdit = 0.68 });

            AggiungiCommand2=new Command(async (key) =>
            {
                RigaStoricoModel model = key as RigaStoricoModel;
                model.QtaEdit += 0.1;

            });
            AggiungiCommand = new Command(async (key) =>
            {
                RigaStoricoModel  model= key as RigaStoricoModel;
                model.QtaEdit -= 0.1;

            });
        }
    }
}
    <StackLayout>
        <!-- Place new controls here -->
        <ListView ItemsSource="{Binding AcquistiList}" x:Name="mieiAcquistiStack" HasUnevenRows="True" >

            <ListView.ItemTemplate>
                <DataTemplate> 
                    <ViewCell>
                        <Frame  >
                            <StackLayout Margin="-15" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">

                                <Label Text="{Binding Articolo.Descrizione}"
                                       />

                                <StackLayout Orientation="Horizontal"
                                                 HorizontalOptions="End" VerticalOptions="Center">

                                    <Button x:Name="removeButton" 
                                                VerticalOptions="Center" HorizontalOptions="Center"
                                                Text="-"
                                                Font="20"
                                                WidthRequest="45" FontAttributes="Bold"

                                                Command="{Binding Source={x:Reference mieiAcquistiStack}, Path=BindingContext.AggiungiCommand}"                                                   
                                                CommandParameter="{Binding .}" />

                                    <StackLayout HorizontalOptions="FillAndExpand"
                                                     VerticalOptions="FillAndExpand" >

                                        <Label Text="{Binding QtaEdit}" 
                                                   TextColor="Black"
                                                   FontSize="18" FontAttributes="Bold"
                                                   WidthRequest="40"
                                                   HorizontalTextAlignment="Center" VerticalTextAlignment="Center"
                                                   VerticalOptions="FillAndExpand"/>
                                    </StackLayout>

                                    <Button x:Name="addButton" 
                                                VerticalOptions="Center" HorizontalOptions="Center"
                                                Text="+"
                                                WidthRequest="45" FontAttributes="Bold"   

                                                Command="{Binding Source={x:Reference mieiAcquistiStack}, Path=BindingContext.AggiungiCommand2}"   
                                                CommandParameter="{Binding .}" />
                                </StackLayout>
                            </StackLayout>
                        </Frame>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
    public MainPage()
        {
            InitializeComponent();

            this.BindingContext = new MyViewModel();
        }