【问题标题】:Alternating row alignment in Xamarin's ListViewXamarin 的 ListView 中的交替行对齐
【发布时间】:2018-06-25 15:51:48
【问题描述】:

我正在为现有的移动应用程序开发一个信使模块 - 将其视为基本的 Whatsapp - 之前没有使用 Xamarin 的经验我在交替显示带有文本的行(消息本身)时遇到问题左对齐或右对齐,这反映了大多数信使中的行为,其中这种对齐取决于用户是消息的发送者还是接收者。我尝试结合几个解决方案来解决我在网上发现的类似问题,但最终我走到了死胡同。

这是我最近的失败。与此问题无关的代码已被省略。请记住,ChatItem 指的是我范围内的每个单独的聊天页面:

ChatItemView.xaml

<ContentPage.Content>
    <ListView VerticalOptions="FillAndExpand" ItemsSource="{Binding chatItem.Messages}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell Appearing="Cell_OnAppearing">
                    <StackLayout Orientation="Horizontal">
                        <Label Text="{Binding Body}" FontAttributes="Bold" HorizontalOptions="{Binding CellAlignment}" VerticalOptions="Center"/>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage.Content>

HorizontalOptions="{Binding CellAlignment} 是这里的关键,因为它似乎是做这类事情的首选方法。但它在ListView 中无法按预期工作。或者至少,不是以我接近它的方式。另请注意,我正在为 ViewCell Appearing 事件使用事件处理程序。

ChatItemViewModel.cs

public class ChatItemViewModel : BaseViewModel
{
    public ChatItem chatItem { get; set; }
    public LayoutOptions CellAlignment { get; set; }

    public ChatItemViewModel(ChatItem chatItem)
    {
        Title = chatItem.Name;
        this.chatItem = chatItem;
    }
}

ChatItemView.xaml.cs

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ChatItemView : ContentPage
{
    private ChatItemViewModel model;
    private ChatItem chatItem;
    private int rows;

    public ChatItemView(ChatItem chatItem)
    {
        InitializeComponent();
        this.chatItem = chatItem;
        model = new ChatItemViewModel(chatItem);
        BindingContext = model;
        rows = 0;
    }

    public void Cell_OnAppearing(object sender, EventArgs e)
    {
        if (chatItem.Self == chatItem.Messages[rows].Sender)
            model.CellAlignment = LayoutOptions.FillAndExpand;
        else
            model.CellAlignment = LayoutOptions.EndAndExpand;
        rows++;
    }
}

我什至在我的Cell_OnAppearing 方法中删除了条件,它的实现只包含model.CellAlignment = LayoutOptions.EndAndExpand;,但这没关系,因为我的文本永远向左对齐。我的脑汁用完了,所以今晚回家时,我只能希望 StackOverflow 大神能帮助我。

【问题讨论】:

    标签: c# xaml listview xamarin layout


    【解决方案1】:

    首先想到的是实现IValueConverter

    <ContentPage.Resources>
    <ResourceDictionary>
      <local:MyConverter x:Key="MyConverter"/>
    </ResourceDictionary>
    </ContentPage.Resources>
    ...
    <Label Text="{Binding Body}" HorizontalOptions="{Binding IsSender, Converter={StaticResource MyConverter}}"/>
    

    但是DataTrigger 可能会这样做;未显示:IsSender 属性的实现:

    <Label Text="{Binding Body}" HorizontalOptions="EndAndExpand">
      <Label.Triggers>
        <DataTrigger TargetType="Label" Binding="{Binding IsSender}" Value="True">
          <Setter Property="HorizontalOptions" Value="FillAndExpand" />
        </DataTrigger>
      </Label.Triggers>
    </Label>
    

    【讨论】:

    • 好的,我会检查你的建议,让你知道我想出了什么
    • 我不得不移动一些东西,但总而言之,DataTrigger 就像一个魅力。非常感谢 Benl!
    猜你喜欢
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    相关资源
    最近更新 更多