如何将所有Switch设置为true更改为ListView?

时间:2019-04-24 18:55:44

标签: xamarin

我有一个具有ListView的页面,并且具有切换到用户选择他想要的寄存器的页面。然后,我创建了一个主Switch,用户单击了其中,我想将所有其他Switch更改为true或false的ListView。

我该怎么做?

XAML

<Switch x:Name="IsSelectAll" Toggled="OnChangeSelectAll" IsToggled="False" HorizontalOptions="EndAndExpand" ></Switch>

<ListView x:Name="GrupoDeProdutos"
                      SeparatorVisibility="Default">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Horizontal">
                                <StackLayout HorizontalOptions="StartAndExpand">
                                    <Label Text="{Binding title}" HorizontalOptions="StartAndExpand" TextColor="Default"></Label>
                                </StackLayout>
                                <Switch x:Name="{Binding id}" IsToggled="{Binding active}"></Switch>
                            </StackLayout>                           
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

控制器

public partial class PopUpGrupoProdutoUsuario : PopupPage{        
        private List<TableModel> listaModel = new List<TableModel>();       

        public PopUpGrupoProdutoUsuario()
        {
            InitializeComponent();            
        }


        //select all switch true/false
        private void OnChangeSelectAll(object sender, ToggledEventArgs args){            
            foreach (TableModel t in this.listaModel){                 
                t.active = args.Value;                
            }            
        }

        //class listview model
        public class TableModel{
            public int id                       { get; set; }
            public String title                 { get; set; }
            public Boolean active                { get; set; }

            public TableModel() { }

            public TableModel(int id, String title, Boolean active) {
                this.id = id;
                this.title = title;
                this.active= active;
            }
        }



    }

编辑

//class listview model
        public class TableModel : INotifyPropertyChanged        {
            public event PropertyChangedEventHandler PropertyChanged;

            public int id
            {
                get { return id; }
                set
                {
                    if(id != value)
                    {
                        id = value;
                        NotifyPropertyChanged();
                    }
                }
            }


            public String title
            {
                get { return title; }
                set
                {
                    if(title != value)
                    {
                        title = value;
                        NotifyPropertyChanged();
                    }
                }
            }


            public Boolean ativo
            {
                get { return ativo; }
                set {
                    if(ativo != value){
                        ativo = value;
                        NotifyPropertyChanged();
                    }
                }
            }

            public TableModel() {
                if (DesignMode.IsDesignModeEnabled)
                {
                    this.id = 0;
                    this.title = "default";
                    this.ativo = false;
                }
            }

            public TableModel(int id, String title, Boolean ativo) {
                if (DesignMode.IsDesignModeEnabled){
                    this.id = id;
                    this.title = title;
                    this.ativo = ativo;
                }                
            }

            protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = ""){
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }


        }

错误

=================================================================
    Native Crash Reporting
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================
04-22 11:34:59.495 E/mono-rt (20272): /proc/self/maps:
=================================================================
    Basic Fault Adddress Reporting

04-22 11:34:59.495 E/mono-rt (20272): 12c00000-32c00000 rw-p 00000000 00:01 10738                              /dev/ashmem/dalvik-main space (region space) (deleted)
04-22 11:34:59.495 E/mono-rt (20272): 5af61000-5af65000 r-xp 00000000 08:06 331                                /system/bin/app_process32
04-22 11:34:59.495 E/mono-rt (20272): 5af66000-5af67000 r--p 00004000 08:06 331                                /system/bin/app_process32
04-22 11:34:59.495 E/mono-rt (20272): 5af67000-5af68000 rw-p 00000000 00:00 0 
04-22 11:34:59.495 E/mono-rt (20272): 6f101000-6f2e7000 rw-p 00000000 08:13 105905                             /data/dalvik-cache/x86/system@framework@boot.art
04-22 11:34:59.495 E/mono-rt (20272): 6f2e7000-6f3a4000 rw-p 00000000 08:13 105906                             /data/dalvik-cache/x86/system@framework@boot-core-libart.art
04-22 11:34:59.495 E/mono-rt (20272): 6f3a4000-6f3e1000 rw-p 00000000 08:13 105907                             /data/dalvik-cache/x86/system@framework@boot-conscrypt.art=================================================================
Memory around native instruction pointer (0xc318029f):0xc318028f  8b 4d f8 8d 64 24 00 90 90 90 8b 45 08 89 04 24  .M..d$.....E...$
0xc318029f  e8 b4 ff ff ff 89 45 f0 8b 4d f8 8d 64 24 00 90  ......E..M..d$..
0xc31802af  90 90 8b 45 f0 8b f0 8b 4d f8 8d 64 24 00 90 90  ...E....M..d$...
0xc31802bf  90 8b c6 8b 4d f8 8d 64 24 00 90 90 90 8d 65 fc  ....M..d$.....e.

No native Android stacktrace (see debuggerd output).

=================================================================
    Managed Stacktrace:
=================================================================
      at TableModel:get_title <0x00047>
      at TableModel:get_title <0x0004b>
      at TableModel:get_title <0x0004b>
      at TableModel:get_title <0x0004b>
      at TableModel:get_title <0x0004b>
      at TableModel:get_title <0x0004b>
      at TableModel:get_title <0x0004b>
      at TableModel:get_title <0x0004b>
      at TableModel:get_title <0x0004b>
      at TableModel:get_title <0x0004b>
      at TableMod

1 个答案:

答案 0 :(得分:0)

您的代码将以无限循环结尾。您的“标题”属性是原因。创建一个私有成员“ title”和一个公共属性“ Title”。那应该可以解决问题。