如何在Xamarin.forms xaml中将列表视图与聊天数据绑定

时间:2018-03-26 13:48:28

标签: xaml xamarin.forms

我无法绑定ListView:我成功绑定列表视图当我使用设计背后的代码但我想改变XAML中的设计但现在我无法绑定我的列表视图,数据成功发送到服务器但无法绑定listbiew。

XAML: <ListView Grid.Row="0" ItemsSource="{Binding TextContainer}" x:Name="ListView" 
                            SeparatorVisibility="None">
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <ViewCell>
                                            <StackLayout VerticalOptions="FillAndExpand" Orientation="Horizontal" Padding="10">
                                                <Label Text="{Binding Text}" YAlign="Center" Font="Large" TextColor="Red" />
                                             </StackLayout>
                                        </ViewCell>
                                        </DataTemplate>
                                </ListView.ItemTemplate>
                            </ListView>


            CS: 
             private ObservableCollection<MessageText> TextContainer = new ObservableCollection<MessageText>();
                    public ListView listView = new ListView();

                    public event OnMessageSent MessageSent;
                    public delegate void OnMessageSent(string message);
          public Chat()
                {
          MessageSent += (message) =>
                    {
                       var text= "Me: " + message;

                        TextContainer.Add(new MessageText { Text = text });


                        SignalRClient.SendMessage(UsernameTextbox.Text, message);
                    };

                    SignalRClient.OnMessageReceived += (username, message) => {

                        if (username == UsernameTextbox.Text)
                        {

                        }
                        else
                        {

                            var cc = username + ": " + message;
                            TextContainer.Add(new MessageText { Text = cc });
                        }
                    };


                }
                public void AddText(string text)
                {
                    TextContainer.Add(new MessageText { Text = text });
                }
                private void EnterButton_Clicked(object sender, EventArgs e)
                {
                    try
                    {
                        var messageSent = MessageSent;
                        if (messageSent != null)
                            messageSent(Messagebox.Text);

                        Messagebox.Text = string.Empty;
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex);
                    }
                }
            }

            public class MessageText
            {
                public string Text { get; set; }
            }
        }

我正在使用聊天应用程序。并且无法绑定本地消息

1 个答案:

答案 0 :(得分:0)

您的TextContainer字段应该是公开的和属性。

改变这个:

private ObservableCollection<MessageText> TextContainer = new ObservableCollection<MessageText>();

要:

public ObservableCollection<MessageText> TextContainer { get; set; } = new ObservableCollection<MessageText>();

请注意我将private更改为public并添加了{ get; set; }以表明这是一个包含自动getter和setter的属性。

有关数据绑定的更多信息,请参阅this的新Microsoft文档页面。