我有一个绑定的ListView,它使用Grid作为ItemTemplate。网格有一个MenuFlyout,由Hold事件触发。在模拟器中,一切都很好,但在设备(带有Windows 8.1更新的Lumia 635)上运行时,Grid Holding事件不会触发。
如果我向ListView添加一个hold事件,则会触发此事件,但是我没有引用该项目。
要复制,1)将其添加到默认页面的主页标签
中<Page.Resources>
<Style TargetType="TextBlock" x:Key="GridText">
<Setter Property="FontSize" Value="20" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</Page.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ScrollViewer HorizontalAlignment="Stretch" Grid.Row="0" Margin="10" x:Name="scrollRecord" Background="DarkMagenta">
<TextBlock x:Name="textRecord" FontSize="18" />
</ScrollViewer>
<ScrollViewer HorizontalAlignment="Stretch" Grid.Row="1" Margin="10">
<ListView x:Name="listView" HorizontalAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch" Holding="Grid_Holding">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<FlyoutBase.AttachedFlyout>
<MenuFlyout>
<MenuFlyoutItem x:Name="buttonTick"
Text="Add"
Click="buttonAdd_Click"/>
<MenuFlyoutItem x:Name="buttonUntick"
Text="Remove"
Click="buttonRemove_Click"/>
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
<TextBlock Style="{StaticResource GridText}" Grid.Column="0" Text="{Binding Colour}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ScrollViewer>
</Grid>
创建一个名为Route的类,并将其粘贴到
中[DataContract] class Route:INotifyPropertyChanged { 公共事件PropertyChangedEventHandler PropertyChanged;
public Route()
{
}
public Route(string colour)
{
Colour = colour;
}
[DataMember]
public string Colour { get; set; }
protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, e);
}
protected void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
}
类RouteList:ObservableCollection // ElementCollection {
}
在MainPage.xaml.cs中,选择类定义下面的所有内容,然后打开大括号并将其粘贴到
中public MainPage() { this.InitializeComponent(); this.NavigationCacheMode = NavigationCacheMode.Required; this.Loaded + = MainPage_Loaded; }
RouteList routes;
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
routes = new RouteList();
routes.Add(new Route("Red"));
routes.Add(new Route("Orange"));
routes.Add(new Route("Pink"));
routes.Add(new Route("Blue"));
routes.Add(new Route("Violet"));
listView.ItemsSource = routes;
//DataTemplate t = listView.ItemTemplate;
//AddText("gridLayout holding)
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// TODO: Prepare page for display here.
// TODO: If your application contains multiple pages, ensure that you are
// handling the hardware Back button by registering for the
// Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
// If you are using the NavigationHelper provided by some templates,
// this event is handled for you.
}
private void Grid_Holding(object sender, HoldingRoutedEventArgs e)
{
try
{
AddText("Grid_Holding fired " + e.HoldingState.ToString());
//ShowFlyout(sender);
}
catch (Exception ex)
{
AddText(ex.Message);
}
}
private void ShowFlyout(object sender)
{
FrameworkElement senderElement = sender as FrameworkElement;
FlyoutBase flyoutBase = FlyoutBase.GetAttachedFlyout(senderElement);
flyoutBase.ShowAt(senderElement);
}
private void AddText(string s)
{
textRecord.Text += s + "\n";
//scrollRecord.ScrollToVerticalOffset(scrollRecord.ScrollableHeight);
scrollRecord.ChangeView(0, scrollRecord.ScrollableHeight,1.0f,false);
}
private void buttonAdd_Click(object sender, RoutedEventArgs e)
{
try
{
AddText("Adding...");
MenuFlyoutItem m = sender as MenuFlyoutItem;
Route route = m.DataContext as Route;
if (route != null)
{
string colour = route.Colour + "+";
routes.Add(new Route(colour));
}
}
catch (Exception ex)
{
AddText(ex.Message);
}
AddText("Finished");
}
private void buttonRemove_Click(object sender, RoutedEventArgs e)
{
MenuFlyoutItem m = sender as MenuFlyoutItem;
Route route = m.DataContext as Route;
if (route != null)
{
routes.Remove(route);
}
}
private void listView_Holding(object sender, HoldingRoutedEventArgs e)
{
try
{
AddText("listView_Holding fired " + e.HoldingState.ToString());
//ShowFlyout(sender);
}
catch (Exception ex)
{
AddText(ex.Message);
}
}