I use the databinding with the ListView.
When the app runs, there is 3 items in ListView.
I invoke the function 'Entry_Completed' to add the 4th item to the ListView.
I trace the function run over, but there is no 4th item add to the ListView on UI.
MainPage.xaml
<ListView x:Name="listView" ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="0" Spacing="0" Orientation="Vertical">
<StackLayout Padding="0" Spacing="10" Orientation="Horizontal">
<Label Text="{Binding MessageNumber}" FontSize="Micro" TextColor="Silver" />
<Label Text="{Binding Nickname}" FontSize="Micro" TextColor="Silver" />
<Label Text="{Binding Ip}" FontSize="Micro" TextColor="Silver" />
</StackLayout>
<Label Text="{Binding Message}" FontSize="Medium" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
MainPage.xaml.cs
public partial class MainPage : ContentPage
{
public IList<ChatMessage> chatMessages;
public MainPage()
{
InitializeComponent();
chatMessages = new List<ChatMessage>();
for (int i = 0; i < 3; i++)
{
var msg = new ChatMessage();
msg.Nickname = "我的六字昵称";
msg.Ip = "127.0.0.1";
msg.Message = "这是一条聊天信息";
msg.MessageNumber = i + 1;
chatMessages.Add(msg);
}
this.BindingContext = chatMessages;
}
private void Entry_Completed(object sender, EventArgs e)
{
var msg = new ChatMessage();
msg.Nickname = "我的六字昵称";
msg.Ip = "127.0.0.1";
msg.Message = sendText.Text;
msg.MessageNumber = 100;
chatMessages.Add(msg);
}
}