小编Bla*_*ons的帖子

如果发件人是静态类或静态类中的对象,如何在 MessagingCenter.Send 中指定发件人?

我有一个包含几个页面的小项目,这些页面都引用了静态类中的真实来源列表。此静态列表使用 HTTPRequest 定期更新。每当发生更新时,我都试图使用 Messaging Center 来更新每个页面 ViewModel 中的 ObservableCollection,但我无法弄清楚如何进行这项工作。

在静态类中,我尝试设置 MessagingCenter.Send() 操作,并将真实来源列表作为发件人,因为我可以将类本身设置为发件人(关键字“this”在静态中无效财产...)。我还将真实来源列表作为参数发送。我不确定这是否有效,但到目前为止我没有收到任何错误,并且还无法通过运行它来测试其功能。这是我的静态类中的相关代码:

using MyApp.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Xamarin.Forms;

namespace MyApp.Services
{
    public static class MyListDataService
    {
        //The List
        private static ObservableCollection<ListItem> myList = new ObservableCollection<ListItem>();

        //Static Method for getting the list from another place
        public static ObservableCollection<ListItem> GetItems()
        {
            return myList;
        }

        //Method that updates the List
        public static async System.Threading.Tasks.Task<bool> RequestUpdatedListAsync()
        {

            // HTTPRequest code that updates local List...

            MessagingCenter.Send(myList, Constants._listUpdateContract, myList);

            return success;
        } …
Run Code Online (Sandbox Code Playgroud)

c# xamarin xamarin.forms

2
推荐指数
1
解决办法
804
查看次数

标签 统计

c# ×1

xamarin ×1

xamarin.forms ×1