【问题标题】:How can I bind View to ViewCell in Xamarin如何在 Xamarin 中将 View 绑定到 ViewCell
【发布时间】:2021-11-26 16:14:02
【问题描述】:

所以我在做一个 MVVM 应用程序并且有一个问题:我需要在应用程序中动态创建一个界面。所以我用Collection创建了一个ViewModel。

public ObservableCollection<ViewCell> dataTemplate { get; set; }

并添加一个添加新元素的按钮

private void NewOther(object obj)
    {
        IsBusy = true;
        try
        {                
            ViewCell other = new ViewCell
            {
                View = new StackLayout
                {
                    Children = { new Label { Text = "I m New" } }
                }

            };
            
            dataTemplate.Add(other);
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
        finally
        {
            IsBusy = false;
        } 
    }

在 Xaml 中我有:

<RefreshView Padding="5" Command="{Binding LoadCheckListsCommand}" IsRefreshing="{Binding IsBusy, Mode=TwoWay}">            
        <ListView ItemsSource="{Binding dataTemplate}">
            <ListView.Header>
                <Grid ColumnDefinitions="*,*,*">
                    <Button Text="Add Measurment" Grid.Column="0" Command="{Binding AddMeasurment}"/>
                    <Button Text="Add Pattern" Grid.Column="1" Command="{Binding AddPattern}"/>
                    <Button Text="Add Other" Grid.Column="2" Command="{Binding AddOther}"/>
                </Grid>
            </ListView.Header>
            <ListView.Footer>
                <StackLayout Orientation="Horizontal" Margin="0,10,0,0">
                    <Button Text="Cancel" Command="{Binding CancelCommand}" HorizontalOptions="FillAndExpand"></Button>
                    <Button Text="Save" Command="{Binding SaveCommand}" HorizontalOptions="FillAndExpand"></Button>
                </StackLayout>
            </ListView.Footer>
            
            
        </ListView>
    </RefreshView>

所以,在我按下AddOther 之后,我有一个带有文本“Xamrin.Forms.ViewCell”的新元素,我认为这只是viewcell.ToString()

这里如何绑定view cell的内容?

【问题讨论】:

  • 是的,默认情况下 ListView 和 CollectionView 使用ToString()。您可能必须在 XAML 中声明 ListView.ItemTemplate

标签: c# xaml xamarin xamarin.forms xamarin.android


【解决方案1】:

要达到你的要求,你必须知道

  1. 我们经常使用Data作为source并绑定到control/layout,直接设置View作为source是不正确的。

  2. ListView 共享同一个模板,也就是说行应该是相同的,我们不能在运行时添加不同的模板。


作为一种解决方法,我们可以使用TableView

TableView 没有 ItemsSource 的概念,因此必须手动将项添加为子项。

示例代码

//xaml
<TableView>
     <TableSection x:Name="section">
          <ImageCell ImageSource="dog.png"/>
          <SwitchCell On="True"/>
      </TableSection>
</TableView>
//code behind
private void NewOther(object obj){

    ViewCell other = new ViewCell
    {
        View = new StackLayout
        {
            Children = { new Label { Text = "I m New" } }
         }

     };
     section.Add(other);
}

屏幕截图

参考

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/data-and-databinding.

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/tableview.

【讨论】:

  • 那么,我什至无法将 cellview 绑定到 Tableview?
  • 是的,不能绑定到任何布局,但你可以按照我提供的方式在代码中动态添加。
猜你喜欢
  • 2021-04-05
  • 2016-12-20
  • 2019-12-01
  • 1970-01-01
  • 2020-02-10
  • 2020-03-28
  • 2018-11-15
  • 2019-04-20
  • 2018-06-25
相关资源
最近更新 更多