C# 我更新了ObservableCollection列表,但UI没有从选项卡页面更新问题
我更新了ObservableCollection列表,但UI未从选项卡页面更新 我也会清除列表,然后分配新列表,列表的值会改变,但UI不会更新C# 我更新了ObservableCollection列表,但UI没有从选项卡页面更新问题,c#,xamarin,xamarin.forms,observablecollection,C#,Xamarin,Xamarin.forms,Observablecollection,我更新了ObservableCollection列表,但UI未从选项卡页面更新 我也会清除列表,然后分配新列表,列表的值会改变,但UI不会更新 public ObservableCollection<Dashboard> DetailsList { get; set; } = new ObservableCollection<Dashboard>(); //API Call details = await _clientAPI.getDashboardDe
public ObservableCollection<Dashboard> DetailsList { get; set; } = new ObservableCollection<Dashboard>();
//API Call
details = await _clientAPI.getDashboardDetails(id);
if (details != null)
{
DetailsList.Clear();
foreach (var item in details)
{
DetailsList.Add(item);
}
}
YourGridView.ItemSource=DetailsList
你漏了这一行。这段代码-它只是将ObservableCollection分配给您想要显示数据的gridview
谢谢 我想你没有抓住上下文。在下面添加代码
this.BindingContext = this;
我做了一个代码示例供您参考。我不确定你的模型是什么,我用一个简单的模型来测试
Page1.xaml.cs
public partial class Page1 : ContentPage
{
public ObservableCollection<Dashboard> DetailsList { get; set; }
public Page1()
{
InitializeComponent();
DetailsList = new ObservableCollection<Dashboard>()
{
new Dashboard(){ Name="AA", Country="CountryA"},
new Dashboard(){ Name="BB", Country="CountryB"},
};
this.BindingContext = this;
}
private void btnUpdate_Clicked(object sender, EventArgs e)
{
List<Dashboard> details = new List<Dashboard>();
details.Add(new Dashboard() { Name = "CC", Country = "CountryC" });
details.Add(new Dashboard() { Name = "DD", Country = "CountryD" });
if (details != null)
{
DetailsList.Clear();
foreach (var item in details)
{
DetailsList.Add(item);
}
}
}
}
public class Dashboard
{
public string Name { get; set; }
public string Country { get; set; }
}
Xaml:
更新:
这在普通页面中运行良好,但在选项卡式页面中不起作用。我在选项卡式页面上测试的代码,它运行良好。
public partial class Page1 : ContentPage
{
public ObservableCollection<Dashboard> DetailsList { get; set; }
public Page1()
{
InitializeComponent();
DetailsList = new ObservableCollection<Dashboard>()
{
new Dashboard(){ Name="AA", Country="CountryA"},
new Dashboard(){ Name="BB", Country="CountryB"},
};
this.BindingContext = this;
}
private void btnUpdate_Clicked(object sender, EventArgs e)
{
List<Dashboard> details = new List<Dashboard>();
details.Add(new Dashboard() { Name = "CC", Country = "CountryC" });
details.Add(new Dashboard() { Name = "DD", Country = "CountryD" });
if (details != null)
{
DetailsList.Clear();
foreach (var item in details)
{
DetailsList.Add(item);
}
}
}
}
public class Dashboard
{
public string Name { get; set; }
public string Country { get; set; }
}
<ContentPage.Content>
<StackLayout>
<Button
x:Name="btnUpdate"
Clicked="btnUpdate_Clicked"
Text="Update" />
<ListView ItemsSource="{Binding DetailsList}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding Name}" />
<Label Text="{Binding Country}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>