如何实现在Xamarin Forms TableView Cell中单击的行?

时间:2016-09-01 12:53:05

标签: xamarin xamarin.forms

我在XAML中为Xamarin Forms设置了以下tableview:

<TableView Intent="Menu">
   <TableRoot>
      <TableSection Title="Categories">
         <ViewCell>
            <StackLayout Orientation="Horizontal" VerticalOptions="Center" Padding="20,0,20,0">
               <Label Text="Category" XAlign="Center"/>
               <Label Text=">" HorizontalOptions="EndAndExpand" XAlign="Center"/>
            </StackLayout>
        </ViewCell>
      </TableSection>
   </TableRoot> 
</TableView>

任何人都可以让我知道如何实现点击行打开另一个内容页面xamarin表单?或者我是否必须在iOS方面单独实施?

2 个答案:

答案 0 :(得分:2)

查看有关TapGestureRecognizer

的Xamarin文档

您也可以在此处应用该方法。 我不完全确定您是否可以直接将其应用于ViewCell,但您应该可以在StackLayout上执行此操作。

所以它会是这样的:

<TableView Intent="Menu">
   <TableRoot>
      <TableSection Title="Categories">
         <ViewCell>
            <StackLayout Orientation="Horizontal" VerticalOptions="Center" Padding="20,0,20,0">
               <StackLayout.GestureRecognizers>
                   <TapGestureRecognizer
                       Tapped="OnTapGestureRecognizerTapped"
                       NumberOfTapsRequired="1" />
               </StackLayout.GestureRecognizers>

               <Label Text="Category" XAlign="Center"/>
               <Label Text=">" HorizontalOptions="EndAndExpand" XAlign="Center"/>
            </StackLayout>
        </ViewCell>
      </TableSection>
   </TableRoot> 
</TableView>

然后当然实现事件方法。

答案 1 :(得分:0)

您还可以将Tapped方法添加到ViewCell本身。

像这样:

const str1 = 'http://this/is/{placeholder1}/{placeholder2}/my/url?limit=10&offset=5';
const str2 = 'http://this/is/100Banana/200Apple/my/url?limit=10&offset=5';

// url1: url with placeholders
// url2: url with filled placeholders
const compareUrls = (url1, url2) => {
  const u1 = url1.split('/');
  const u2 = url2.split('/');

  if (u1.length !== u2.length) { return false; }

  for (let i = 0; i < u1.length; i++) {
    if (u1[i].startsWith('{placeholder')) { continue; }
    if (u1[i] !== u2[i]) { return false; }
  }
  return true;
};

console.log(compareUrls(str1, str2));

然后在后面的代码中编写Handle_Cell_Tapped事件处理程序。