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

标签 c# xamarin xamarin.forms

我有一个小项目,其中包含一些页面,这些页面都引用静态类中的真实来源列表。此静态列表使用 HTTPRequests 定期更新。每当发生更新时,我都尝试使用 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;
        }
    }
}

订阅在我的 ListPage 的 ViewModel 中发生,如下所示:

using MyApp.Models;
using MyApp.Services;
using MyApp.Views;
using System.Collections.ObjectModel;
using Xamarin.Forms;
using MvvmHelpers;

namespace MyApp.ViewModels
{
    public class ListPageViewModel
    {
        public ObservableRangeCollection<ListItem> MyList { get; private set; } = new ObservableRangeCollection<ListItem>(); 
        INavigation Navigation;

        public ListPageViewModel(INavigation MyListNavigation)
        {
            Navigation = MyListNavigation;

            MessagingCenter.Subscribe<MyListDataService.myList, ObservableCollection<ListItem>>(this, Constants._listUpdateContract, (s, a) =>
            {
                //populate list code
            });

        }
    }
}

我尝试公开 MyListDataService.myList 并将 MyListDataService.myList 对象引用为接收发件人的规范,但是当我这样做时我收到错误消息:“类型名称‘myList’在‘MyListDataService 类型中不存在’ '。即使它确实有效,我也不知道我是否应该公开该列表。这样做的正确方法是什么?在这种情况下我应该指定什么作为发件人?这甚至可以工作吗?

更新:我已经设法通过替换来编译代码

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

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

和替换

MessagingCenter.Subscribe<MyListDataService.myList, ObservableCollection<ListItem>>(this, Constants._listUpdateContract, (s, a) =>
            {
                //populate list code
            });

            MessagingCenter.Subscribe<App, ObservableCollection<ListItem>>(Application.Current, Constants._listUpdateContract, (s, a) =>
            {
                //populate list code
                System.Diagnostics.Debug.WriteLine(a.ToString());
            });

但是在发送消息时没有触发调试消息。对这一新发展有任何见解吗?

最佳答案

SendSubscribe需要使用相同类型的参数

MessagingCenter.Send<object,ObservableCollection<ListItem>>(Application.Current, Constants._listUpdateContract, myList);

MessagingCenter.Subscribe<object, ObservableCollection<ListItem>>(Application.Current, Constants._listUpdateContract, (s, a) =>
        {
            //populate list code
            System.Diagnostics.Debug.WriteLine(a.ToString());
        });

关于c# - 如果发件人是静态类或静态类中的对象,如何在 MessagingCenter.Send 中指定发件人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59776094/

相关文章:

c# - OwinContext 中的批量用户创建(性能)

c# - Clean XML 序列化分层递归数据结构

Xamarin Forms - 面向 .NET Standard

c# - 版本控制序列化文件

c# - 多目标 .NET Framework 4 和 Visual Studio 2012

c# - 如何在自定义选择器中设置和获取选定值?该值不可更改

c# - 添加 Xamarin 插件时出错

c# - FindViewById 返回 null(只有一个,在 OnCreate 内部和 SetContentView 之后)

xamarin.forms - 如何在 Xamarin Shell MenuItem 中添加开关

ssl - Xamarin - Android - 手动验证服务器的 SSL 证书 ModernHttpClient