我正在通过xamarin.Forms中的pubnub尝试聊天应用程序,我可以通过保留断点登录到我理解的频道。现在我正在尝试获取登录到我的频道的人员列表。< / p>
<ContentPage.Content>
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Label Text="Welcome" TextColor="Blue" HorizontalOptions="Center" VerticalOptions="Center">
<Label.FontSize>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="20" Android="20" WinPhone="20" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="30" Android="30" WinPhone="30" />
</OnIdiom.Tablet>
</OnIdiom>
</Label.FontSize>
</Label>
<ListView x:Name="LoginUserList" ItemsSource="{Binding LoginUserList}"ItemSelected="LoginUserList_OnItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding UserName}" TextColor="SkyBlue" FontSize="20" HorizontalOptions="Center" VerticalOptions="Center"></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
这里如何显示登录用户列表。
public class ChatListViewModel : INotifyPropertyChanged
{
private List<UserDto> _loginUserList;
private string _offlineImageSource;
private bool _offlineVisibility;
private string _userName;
public ChatListViewModel()
{
InitializeData();
GetAllUsersFromServer();
}
public event PropertyChangedEventHandler PropertyChanged;
public List<UserDto> LoginUserList
{
get { return _loginUserList; }
set
{
_loginUserList = value;
OnPropertyChanged();
}
}
public string OfflineImageSource
{
get { return _offlineImageSource; }
set
{
_offlineImageSource = value;
OnPropertyChanged();
}
}
public string UserName
{
get { return _userName; }
set
{
_userName = value;
OnPropertyChanged();
}
}
public bool OfflineVisibility
{
get { return _offlineVisibility; }
set
{
_offlineVisibility = value;
OnPropertyChanged();
}
}
private async void GetAllUsersFromServer()
{
LoginUserList = await UserService.Instance.GetAllUsers();
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void InitializeData()
{
OfflineImageSource = "BlackCircle.png";
}
}
这是用于列表显示目的的ViewModel。
答案 0 :(得分:0)
您可以使用on&#34; connect&#34;来获取PubNub频道上的活跃订阅者/用户列表。打回来。您将提供on&#34; connect&#34;当新用户或现有用户连接时要调用的函数。发生这种情况时,您会将此信息转发到最终需要的位置。
我将向您展示一些示例代码:
PUBNUB.subscribe({
channel : "hello_world", // CONNECT TO THIS CHANNEL.
callback : function(message){} // RECEIVED A MESSAGE.
connect : function() { // CONNECTION ESTABLISHED.
// A new user or existing user has
// connected. Send details to your server.
// ---------------------------------------
// This is a psudo-code example function:
// ---------------------------------------
psudo_post_url( "http://www.mywebsite.com/pubnub-user-connected", {
"user_id" : 123456789,
"channel" : "hello_word"
} );
}
})