如何在Windows Phone中点击/点击事件设置上下文菜单?

时间:2013-09-03 11:20:45

标签: xaml c#-4.0 windows-phone-8 contextmenu

嗨我需要在windows phone app中设置内容菜单。我尝试了一些代码,但是内容菜单没有触发,所以任何人都告诉我为什么我的代码不能正常工作。我的代码出了什么问题。

我的代码如下:

 <Button  Width="113" Click="Home" BorderThickness="0" HorizontalAlignment="Left" Height="87" >
    <Image Source="Images/home_30.png"  Stretch="Uniform"  VerticalAlignment="Top" Height="66" Width="68"  />
     <toolkit:GestureService.GestureListener>
        <toolkit:GestureListener Tap="GestureListener_Tap" />
     </toolkit:GestureService.GestureListener>
     <toolkit:ContextMenuService.ContextMenu>
         <toolkit:ContextMenu    x:Name="menu">
               <toolkit:MenuItem Header="Add"/>
               <toolkit:MenuItem Header="Update"/>
               <toolkit:MenuItem Header="Delete"/>
         </toolkit:ContextMenu>
     </toolkit:ContextMenuService.ContextMenu>
  </Button>

我的C#代码

private void GestureListener_Tap(object sender, GestureEventArgs e)
    {
        Button button = sender as Button;
        ContextMenu contextMenu = ContextMenuService.GetContextMenu(button);
        if (contextMenu.Parent == null)
        {
            contextMenu.IsOpen = true;
        }
    }

我正在使用此代码,但菜单按钮不会触发。

2 个答案:

答案 0 :(得分:1)

您有MenuItem的Click事件

请尝试这样的事情

<toolkit:MenuItem x:Name="Copy" Header="Copy" Click="Copy_Click"/>

关于你背后的代码

private void Copy_Click(object sender, RoutedEventArgs e)
        {
               //handle the event here
        }

答案 1 :(得分:0)

当你有一个对象并且你为它设置了一个上下文菜单时,Rithesh Baradi发布的内容是正确的,但是当上下文菜单应用于列表时可能不那么方便。考虑来自Windows Phone Toolkit的以下解决方案(为简单起见,我只接受了一个命令):

<toolkit:ContextMenuService.ContextMenu>
    <toolkit:ContextMenu>
        <toolkit:MenuItem Header="Header1" Command="{Binding Header1Command}"/>
    </toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>

ViewModel中的代码:

public partial class MainPage : PhoneApplicationPage
    {
        ViewModel _viewModel;

        public ContextMenuSample()
        {
            InitializeComponent();
            _viewModel = new ViewModel();
            _viewModel.Notify += OnViewModelNotify;
            LayoutRoot.DataContext = _viewModel;
        }

        void OnViewModelNotify(object sender, CommandEventArgs e)
        {
            Debug.WriteLine(string.Format("ICommand: {0}", e.Message));
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
             Debug.WriteLine((string)((MenuItem)sender).Header);
        }
    }

    public class CommandEventArgs : EventArgs
    {
        public CommandEventArgs(string message)
        {
            Message = message;
        }

        public string Message { get; private set; }
    }

    public class ViewModel
    {
        public ICommand Header1Command { get; private set; }

        public event EventHandler<CommandEventArgs> Notify;

        public ViewModel()
        {
            Header1Command= new Header1ICommand();
            ((Header1ICommand)Header1Command).Notify += OnNotify;
        }

        private void OnNotify(object sender, CommandEventArgs e)
        {
            var notify = Notify;
            if (notify != null)
            {
                notify(this, e);
            }
        }
    }

    public class Header1ICommand: ICommand
    {
        public bool CanExecute(object parameter)
        {
            return true;
        }
        public event EventHandler CanExecuteChanged;
        public void Execute(object parameter)
        {
            var unused = CanExecuteChanged;
            var notify = Notify;
            if (notify != null)
            {
                notify(this, new CommandEventArgs("Header1ICommand- " + (parameter ?? "[null]")));
            }
        }
        public event EventHandler<CommandEventArgs> Notify;
    }

ICommand MVVM Light Toolkit的另一种解决方案 - RelayCommand。有很多例子,但它与ICommands的工作原理相同 - 它更简单,因为你必须编写更少的代码!选择最适合你的。

最后注意事项:小心ContextMenu,因为我遇到问题之前DataContext的{​​{1}}不会改变,而其余的元素(文本块等)也会改变。