微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Xamarin.Forms 中,以纯文本形式显示来自视图模型的列表的简单方法是什么?

如何解决在 Xamarin.Forms 中,以纯文本形式显示来自视图模型的列表的简单方法是什么?

在 Xamarin.Forms 应用程序中。我想让一个视图以纯文本形式显示来自 viewmodel 的对象列表。我见过使用这样的列表视图的例子。

<ListView x:Name="ItemView"
          ItemsSource="{Binding Items}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding Text}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

方法似乎有效,但它不允许我在 TextCell 中使用多个对象属性。 Visual Studio 2019 生成的示例应用程序使用 CollectionView 来做一些更复杂的事情。

<CollectionView x:Name="ItemsListView"
                ItemsSource="{Binding Items}"
                SelectionMode="None">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <StackLayout Padding="10" x:DataType="model:Item">
                <Label Text="{Binding Name}" 
                       LineBreakMode="Nowrap" 
                       Style="{DynamicResource ListItemTextStyle}" 
                       FontSize="16" />
                <Label Text="{Binding Description}" 
                       LineBreakMode="Nowrap"
                       Style="{DynamicResource ListItemDetailTextStyle}"
                       FontSize="13" />
                <StackLayout.GestureRecognizers>
                    <TapGestureRecognizer 
                        NumberOfTapsrequired="1"
                        Command="{Binding Source={RelativeSource AncestorType={x:Type local:Itemsviewmodel}},Path=ItemTapped}"     
                        CommandParameter="{Binding .}">
                    </TapGestureRecognizer>
                </StackLayout.GestureRecognizers>
            </StackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

不过,此方法似乎不适用于我当前的 viewmodel。仅以现有代码为例,我做了如下所示的内容

Student.cs

public class Student
{
    public string Id { get; set; }
    public string Name { get; set; }
    public ICollection<Course> Courses{ get; set; }
}

Course.cs

public class Course
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string CourseNumber { get; set; }
}

StudentDetailviewmodel.cs

[QueryProperty(nameof(StudentId),nameof(StudentId))]
public class StudentDetailviewmodel : Baseviewmodel
{
    private string studentId;
    private string name;
    private ICollection<Courses> courses;

    public string Id { get; set; }

    public string Name
    {
       get => name;
       set => SetProperty(ref name,value);
    }

    public ICollection<Course> Courses
    {
            get => Courses;
            set => SetProperty(ref courses,value);
    }

    public string StudentId
    {
        get
        {
            return studentId;
        }
        set
        {
            studentId = value;
            LoadStudentId(value);
        }
    }

    public async void LoadStudentId(string sId)
    {
        try
        {
            var student = await DataStore.GetItemAsync(sId);
            Id = student.Id;
            Name = student.Name;
            Courses = student.Courses;
        }
        catch (Exception)
        {
            Debug.WriteLine("Failed to Load Item");
        }
    }
}

我希望结果页面显示类似

Student Name
Courses:
   * CourseNumber - CourseName
   * CourseNumber - CourseName
   * CourseNumber - CourseName

但是,当我尝试使用 ListView 方法时,我无法弄清楚如何格式化 TextCell 的 Text="{Binding PropName}" 属性,以便我可以在此“{Prop1} - {Prop2}”中使用两个属性“ 格式。使用 CollectionView,我完全不知道如何正确绑定数据以显示它。显示此类内容的最简单方法是什么?设置 viewmodel 以执行此操作的最佳做​​法是什么?

解决方法

有很多方法可以解决这个问题。最简单的可能是使用包含 ViewCellLabelSpans 而不是 TextCell。你也可以用 CollectionView 做类似的事情,它不需要使用 Cells

<ListView ItemsSource="{Binding Courses}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
              <Label>
                <Label.FormattedText>
                  <FormattedString>
                     <Span Text="{Binding Name}" />
                     <Span Text=" - " />
                     <Span Text="{Binding CourseNumber}" />
                  </FormattedString>
                <Label.FormattedText>
              </Label>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

学生信息可以在课程之前放置在单独的布局中,或者添加到课程的ListView.Header

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。