ObservableCollection不绑定 大家好,
在Xamarin Forms中使用的ObservableCollection的一个问题中,我需要帮助。 我需要为TabIndex构建一个“自定义ListView”,以便在屏幕上正常工作。 为此,我在后面的代码中创建: -ScrollView - 标签 -条目
屏幕渲染正常,只有我的问题是绑定到一个ObservableCollection中。
我使用OnPropertyChanged测试一个属性单个字符串可以正常工作,但是使用ObservableCollection无法工作。我尝试使用CollectionChanged,但成功了。
有人遇到同样的问题吗?现有解决方案吗?
ViewModel:
private ObservableCollection<RawData> rawDataList;
public ObservableCollection<RawData> RawDataList
{
get { return rawDataList; }
set { rawDataList = value; OnPropertyChanged("RawDataList"); }
}
隐藏代码
private void BuildRawDataTemplate()
{
var grid = BuildGrid();
#region Templates
grid.Children.Add(BuildScheduleHeaderTitleTemplate(), 0, 0); // Posição: Coluna: 0 | Linha: 0
grid.Children.Add(BuildScheduleDetailsSubtitleTemplate("Dados Brutos"), 0, 1); // Posição: Coluna: 0 | Linha: 1
#endregion
#region RawData
var scrollView = BuildScrollView();
var stackLayout = BuildStackLayout("DefaultLight", 10, StackOrientation.Vertical, null, LayoutOptions.Center);
rawDataCount = scheduleViewModel.RawDataList.Count();
foreach (var item in scheduleViewModel.RawDataList.Select((rawData, i) => new { i, rawData }))
{
stackLayout.Children.Add(GenerateLabel(item.rawData.Identification));
if (item.rawData.InfoTypeId == (int)InfoType.Equipment)
stackLayout.Children.Add(GenerateEquipmentsPicker(item.rawData, item.i, item.i == 0, rawDataCount == (item.i + 1)));
else
stackLayout.Children.Add(GenerateEntry(item.rawData, item.i, item.i == 0, rawDataCount == (item.i + 1)));
}
scrollView.Content = stackLayout;
grid.Children.Add(scrollView, 0, 2); // Posição: Coluna: 0 | Linha: 2
#endregion
#region Buttons
var stackLayoutFooterButtons = BuildStackLayout(string.Empty, null, StackOrientation.Horizontal, LayoutOptions.End, null);
stackLayoutFooterButtons.Children.Add(GeneratePrimaryButton("RESULTADOS", scheduleViewModel.VisibleResultCommand, rawDataCount++, scheduleViewModel.SelectedSampleBase.Finished));
stackLayoutFooterButtons.Children.Add(GeneratePrimaryButton("CALCULAR", scheduleViewModel.CalculateRawDataCommand, rawDataCount++, !scheduleViewModel.SelectedSampleBase.Synced));
stackLayoutFooterButtons.Children.Add(GeneratePrimaryButton("SALVAR", scheduleViewModel.SaveRawDataCommand, rawDataCount++, !scheduleViewModel.SelectedSampleBase.Synced));
grid.Children.Add(stackLayoutFooterButtons, 0, 3); // Posição: Coluna: 0 | Linha: 3
#endregion
Content = grid;
SetFocusOnFirstEntry();
}
private Entry GenerateEntry(RawData rawData, int tabIndex, bool firstElement = false, bool hasNextTabIndex = false)
{
var entry = new Entry();
entry.TabIndex = tabIndex;
entry.Text = rawData.DisplayValue;
entry.SetBinding(Entry.TextProperty, nameof(rawData.DisplayValue), BindingMode.TwoWay);
entry.StyleId = $"RawDataDynamicEntry_{tabIndex}";
entry.IsTabStop = true;
entry.IsEnabled = rawData.NotSynced;
if (rawData.InfoTypeId == (int)InfoType.Integer)
entry.Behaviors.Add(new NumericValidationBehavior());
if (firstElement)
entry.Focus();
// Temp
var focusOnReturnBehavior = new FocusOnReturnBehavior();
focusOnReturnBehavior.HasNextTabIndex = hasNextTabIndex;
entry.Behaviors.Add(focusOnReturnBehavior);
return entry;
}
答案 0 :(得分:1)
这个问题我有点困惑。但我相信您在将条目添加到列表视图时遇到问题。您的“可观察的”收集数据没有填充到Listview中。
我写了一篇关于创建多选列表视图的文章。您只是看到我如何创建一个列表视图并将可观察集合的内容绑定到该列表视图。
请在此处查看:https://androidwithashray.blogspot.com/2018/03/multiselect-list-view-using-xamarin.html
请注意,对于列表视图,如果内容更适合屏幕内部,则不需要滚动视图,因为列表视图会自动滚动。
希望这会有所帮助!
答案 1 :(得分:0)
为解决我的问题,我使用:
隐藏代码
#region Events Changed
private void EntryTextChanged(object sender, TextChangedEventArgs e)
{
var entryChanged = (Entry)sender;
selectedRawData.DisplayValue = entryChanged.Text;
}
private void PickerSelectedIndexChanged(object sender, System.EventArgs e)
{
var pickerChanged = (Picker)sender;
selectedRawData.DisplayValue = selectedItem.Identification;
}
#endregion