我在Xamarin表单ListView
中遇到数据绑定问题这是页面代码:
using System;
using System.Collections.Generic;
using Punteam.GetTraderStatusApi;
using Punteam.ReceiveTraderStatusApi;
using Xamarin.Forms;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Linq;
using XLabs.Forms.Controls;
using System.Collections;
using System.Collections.ObjectModel;
using Punteam.RequestChatMessages;
using Punteam.ReceiveChatMessages;
using System.Diagnostics;
namespace Punteam
{
public partial class ChatForm : ContentPage
{
ObservableCollection <chatDataSource> source;
StatusAPI getStatus;
bool KeyboardStatus = false;
public ChatForm ()
{
InitializeComponent ();
if (Device.OS == TargetPlatform.iOS) {
txtMsg.HeightRequest = 34;
btnTxt.HeightRequest = 34;
btnPick.HeightRequest = 60;
}
double bottomOffset;
Int32 i = 0;
getStatus = new StatusAPI ();
getStatus.json.memberList [0].memberId = App.memberId;
getStatus.json.pType = Constants.pTypeTraderStatus;
SendRequest (getStatus, Constants.pTypeTraderStatus, this);
btnTxt.Clicked += (object sender, EventArgs e) => {
i++;
System.Diagnostics.Debug.WriteLine ("In button Clicked: " + i.ToString ());
if (Device.OS == TargetPlatform.Android) {
btnPick.IsVisible = true;
}
txtMsg.Text = "";
};
KeyboardHelper.KeyboardChanged += (sender, e) => {
if (KeyboardStatus) {
} else {
}
bottomOffset = mainStack.Bounds.Bottom - textStack.Bounds.Bottom; // This offset allows us to only raise the stack by the amount required to stay above the keyboard.
textStack.TranslationY -= e.Visible ? e.Height - bottomOffset : bottomOffset - e.Height; // The textStack is translated up, and then returned to original position.
};
}
public static async Task<string> SendRequest (Object obj, String pType, ChatForm chat)
{
// Get Trader status
String jsonSring = "";
Utils util = new Utils ();
jsonSring = util.deserialze (obj);
var result = await Http.SendData (Constants.serverAddress, jsonSring, true);
string inputStr = (string)result;
Punteam.ReceiveTraderStatusApi.RootObject traderStatus = new Punteam.ReceiveTraderStatusApi.RootObject ();
traderStatus = JsonConvert.DeserializeObject<Punteam.ReceiveTraderStatusApi.RootObject> (inputStr);
// Get Chat last messages
ChatdAPI chatApi = new ChatdAPI ();
chatApi.json.pType = Constants.puTypeGetChatMessages;
chatApi.json.memberList [0].memberId = App.memberId;
jsonSring = "";
jsonSring = util.deserialze (chatApi);
result = await Http.SendData (Constants.serverAddress, jsonSring, true);
Punteam.ReceiveChatMessages.ReceivedMemberMessages memberMessages = new Punteam.ReceiveChatMessages.ReceivedMemberMessages ();
memberMessages = JsonConvert.DeserializeObject<Punteam.ReceiveChatMessages.ReceivedMemberMessages> (result);
chat.fillChatList (memberMessages);
return result;
}
public void fillChatList (Punteam.ReceiveChatMessages.ReceivedMemberMessages memberMessages)
{
var chatLine = new chatDataSource ();
source = new ObservableCollection<chatDataSource> ();
Int32 chatIndex = memberMessages.MemberChatMessagesData.Count;
for (int i = 0; i < chatIndex; i++) {
chatLine = new chatDataSource ();
chatLine.senderName = memberMessages.MemberChatMessagesData [i].SenderName;
chatLine.text = memberMessages.MemberChatMessagesData [i].Message;
source.Add (chatLine);
}
chatList.ItemsSource = source;
chatList.ItemTemplate = new DataTemplate (() => {
var name = new Label ();
return new ViewCell{ View = name };
});
}
public void txtMsgFocused (Object s, EventArgs a)
{
if (Device.OS == TargetPlatform.Android) {
btnPick.IsVisible = false;
}
}
public void txtMsgCompleted (Object s, EventArgs a)
{
if (Device.OS == TargetPlatform.Android) {
btnPick.IsVisible = true;
}
}
*
public void sendClicked (Object s, EventArgs a)
{
if (Device.OS == TargetPlatform.Android) {
btnPick.IsVisible = true;
}
}
public void itemTapped (Object s, EventArgs a)
{
if (Device.OS == TargetPlatform.Android) {
btnPick.IsVisible = true;
}
}
}
public class chatDataSource
{
public string senderName = "";
public string text = "";
}
}
答案 0 :(得分:1)
发现问题: 数据源类看起来像这样
public class chatDataSource
{
public string senderName = "";
public string text = "";
}
它应该有它的字段是这样的属性:
public class chatDataSource
{
public string senderName { get; set; }
public string text { get; set; }
}
答案 1 :(得分:0)
在ListView中,您必须将视图绑定到模型类属性。
在您的情况下,模型类“chatDataSource”具有senderName和text。
因此,xaml中的绑定应如下所示:
<Label x:Name="labelname" Text="{Binding senderName}" />
或Text =“{Binding text}”
请确保Binding属性中的名称和Model类中属性的名称完全相同。