【发布时间】:2017-03-27 07:54:47
【问题描述】:
我在一个类中创建了一个列表视图,并从另一个类中调用了 delete 方法。如果我从另一个班级打电话,Listview 接到电话但不更新列表视图。但是当我在同一个班级内打电话时它会得到更新。如何解决这个问题?
namespace New
{
public partial class WishesPage : ContentPage
{
ListView listView = new ListView();
public WishesPage()
{
InitializeComponent();
var arr = JToken.Parse(ids);
foreach (var ite in arr.Children())
{
var itemProperties = ite.Children<JProperty>();
string contactElement = itemProperties.FirstOrDefault(x => x.Name == "contact").Value.ToString();
sample.Add(contactElement);
}
listView.ItemTemplate = new DataTemplate(typeof(CustomListCell));
listView.ItemsSource = sample;
Content = new StackLayout
{
Children =
{
listView,
}
};
}
public async Task delete(string wishid)
{
indicator.IsRunning = true;
var client = new HttpClient();
client.BaseAddress = new Uri("http:……”);
if (response == "success")
{
listView.ItemsSource = null;
listView.ItemsSource = sample;
}
}
}
public class CustomListCell : ViewCell
{
public CustomListCell()
{
wishIdLabel.SetBinding(Label.TextProperty, new Binding("contact"));
horizontalLayout.Children.Add(wishIdLabel);
var deleteAction = new MenuItem { Text = "Delete", IsDestructive = true };
deleteAction.Clicked += async (sender, e) =>
{
WishesPage wishes = new WishesPage();
wishes.delete(wishId);
};
ContextActions.Add(deleteAction);
}
}
}
【问题讨论】:
-
什么是“样本”?
-
它是一个元素数组。我使用 Modal 类创建。喜欢 (sample = new ObservableCollection
();) -
我不明白你为什么在 deleteAction.Clicked 中创建另一个 WishesPage... WishesPage wish = new WishesPage(); wish.delete(wishId);
-
只有我卡住了。。如果没有为 WishesPage 类创建对象,就无法调用 delete 方法。还有其他方法可以调用该方法吗?
-
移动ViewCell类中的delete方法
标签: class listview xamarin.forms itemsource