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

ct3nt3jp  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(146)

我在FireBase中做了一个应用程序来测试一些东西,实际上我创建了一个聊天应用程序。但是我想在我发送消息时看右边显示消息。如果我收到消息看左边。实际上我不需要复杂化。所以我有聊天用户。这个用户可以登录并加入聊天。
所以基本上我的聊天页面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));
    }
}
monwx1rj

monwx1rj1#

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

相关问题