如何在Xamarin.Forms中启用通过绑定启用多选列表视图的开关? [英] How to enable switch on multi-select listview with binding in Xamarin.Forms?

查看:72
本文介绍了如何在Xamarin.Forms中启用通过绑定启用多选列表视图的开关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了这个文章/示例代码,这是制作待办事项清单的方法.如果浏览该代码,它将使用绑定来确定任务是否完成.我要完成的工作与待办事项清单的概念相同.

I found this article/sample code it is how to make a to-do list. If you browse the code it is using bindings to determine if the task is done or not. What I am trying to accomplish is the same concept as the to-do list.

请访问我的 Github 存储库,获取我的完整代码:

Please visit my Github Repository for my full code:

这是我的应用逻辑:

Here is my app logic:

  1. 在加载应用程序后,该应用程序将显示所有活动列表(例如,步行,爬山,漂流等).
  2. 用户将选择1个或多个活动.
  3. 用户会将表单保存在本地数据库中.
  4. 如果用户要删除或更新他们选择的活动,则该用户将转到他们提交的表单列表,并且当用户选择要更新的表单时,该用户将被重定向到具有以下内容的活动列表:
  5. 启用了他们选择的活动的切换(表示存储在活动选定表中的活动).
  1. Upon loading of the application the app will show all the list of activities (e.g. Walking, climbing, rafting, etc.).
  2. The user will select 1 or more activities.
  3. The user will save the form in the local database.
  4. If the user wants to remove or update an activity they have chosen the user will go to the list of forms they have submitted and when the user chose what form the want to update the user will be redirected to the list of activities with the switch of the activities they have chosen (meaning the activities stored in the activities selected table) enabled.

基本上像一个待办事项清单.当用户检查哪些活动已经完成时,将检查或不检查状态.我对绑定没有太多了解,我很难在我的应用程序中实现它.

Basically like a to-do list. When the user checked what activities have already done the state will be checked or not. I don't have much knowledge in bindings I am having a hard time to implement it on my application.

这是我的XAML代码:

Here is my XAML Code:

<ListView SeparatorVisibility="None" x:Name="lstActivity" ItemSelected="lstActivity_ItemSelected" HasUnevenRows="True">
  <ListView.ItemTemplate>
       <DataTemplate>
            <ViewCell>
                <Frame StyleClass="lstframe" CornerRadius="0" BorderColor="Transparent" HasShadow="False">
                     <StackLayout StyleClass="lstContainer" VerticalOptions="CenterAndExpand">
                          <Grid>
                             <Label StyleClass="lstActivityName" VerticalOptions="Center" Grid.Row="0" Grid.Column="0" Text="{Binding ActivityDescription}">
                                  <Label.FontFamily>
                                        <OnPlatform x:TypeArguments="x:String">
                                             <On Platform="Android" Value="Poppins-Regular.otf#Poppins-Regular"/>
                                        </OnPlatform>
                                   </Label.FontFamily>
                             </Label>
                             <Switch Grid.Row="0" Grid.Column="1" IsToggled="{Binding Selected}" />
                           </Grid>
                      </StackLayout>
                 </Frame>
             </ViewCell>
       </DataTemplate>
  </ListView.ItemTemplate>

这是我列出所有未启用状态的活动的方式:

Here is how I list all the activities without enabled state:

var db = DependencyService.Get<ISQLiteDB>();
var conn = db.GetConnection();

var getActivity = conn.QueryAsync<ActivityTable>("SELECT * FROM tblActivity WHERE Deleted != '1' ORDER BY ActivityDescription");
var resultCount = getActivity.Result.Count;

if (resultCount > 0)
{
   result = getActivity.Result;
   lstActivity.ItemsSource = result;

   lstActivity.IsVisible = true;
}
else
{
    lstActivity.IsVisible = false;
}

这是我的ActivityTable类(这是我存储活动列表的位置):

Here is my ActivityTable Class (Here is where I store the list of the activity):

namespace TBSApp.Data
{
    [Table("tblActivity")]
    public class ActivityTable
    {
        [PrimaryKey]
        public string ActivityID { get; set; }
        public string ActivityDescription { get; set; }
        public string RecordLog { get; set; }
        public DateTime LastSync { get; set; }
        public DateTime LastUpdated { get; set; }
        public int Deleted { get; set; }
        public int Checked { get; set; }
        public bool Selected { get; set; }
    }
}

这是我的CAFActivityTable(我存储所选活动的位置):

Here is my CAFActivityTable (Where I store the selected activity):

[Table("tblCAFActivity")]
public class CAFActivityTable
{
    public string CAFNo { get; set; }
    public string ActivityID { get; set; }
    public DateTime LastSync { get; set; }
    public DateTime LastUpdated { get; set; }
    public int Deleted { get; set; }
    public int Checked { get; set; }
}

推荐答案

似乎您已将Switch的IsToggled绑定到模型的Selected.因此,您的ActivityTable模型应包含布尔值Selected属性:

It seems you have bound the Switch's IsToggled to your model's Selected. So your ActivityTable model should contain a bool value Selected property:

public class ActivityTable : INotifyPropertyChanged
{
    string activityDescription;
    public string ActivityDescription
    {
        get => activityDescription;
        set
        {
            if (activityDescription != value)
            {
                activityDescription = value;
                onPropertyChanged();
            }
        }
    }

    bool selected;
    public bool Selected
    {
        get => selected;
        set
        {
            if (selected != value)
            {
                selected = value;
                onPropertyChanged();
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    void onPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

在实现INotifyPropertyChanged接口之后,您可以通过Selected更改开关的状态.

You could change the switch's state through Selected after implementing the INotifyPropertyChanged interface.

更新:

ActivityTable中的元素与CAFActivityTable中的项目进行比较.当您发现该商品具有相同的ActivityID时,请更改当前商品的选定状态:

Compare the elements in ActivityTable to the items in CAFActivityTable. When you find the item has the same ActivityID, change the current item's selected state:

var activityItems = await ActivityConnection.Table<ActivityTable>().ToListAsync();
var CAFItems = await CafConnection.Table<CAFActivityTable>().ToListAsync();

foreach (var item in activityItems)
{
    foreach (var CAFItem in CAFItems)
    {
        if (item.ActivityID == CAFItem.ActivityID)
        {
            item.Selected = true;
            break;
        }
    }
}
// Bind your list view's items source to this activityItems
// If you want to update the database use the code below
await ActivityConnection.UpdateAllAsync(activityItems);

这篇关于如何在Xamarin.Forms中启用通过绑定启用多选列表视图的开关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆