我发现这个article/sample code是如何制作待办事项清单的方法。如果您浏览该代码,它将使用绑定来确定任务是否完成。我要完成的工作与待办事项列表的概念相同。
请访问我的Github存储库以获取我的完整代码:
这是我的应用逻辑:
基本上像一个待办事项清单。当用户检查哪些活动已经完成时,将检查或不检查状态。我对绑定没有太多了解,我很难在应用程序上实现它。
这是我的XAML代码:
<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>
这是我列出所有未启用状态的活动的方式:
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类(这是我存储活动列表的位置):
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(我存储所选活动的位置):
[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; }
}
答案 0 :(得分:1)
似乎您已将Switch的IsToggled
绑定到模型的Selected
。因此,您的ActivityTable
模型应包含布尔值Selected
属性:
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));
}
}
在实现Selected
接口之后,您可以通过INotifyPropertyChanged
更改开关的状态。
更新:
将ActivityTable
中的元素与CAFActivityTable
中的项目进行比较。当您发现该商品具有相同的ActivityID时,请更改当前商品的选定状态:
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);