Bla*_*ons 2 c# xamarin xamarin.forms
我有一个包含几个页面的小项目,这些页面都引用了静态类中的真实来源列表。此静态列表使用 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)
订阅发生在我的 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
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
我曾尝试将 MyListDataService.myList 设为公开并引用 MyListDataService.myList 对象作为接收发件人的规范,但是当我这样做时,我收到一个错误:“类型名称 'myList' 在类型 'MyListDataService' 中不存在。并且即使它确实有效,我也不知道我是否应该公开该列表。这样做的正确方法是什么?在这种情况下我应该指定什么作为发件人?这甚至可以工作吗?
更新:我已经设法通过替换来编译代码
MessagingCenter.Send(myList, Constants._listUpdateContract, myList);
Run Code Online (Sandbox Code Playgroud)
和
MessagingCenter.Send(Application.Current, Constants._listUpdateContract, myList);
Run Code Online (Sandbox Code Playgroud)
并更换
MessagingCenter.Subscribe<MyListDataService.myList, ObservableCollection<ListItem>>(this, Constants._listUpdateContract, (s, a) =>
{
//populate list code
});
Run Code Online (Sandbox Code Playgroud)
和
MessagingCenter.Subscribe<App, ObservableCollection<ListItem>>(Application.Current, Constants._listUpdateContract, (s, a) =>
{
//populate list code
System.Diagnostics.Debug.WriteLine(a.ToString());
});
Run Code Online (Sandbox Code Playgroud)
但是发送消息时不会触发调试消息。对这一新发展有什么见解吗?
Send并且Subscribe需要使用相同的类型参数
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());
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
804 次 |
| 最近记录: |