ObservableCollection 不要在 CodeBehind 中绑定
ObservableCollection don't Binding in CodeBehind
ObservableCollection 不绑定
大家好,
我在 Xamarin Forms 中使用 ObservableCollection 的一个问题上需要帮助。
我需要构建一个 "Custom ListView" 以使 TabIndex 在我的屏幕上正常工作。
为此,我在后面的代码中创建:
- 滚动视图
- 标签
- 条目
屏幕渲染正常,唯一的问题是在一个 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;
}
我对这个问题有点困惑。但我相信您在将条目添加到列表视图时遇到问题。您的 Observable 集合数据未填充到 Listview 中。
我写了一篇关于创建多选列表视图的 post。您只看到我如何创建列表视图并将可观察集合的内容绑定到它。
请看这里:https://androidwithashray.blogspot.com/2018/03/multiselect-list-view-using-xamarin.html
请注意,对于列表视图,如果内容更适合屏幕,则不需要滚动视图,因为列表视图会自动滚动。
希望对您有所帮助!!
为了解决我的问题,我使用:
代码隐藏
#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
ObservableCollection 不绑定 大家好,
我在 Xamarin Forms 中使用 ObservableCollection 的一个问题上需要帮助。 我需要构建一个 "Custom ListView" 以使 TabIndex 在我的屏幕上正常工作。 为此,我在后面的代码中创建: - 滚动视图 - 标签 - 条目
屏幕渲染正常,唯一的问题是在一个 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;
}
我对这个问题有点困惑。但我相信您在将条目添加到列表视图时遇到问题。您的 Observable 集合数据未填充到 Listview 中。
我写了一篇关于创建多选列表视图的 post。您只看到我如何创建列表视图并将可观察集合的内容绑定到它。
请看这里:https://androidwithashray.blogspot.com/2018/03/multiselect-list-view-using-xamarin.html
请注意,对于列表视图,如果内容更适合屏幕,则不需要滚动视图,因为列表视图会自动滚动。
希望对您有所帮助!!
为了解决我的问题,我使用:
代码隐藏
#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