Xamarin的.窗体聊天应用程序消息左右位置



我做了一个应用程序在FireBase测试的东西,实际上我创建了一个聊天应用程序。但我想显示消息像whatsapp当我发送消息看右边。如果我看到左边的信息。其实我不需要这么复杂。所以我有聊天的用户。此用户可以登录并加入聊天。

也就是我的聊天页面XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="4*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<ListView Grid.Row="0" ItemsSource="{Binding .}" x:Name="_lstChat"
SeparatorVisibility="None">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding UserName}" Detail="{Binding UserMessage}" DetailColor="Fuchsia" />                 
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Grid Grid.Row="1" RowSpacing="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

<Entry Placeholder="Write Message .." Grid.Column="0" x:Name="_etMessage"/>
<Button Text="Send!" Grid.Column="1" Clicked="Button_Clicked"/>
</Grid>
</Grid>

我不需要使用TextCell,我只是想需要左右位置。

public partial class ChattingPage : ContentPage
{
Scripts.FireBase db = new Scripts.FireBase();
Model.Room rm = new Model.Room();
public ChattingPage()
{
InitializeComponent();
if (Preferences.Get("AdminCheck", string.Empty)== "1")
{
MessagingCenter.Subscribe<ChatPage, Model.Room>(this, "RoomProp", async (page, data) =>
{
rm = data;
_lstChat.BindingContext = await db.subChat(data.Name);

MessagingCenter.Unsubscribe<ChatPage, Model.Room>(this, "RoomProp");
});
}else
{
SubList();
}
}
void Button_Clicked(System.Object sender, System.EventArgs e)
{
var chatOBJ = new Model.Chat { UserMessage = _etMessage.Text, UserName = Preferences.Get("RandomText", string.Empty) };
if(Preferences.Get("AdminCheck", string.Empty) == "1")
{
db.saveMessage(chatOBJ, rm.Name);
}
else
{
db.saveMessage(chatOBJ, Preferences.Get("RandomText", string.Empty));
}
_etMessage.Text = "";
}
async void SubList()
{
_lstChat.BindingContext = await db.subChat(Preferences.Get("RandomText", string.Empty));
}
}

为消息添加一个属性,例如bool类型,它将确定消息是传入的还是传出的。然后创建一个转换器,该转换器将根据该值返回水平LayoutOption对齐方式,输入消息为左(Start),输出消息为右(End)。通过带有消息类型值的转换器将此水平对齐方式设置为显示消息文本的气泡。

最新更新