2017-09-01 152 views
0

使用Xamarin.Forms,我怎样才能实现这样的布局?如何将这些自定义单元格添加到TableView?

enter image description here

我说的灰色背景的文本提示:

开启个人热点分享...

所有我可以添加到ListViewTableView是标准的ViewCells,它们都用分隔符和白色背景呈现。

如果这种布局是不可能通过使用TableViewListView,我怎么能呈现ViewCell(如Wi-Fi密码一个样品图像中)在标准StackLayout

回答

1

尽管TableView没有开箱即用的ViewCell可以做到这一点,但您肯定可以推出自己的实现。显然有一个TextCell,但它不会让你指定字体大小和其他东西。

你可以做这样的事情:

<?xml version="1.0" encoding="UTF-8"?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="DemoTableView.TablePage" Title="TableView"> 
    <ContentPage.Content> 
     <TableView Intent="Settings"> 
      <TableRoot> 
       <TableSection> 
        <SwitchCell Text="Personal Hotspot" /> 
        <ViewCell> <!-- Simple text --> 
         <Label Text="Turn on personal hotspot..." FontSize="Small" ForegroundColor="Gray" /> 
        </ViewCell> 
        <ViewCell> <!-- More complex cell --> 
         <StackLayout Orientation="Horizontal> 
          <Image Source="someicon" /> 
          <StackLayout> 
           <Label Text="TO CONNECT USING WIFI" FontSize="Large"/> 
           <Label Text="Something more" FontSize="Small" /> 
          </StackLayout> 
         </StackLayout> 
        </ViewCell> 
       </TableSection> 
      </TableRoot> 
     </TableView> 
    </ContentPage.Content> 
</ContentPage> 

这仅仅是一个简单的例子写的匆忙,但你的想法。您也可以通过从ViewCell类继承来以编程方式定义自己的单元格,并在TableView中显示它们。

Here's more关于官方文档定制单元。


编辑:按您的意见,我们需要挖比特到特定于平台的代码,以禁用文本只有细胞的选择。您应该创建一个自定义ViewCell实现与iOS的下列自定义渲染:

[assembly: ExportCell(typeof(YourCell), typeof(YourCellRenderer))] 
namespace YourProject.iOS 
{ 
    public class YourCellRenderer : ViewCellRenderer 
    { 
     public override UITableViewCell GetCell (Cell item, UITableView tv) 
     { 
      var cell = base.GetCell (item, tv); 
      cell.SelectionStyle = UITableViewCellSelectionStyle.None; 
      return cell; 
     } 
    } 
} 
+0

谢谢,但使用ViewCell有两个缺点:1)我仍然得到轻敲细胞,b)从该分离器的悬停效果上面的SwitchCell会略微缩小,因为如果下面有更多选项,通常情况下会这样。有关如何克服这些问题的任何想法? – Physikbuddha

+0

@Physikbuddha哦,没错,没有想到这一点。我更新了我的答案。虽然不太确定b。 – hankide

相关问题