XAML-附加到ListView的MenuFlyUtiItem不';不能在WP8.1中工作
我开发了一个使用MVVM Lght工具包的通用应用程序 在一个页面上,我显示了一个评论列表。我想,用户可以添加一个新的评论,并编辑或删除其现有的评论 为了添加新评论,我在命令栏上使用了一个AppBarButton,效果很好 要编辑和删除现有评论,我想显示一个菜单,其中提供两项:“edit”和“delete”。我可以显示菜单,但当我点击它的项目时,什么都不会发生 这是我关心的xaml代码:XAML-附加到ListView的MenuFlyUtiItem不';不能在WP8.1中工作,xaml,windows-phone-8.1,mvvm-light,relaycommand,flyout,Xaml,Windows Phone 8.1,Mvvm Light,Relaycommand,Flyout,我开发了一个使用MVVM Lght工具包的通用应用程序 在一个页面上,我显示了一个评论列表。我想,用户可以添加一个新的评论,并编辑或删除其现有的评论 为了添加新评论,我在命令栏上使用了一个AppBarButton,效果很好 要编辑和删除现有评论,我想显示一个菜单,其中提供两项:“edit”和“delete”。我可以显示菜单,但当我点击它的项目时,什么都不会发生 这是我关心的xaml代码: <ListView x:Name="myCommentaires"
<ListView x:Name="myCommentaires"
ItemsSource="{Binding Comments}"
IsItemClickEnabled="True"
SelectionMode="Single"
SelectedItem="{Binding SelectedComment}"
ContinuumNavigationTransitionInfo.ExitElementContainer="True">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,19,12"
HorizontalAlignment="Stretch">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- 1. Author -->
<TextBlock Grid.Column="0"
Text="{Binding name}"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
TextAlignment="Left"
Margin="0"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemSubheaderTextBlockStyle}" />
<!-- 2. Date -->
<TextBlock Grid.Column="1"
Text="{Binding date, Converter={StaticResource DateToStringConverter}}"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
TextAlignment="Right"
Margin="0"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemSubheaderTextBlockStyle}" />
</Grid>
<!-- 3. Content -->
<TextBlock Text="{Binding content}"
TextAlignment="Left"
TextWrapping="Wrap"
Margin="0"
Foreground="Black"
FontSize="20"
Style="{StaticResource GroupHeaderTextBlockStyle}" />
<!-- MenuFlyout - with Commands -->
<FlyoutBase.AttachedFlyout>
<MenuFlyout>
<MenuFlyout.MenuFlyoutPresenterStyle>
<Style TargetType="MenuFlyoutPresenter">
<Setter Property="Background" Value="{StaticResource ThemeBrush}"/>
</Style>
</MenuFlyout.MenuFlyoutPresenterStyle>
<MenuFlyoutItem Text="Edit"
Command="{Binding EditCommentCommand}"/>
<MenuFlyoutItem Text="Delete"
Command="{Binding DeleteCommentCommand}"/>
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
<!-- Behavior -->
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<local:OpenFlyoutAction />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
// RelaydCommands declarations
...
public RelayCommand AddCommentCommand { get; set; }
public RelayCommand EditCommentCommand { get; set; }
public RelayCommand DeleteCommentCommand { get; set; }
...
// RelayCommands are affected in the constructor
...
AddCommentaireCommand = new RelayCommand(AddCommentaire);
EditCommentaireCommand = new RelayCommand(EditCommentaire);
DeleteCommentaireCommand = new RelayCommand(DeleteCommentaire);
...
// Methods related to the RelayCommands
private async void AddComment()
{
NavigationService.NavigateTo<EditCommentViewModel>(this, new object[] { _article, _titleStructure.Title }, true);
}
private async void EditComment()
{
NavigationService.NavigateTo<EditCommentViewModel>(this, new object[] { _article, _titleStructure.Title, SelectedComment }, true);
}
private async void DeleteComment()
{
var test = SelectedComment;
}
这是我的视图模型代码:
<ListView x:Name="myCommentaires"
ItemsSource="{Binding Comments}"
IsItemClickEnabled="True"
SelectionMode="Single"
SelectedItem="{Binding SelectedComment}"
ContinuumNavigationTransitionInfo.ExitElementContainer="True">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,19,12"
HorizontalAlignment="Stretch">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- 1. Author -->
<TextBlock Grid.Column="0"
Text="{Binding name}"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
TextAlignment="Left"
Margin="0"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemSubheaderTextBlockStyle}" />
<!-- 2. Date -->
<TextBlock Grid.Column="1"
Text="{Binding date, Converter={StaticResource DateToStringConverter}}"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
TextAlignment="Right"
Margin="0"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemSubheaderTextBlockStyle}" />
</Grid>
<!-- 3. Content -->
<TextBlock Text="{Binding content}"
TextAlignment="Left"
TextWrapping="Wrap"
Margin="0"
Foreground="Black"
FontSize="20"
Style="{StaticResource GroupHeaderTextBlockStyle}" />
<!-- MenuFlyout - with Commands -->
<FlyoutBase.AttachedFlyout>
<MenuFlyout>
<MenuFlyout.MenuFlyoutPresenterStyle>
<Style TargetType="MenuFlyoutPresenter">
<Setter Property="Background" Value="{StaticResource ThemeBrush}"/>
</Style>
</MenuFlyout.MenuFlyoutPresenterStyle>
<MenuFlyoutItem Text="Edit"
Command="{Binding EditCommentCommand}"/>
<MenuFlyoutItem Text="Delete"
Command="{Binding DeleteCommentCommand}"/>
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
<!-- Behavior -->
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<local:OpenFlyoutAction />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
// RelaydCommands declarations
...
public RelayCommand AddCommentCommand { get; set; }
public RelayCommand EditCommentCommand { get; set; }
public RelayCommand DeleteCommentCommand { get; set; }
...
// RelayCommands are affected in the constructor
...
AddCommentaireCommand = new RelayCommand(AddCommentaire);
EditCommentaireCommand = new RelayCommand(EditCommentaire);
DeleteCommentaireCommand = new RelayCommand(DeleteCommentaire);
...
// Methods related to the RelayCommands
private async void AddComment()
{
NavigationService.NavigateTo<EditCommentViewModel>(this, new object[] { _article, _titleStructure.Title }, true);
}
private async void EditComment()
{
NavigationService.NavigateTo<EditCommentViewModel>(this, new object[] { _article, _titleStructure.Title, SelectedComment }, true);
}
private async void DeleteComment()
{
var test = SelectedComment;
}
//RelaydCommands声明
...
公共RelayCommand AddCommentCommand{get;set;}
public RelayCommand EditCommentCommand{get;set;}
公共RelayCommand DeleteCommentCommand{get;set;}
...
//RelayCommand在构造函数中受影响
...
AddCommentaireCommand=新的RelayCommand(AddCommentaireCommand);
EditCommentaireCommand=新的RelayCommand(EditCommentaireCommand);
DeleteCommentaireCommand=新的RelayCommand(DeleteCommentaire);
...
//与RelayCommands相关的方法
私有异步void AddComment()
{
NavigationService.NavigateTo(这是一个新对象[]{u article,{u titleStructure.Title},true);
}
私有异步void EditComment()
{
NavigationService.NavigateTo(这是一个新对象[]{u article,_titleStructure.Title,SelectedComment},true);
}
私有异步void DeleteComment()
{
var测试=SelectedComment;
}
我没有遇到“AddComment”方法的任何问题,该方法是从AppBarButton调用的,但我从未输入方法“EditComment”和“deleteComent”,而我放置断点
我还试图用一个CallMethodAction替换MenuFlyoutItem的命令,但它没有改变任何东西:
<!-- MenuFlyout - with CallMethodAction -->
<FlyoutBase.AttachedFlyout>
<MenuFlyout>
<MenuFlyout.MenuFlyoutPresenterStyle>
<Style TargetType="MenuFlyoutPresenter">
<Setter Property="Background" Value="{StaticResource ThemeBrush}"/>
</Style>
</MenuFlyout.MenuFlyoutPresenterStyle>
<MenuFlyoutItem Text="Edit">
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<core:CallMethodAction MethodName="EditComment" />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</MenuFlyoutItem>
<MenuFlyoutItem Text="Delete">
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<core:CallMethodAction MethodName="DeleteComment" />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</MenuFlyoutItem>
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
=>有人能告诉我出了什么问题吗?您正在listview中定义您的
弹出按钮
,因此datacontext被设置为ViewModel上的Comments
属性。
这意味着找不到您正在使用的两个命令
要解决此问题,您需要引用页面datacontext并使用它来定位命令
为您的
指定一个类似x:name=“PageRoot”
的名称,并将命令的绑定更改为
{Binding ElementName=PageRoot,Path=DataContext.EditCommentCommand}
谢谢,它可以工作了,我现在可以输入deleteComent方法,但我不知道如何获取单击的注释以进行编辑或删除。。。