如何使Windows Phone的上下文菜单的工作?(How to make context men

2019-07-17 21:00发布

我使用的ContextMenu从Windows Phone的控制工具。 想知道我怎么知道哪些列表项在列表中按下了吗? 看来我可以知道哪个上下文菜单中选择,但我也没有办法知道哪些列表项手术。 请帮忙。 谢谢!

        <DataTemplate x:Key="ListItemTemplate">
            <StackPanel Grid.Column="1" VerticalAlignment="Top">
                <TextBlock Tag="{Binding Index}"  Text="{Binding SName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" />
              <toolkit:ContextMenuService.ContextMenu>
                <toolkit:ContextMenu>
                  <toolkit:MenuItem Header="Add to playlist" Click="Move_Click"/>
                </toolkit:ContextMenu>
              </toolkit:ContextMenuService.ContextMenu>
            </StackPanel>

        private void Move_Click(object sender, RoutedEventArgs e)
    {
        String name = (string)((MenuItem)sender).Header;
        // how to know which index of the item is targeted on
    }

Answer 1:

我还建议MVVM,但它可以简化。 没有必要去到具有视图模型为每个对象的极端。 关键是要命令绑定到你的ItemsControl的DataContext的(如列表框)。

让我们假设你的ItemTemplate是一个列表框,列表框有它绑定到您的视图模型ItemsSource属性。 XAML中是这样的:

<ListBox x:Name="SongsListBox" ItemsSource="{Binding Songs}">
    <ListBox.ItemTemplate>
        <DataTemplate >
            <StackPanel >
                <TextBlock Text="{Binding SName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" />
                <toolkit:ContextMenuService.ContextMenu>
                   <toolkit:ContextMenu>
                       <toolkit:MenuItem Header="Add to playlist" Command="{Binding DataContext.AddToPlaylistCommand, ElementName=SongsListBox}" CommandParameter="{Binding}"/>
                   </toolkit:ContextMenu>
                </toolkit:ContextMenuService.ContextMenu>
            </StackPanel>              
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

您的视图模型,然后将有产权Songs ,那是你的模型对象的集合。 它也有一个ICommand AddToPlaylistCommand 。 正如我以前说过 ,我最喜欢的实现的ICommand是从PNP队DelegateCommand。

public class ViewModel : INotifyPropertyChanged
{
    public ViewModel()
    {
        Songs = new ObservableCollection<Songs>();
        AddToPlaylistCommand = new DelegateCommand<Song>(AddToPlaylist);
    }
    public ICollection<Songs> Songs { get; set; }
    public ICommand AddToPlaylistCommand  { get; private set; }

    private void AddToPlaylist(Song song)
    {
        // I have full access to my model!
        // add the item to the playlist
    }

    // Other stuff for INotifyPropertyChanged
}


Answer 2:

答案是:MVVM。 不要在事件中使用注册码,而背后有一个命令,在视图模型调用。 首先,而不是绑定到数据对象绑定到视图模型(或代表一个列表项,如果你在一个列表是一个视图模型)。 然后,而不是使用Click事件,有您的视图模型公开一个命令可以直接在数据绑定VM进行调用。

下面是我的一个简单的例子联合国新闻 OSS WP7应用程序:( XAML , C# )

<DataTemplate x:Key="ArticleItemDataTemplate">
    <StackPanel>
        <toolkit:ContextMenuService.ContextMenu>
            <toolkit:ContextMenu>
                <toolkit:MenuItem Command="{Binding NavigateToArticle}" Header="read article"/>
                <toolkit:MenuItem Command="{Binding ShareViaEmail}" Header="share via email"/>
                <toolkit:MenuItem Command="{Binding ShareOnFacebook}" Header="share on facebook"/>
                <toolkit:MenuItem Command="{Binding ShareOnTwitter}" Header="share on twitter"/>
            </toolkit:ContextMenu>
        </toolkit:ContextMenuService.ContextMenu>
        <TextBlockText="{Binding Title}">
    </StackPanel>
</DataTemplate>
public ICommand ShareOnTwitter
{
    get
    {
        return new RelayCommand(() => 
            IoC.Get<ISocialShareService>().ShareOnTwitter(ShareableOnSocialNetwroks));
    }
}

public ICommand ShareOnFacebook
{
    get
    {
        return new RelayCommand(() =>
            IoC.Get<ISocialShareService>().ShareOnFacebook(ShareableOnSocialNetwroks));
    }
}

public ICommand ShareViaEmail
{
    get
    {
        return new RelayCommand(() =>
            IoC.Get<ISocialShareService>().ShareViaEmail(ShareableOnSocialNetwroks));
    }
}

下面是我使用了同样的想法的另一幅简化样本神经元 WP7 OSS项目:( XAML , C# )

    <DataTemplate x:Key="YouTubeVideoItem">
        <Grid>
            <Button >
                <toolkit:ContextMenuService.ContextMenu>
                    <toolkit:ContextMenu IsZoomEnabled="False">
                        <toolkit:MenuItem Command="{Binding NavigateToVideo}" Header="play video" />
                        <toolkit:MenuItem Command="{Binding ViewInBrowser}" Header="open in browser" />
                        <toolkit:MenuItem Command="{Binding SendInEmail}" Header="share via email" />
                        <toolkit:MenuItem Command="{Binding FacebookInBrowser}" Header="share on facebook" />
                        <toolkit:MenuItem Command="{Binding TweetInBrowser}" Header="share on twitter" />
                    </toolkit:ContextMenu>
                </toolkit:ContextMenuService.ContextMenu>
                <Custom:Interaction.Triggers>
                    <Custom:EventTrigger EventName="Click">
                        <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding NavigateToVideo}"/>
                    </Custom:EventTrigger>
                </Custom:Interaction.Triggers>
                <StackPanel Orientation="Horizontal">
                    <Image Height="90" Source="{Binding ImageUrl}" />
                    <TextBlock Width="271" Text="{Binding Title}" />
                </StackPanel>
            </Button>
        </Grid>
    </DataTemplate>
    public ICommand ViewInBrowser
    {
        get
        {
            return new RelayCommand(() =>
                TaskInvoker.OpenWebBrowser(this.ExternalLink.OriginalString)
            );
        }
    }

    public ICommand TweetInBrowser
    {
        get
        {
            return new RelayCommand(() =>
                IoC.Get<IMessenger>().Send(new NavigateToMessage(PageSources.WebBrowser, TwitterUri)));
        }
    }

    public ICommand FacebookInBrowser
    {
        get
        {
            return new RelayCommand(() =>
                IoC.Get<IMessenger>().Send(new NavigateToMessage(PageSources.WebBrowser, FacebookUri)));
        }
    }


文章来源: How to make context menu work for windows phone?