Class 如何在Xamarin表单中从另一个类更新列表视图?
我在一个类中创建了一个列表视图,并从另一个类中调用了delete方法。Listview正在获取调用,但如果我从另一个类调用,则不会更新列表视图。但是当我在同一个类中调用时,它会得到更新。如何解决这个问题Class 如何在Xamarin表单中从另一个类更新列表视图?,class,listview,xamarin.forms,itemsource,Class,Listview,Xamarin.forms,Itemsource,我在一个类中创建了一个列表视图,并从另一个类中调用了delete方法。Listview正在获取调用,但如果我从另一个类调用,则不会更新列表视图。但是当我在同一个类中调用时,它会得到更新。如何解决这个问题 namespace New { public partial class WishesPage : ContentPage { ListView listView = new ListView(); public WishesPage()
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);
}
}
}
名称空间新建
{
公共部分类WishesPage:ContentPage
{
ListView ListView=新建ListView();
公众愿望页()
{
初始化组件();
var arr=JToken.Parse(ids);
foreach(arr.Children()中的变量)
{
var itemProperties=ite.Children();
字符串contactElement=itemProperties.FirstOrDefault(x=>x.Name==“contact”).Value.ToString();
示例。添加(contactElement);
}
listView.ItemTemplate=新数据模板(typeof(CustomListCell));
listView.ItemsSource=示例;
内容=新的堆栈布局
{
孩子们=
{
listView,
}
};
}
公共异步任务删除(字符串wishid)
{
indicator.IsRunning=true;
var client=新的HttpClient();
client.BaseAddress=新Uri(“http:…”;
如果(响应=“成功”)
{
listView.ItemsSource=null;
listView.ItemsSource=示例;
}
}
}
公共类CustomListCell:ViewCell
{
公共CustomListCell()
{
wishIdLabel.SetBinding(Label.TextProperty,新绑定(“联系人”);
水平布局.Children.Add(wishIdLabel);
var deleteAction=new MenuItem{Text=“Delete”,IsDestructive=true};
deleteAction.Clicked+=async(发送方,e)=>
{
WishesPage愿望=新建WishesPage();
删除(wishId);
};
添加(删除操作);
}
}
}
我已经更新了一些说明如何在ViewCell中使用命令的代码
在您的情况下,应该将ViewCell的构造移动到ContentPage中
lv.ItemTemplate = new DataTemplate(() =>
{
StackLayout slView = new StackLayout();
Label lDesc = new Label();
lDesc.SetBinding(Label.TextProperty, "Description", stringFormat: "DESCRIPTION: {0}");
var deleteAction = new MenuItem { Text = "Delete", IsDestructive = true }; // red background
deleteAction.SetBinding(MenuItem.CommandProperty, new Binding("BindingContext.TrashCommand", source: this));
deleteAction.SetBinding(MenuItem.CommandParameterProperty, ".");
slView.Children.Add(lDesc);
ViewCell vc = new ViewCell() {View = slView };
vc.ContextActions.Add(deleteAction);
return vc;
}
现在,当您长按该行时,将显示一个ContextAction“Delete”,并在ViewModel中执行一个垃圾处理命令(您应该使用MVVM…),传递一个“these”参数(选定的obj),以便您可以将其从列表中删除
this.TrashCommand = new Command(async (object obj) => {
try
{
if (_isTapped)
return;
if (obj != null)
System.Diagnostics.Debug.WriteLine("Obj is not null");
else
System.Diagnostics.Debug.WriteLine("Obj IS null");
_isTapped = true;
var ret = await Application.Current.MainPage.DisplayAlert("Attention", "Delete this row?", "Yes", "No");
if (ret)
{
// List is your "sample" list... Removing the obj, is it reflected to ListView if you use ObservableCollection instead of List
List.Remove((Model)obj);
Count = List.Count;
}
_isTapped = false;
}
catch (Exception ex) {
_isTapped = false;
await Application.Current.MainPage.DisplayAlert("Attention", ex.Message, "Ok");
}
});
}
什么是“sample”?它是一个元素数组。我使用Modal类创建了。比如(sample=newobservedcollection();)我不明白为什么要在deleteAction中创建另一个WishesPage。单击…WishesPage WishesPage=new WishesPage();WishesPage.delete(wishId);只有我卡住了..没有为WishesPage类创建对象,无法调用delete方法..还有其他方法调用该方法吗?在ViewCell类中移动delete方法谢谢..让我试试这些