Xamarin中的事件和委托构成父页面和子页面 [英] Events and Delegates in Xamarin forms parent and child pages

查看:94
本文介绍了Xamarin中的事件和委托构成父页面和子页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助.我是事件和处理程序的新手,我想开始将事件用于去耦目的,但我感到困惑.

I need some help. I am new to Events and handlers and I would like to begin using events for decoupling purposes but I am confused.

我有一个类,其中有一个列表视图,该列表视图填充在OnAppearing上.但是,为了防止每次单击页面时都会出现onAppearing,我加载了一次列表,然后希望通过使用事件从服务器添加或删除项目后,将项目添加到列表中或从列表中删除.

I have a class where I have a list view that is populated on OnAppearing. However to prevent onAppearing to happen each time the page is clicked I load the list once and then I would like to have items to get added or deleted to the list upon being added or removed from the server through the use of events.

ListView页面是我最喜欢的报纸文章链接的列表.当单击这些链接中的任何一个时,我将重定向到LinksDetailsPage,在其中输入所选链接,然后显示与该链接关联的所有详细信息.

The ListView page is a list of my favorite newspaper article Links. When clicking on any one of these links I get redirected to a LinksDetailsPage where I pass in the selected link and then display any details associated with the link.

无论如何...

我想无缝地添加或删除我的收藏夹"列表中的项目.因此,当我在LinksDetails页面中单击AddItem或RemoveItem时,我希望该项目被删除或添加到收藏夹页面"列表中.在我仅依靠OnAppearing发挥其魔力并更新收藏夹"列表之前,但是从列表中删除该项目会比较滞后,因此我希望这种方法将在偶数被调用时立即删除或添加,而不是在页面在OnAppearing上加载.但是,我认为我不是正确地调用了事件,或者我的订阅者未正确订阅.活动和代表从一开始就让我感到困惑.在我的收藏夹列表最上面进行了分组,这也是我第一次使用分组列表.查看我的代码:

I would like to add or remove an item on the my Favorites list seamlessly. So when I click on the AddItem or RemoveItem in the LinksDetailsPage I would like the item to either remove or add to theFavoritesPage List. Before I was only relying on the OnAppearing to work its magic and update the favorites list but it would lag to remove the item from the list, so this way I hope it would remove or add as soon as the even is invoked and not when the page loads on OnAppearing. However I think I am either not invoking the event properly or my subscriber is not subscribed properly. Events and delegates have been confusing for me from the get go. On top of that my favorites list is grouped so and it's my first time working with grouped lists as well. Check out my code:

订户我的收藏夹页面:

 public FavoritesPage()
        {
            InitializeComponent();
           
        }
        protected override async void OnAppearing()
        {

            if (_isDataLoaded)
                return;

            _isDataLoaded = true;
            base.OnAppearing();
            await LoadFavorites();

        }

        private async Task LoadFavorites()
        {
            groups = new ObservableCollection<LinksTypeGroup<string, NewspaperLink>>();
           
            var links = await _db.GetAllFavorites();
            
                var linkType = await _manager.GetLinkCategories();
                foreach(var type in linkType)
                {
                   
                    var typegroup = links.FindAll(
                    delegate(NewspaperLink link)
                    {   
                        return link.iLinkTypeID == type.iLinkTypeID;
                    });
              
                    groups.Add(new LinksTypeGroup<string, NewspaperLink>(type.Title, typegroup));
              
                
                MyList.GroupDisplayBinding = new Binding("GroupKey");
                MyList.ItemsSource = groups;
               

            }
        }
 public void Listener(FavoritesPage P)
        {
            P.LinkAdded += Item_Added;
            P.LinkDeleted += Item_Deleted;
            
        }
void Item_Deleted(object sender, int e)
        {
            Console.WriteLine("Item_Deleted");
// remove item from groups ..see code above

        }

 void Item_Added(object sender, int e)
        {
            Console.WriteLine("Item_Added");
// add to groups ..see code above

        }

到目前为止,我还没有访问任何内容.

I am not accessing anything so far.

发布者LinksDetailsPage:

Publisher LinksDetailsPage:

    private NewspaperLink _link;
    public event EventHandler< NewspaperLink> ItemAdded;
    public event EventHandler< NewspaperLink> ItemDeleted;

  public LinksDetailsPage(NewspaperLink link)
        {
           
            _link = link; 
            BindingContext = _link;
            InitializeComponent();
        }

        protected override async void OnAppearing()
        {
            base.OnAppearing();
            

            await LoadLink();
        }

        private async Task LoadLink()
        {
            var url = await db.ReturnRecipeLink(_link.iLinkID);
            
                    linkWebView.Source = url;
                
                  CheckifExists(_link);
            }
        }

  void AddLink(object sender, System.EventArgs e)
        {
            var link = BindingContext as NewspaperLink;
            db.InsertIntoMyList(_link);
            ItemAdded?.Invoke(this, link);
  
            
        }

   void  DeleteLink(object sender, System.EventArgs e)
        {

            var link = BindingContext as NewspaperLink;
             db.DeleteFromMyList(_link);
             ItemDeleted?.Invoke(this, link);
    
        }

有人可以指导我如何使此均匀过程正常工作吗?

Can someone guide me on how to make this even process work?

推荐答案

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆