2016-12-18 20 views
1

我只想在gridViewItem上的righttapped事件后弹出菜单。右击事件后的菜单弹出

我XAML代码:

<Page 
x:Class="HNT_listView2.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:HNT_listView2" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:data="using:HNT_listView2.Models" 
mc:Ignorable="d"> 

<Grid Background= "Salmon" Margin="0,0,10,0" > 

    <GridView ItemsSource="{x:Bind MyContactList}" 
       ItemClick="GridViewItem_Click" Name="NameOf_ItemClick" 
       IsItemClickEnabled="True" 
       RightTapped="GridViewItem_RightTapped" 
       IsRightTapEnabled="True"> 


     <GridView.ItemTemplate> 
      <DataTemplate x:DataType="data:Contact"> 
       <StackPanel > 
        <FlyoutBase.AttachedFlyout> 
         <MenuFlyout Placement="Top"> 
          <MenuFlyoutItem Text="Call"/> 
          <MenuFlyoutItem Text="Send a message"/> 
          <MenuFlyoutItem Text="Delete"/> 
         </MenuFlyout> 
        </FlyoutBase.AttachedFlyout> 

        <StackPanel HorizontalAlignment="Center" Orientation="Horizontal"> 


        <Image Width="100" Height="120" Source="{x:Bind Photo}" HorizontalAlignment="Center" Stretch="UniformToFill"/> 
        <StackPanel Orientation="Vertical"> 
         <TextBlock FontSize="30" Text="{x:Bind Name}" HorizontalAlignment="Center"/> 
         <TextBlock FontSize="30" Text="{x:Bind Phone}" HorizontalAlignment="Center"/> 
        </StackPanel> 
       </StackPanel> 
       </StackPanel> 
      </DataTemplate> 
     </GridView.ItemTemplate> 

    </GridView> 


</Grid> 

我升C代码:

 private void GridViewItem_RightTapped(object sender, RightTappedRoutedEventArgs e) 
    { 
     var s = (FrameworkElement)sender; 

     if (s != null) 
     { 
      FlyoutBase f = FlyoutBase.GetAttachedFlyout(s); 
      if (f != null) 
      { 
       f.ShowAt(s); 
      } 
      else {Debug.WriteLine("No f value");}     
     } 
     else { Debug.WriteLine("No s value"); } 
    } 

我Contact.cs(结合):

public class Contact 
{ 
    public string Name { get; set; } 
    public string Photo { get; set; } 
    public string Phone { get; set; } 
} 

public class ContactManager 
{ 
    public static List<Contact> GetContacts() 
    { 
     var contact1 = new List<Contact>(); 
     contact1.Add(new Contact { Name = "Nguyen Van A", Phone = "0168111222", Photo = "Assets/1.jpg" }); 
     contact1.Add(new Contact { Name = "Tran Van B", Phone = " 0168333444", Photo = "Assets/2.jpg" }); 
     contact1.Add(new Contact { Name = "Le Van C", Phone = "0166555666", Photo = "Assets/3.jpg" }); 

     return contact1; 
    } 
} 

然后,出放印有“无f值”

请帮忙,谢谢!

回答

1

发件人是GridViewItem的项目,FlyoutBase在GridViewItem中。

您的FlyoutBase f = FlyoutBase.GetAttachedFlyout(s);一直为空,因为GridViewItem的项目没有Flyout。

您可以使用定义代码flyoutbase。

MenuFlyout myFlyout = new MenuFlyout(); 
MenuFlyoutItem callItem = new MenuFlyoutItem { Text = "Call" }; 
MenuFlyoutItem sendItem = new MenuFlyoutItem { Text = "Send a message" }; 
MenuFlyoutItem deleteItem = new MenuFlyoutItem { Text = "Delete" }; 

myFlyout.Items.Add(callItem); 
myFlyout.Items.Add(sendItem); 
myFlyout.Items.Add(deleteItem); 

并在点击位置设置弹出。

myFlyout.ShowAt(sender as UIElement, e.GetPosition(sender as UIElement)); 
+0

它的工作原理。非常感谢你 –

+0

这是我项目的2个部分中的1个。 非常感谢您的有益建议。 如果你有时间,请检查我的第2部分(是的,它也有错误....) –

+0

链接?我不知道你说什么。 – lindexi