Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xaml 如何在windows phone中使用点击事件设置上下文菜单?_Xaml_C# 4.0_Windows Phone 8_Contextmenu - Fatal编程技术网

Xaml 如何在windows phone中使用点击事件设置上下文菜单?

Xaml 如何在windows phone中使用点击事件设置上下文菜单?,xaml,c#-4.0,windows-phone-8,contextmenu,Xaml,C# 4.0,Windows Phone 8,Contextmenu,您好,我需要在windows phone应用程序中设置内容菜单。我尝试了一些代码,但内容菜单无法启动,因此任何人都可以告诉我为什么我的代码不起作用。我的代码中有什么错误 我的代码如下: <Button Width="113" Click="Home" BorderThickness="0" HorizontalAlignment="Left" Height="87" > <Image Source="Images/home_30.png" Stretch="Uni

您好,我需要在windows phone应用程序中设置内容菜单。我尝试了一些代码,但内容菜单无法启动,因此任何人都可以告诉我为什么我的代码不起作用。我的代码中有什么错误

我的代码如下:

 <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>

我正在使用此代码,但菜单按钮未启动。

您有一个菜单项的单击事件

请尝尝这样的

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

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


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;
    }
public部分类主页:PhoneApplicationPage
{
ViewModel _ViewModel;
公共上下文示例()
{
初始化组件();
_viewModel=新的viewModel();
_viewModel.Notify+=OnViewModelNotify;
LayoutRoot.DataContext=\u viewModel;
}
void OnViewModelNotify(对象发送方,CommandEventArgs e)
{
Debug.WriteLine(string.Format(“ICommand:{0}”,e.Message));
}
私有无效菜单项单击(对象发送方,路由目标)
{
Debug.WriteLine((字符串)((MenuItem)sender.Header);
}
}
公共类CommandEventArgs:EventArgs
{
公共CommandEventArgs(字符串消息)
{
消息=消息;
}
公共字符串消息{get;private set;}
}
公共类视图模型
{
公共ICommand头1命令{get;private set;}
公共事件处理程序通知;
公共视图模型()
{
Header1Command=新的Header1Command();
((Header1Command)Header1Command).Notify+=OnNotify;
}
私有void OnNotify(对象发送方,CommandEventArgs e)
{
var notify=notify;
如果(通知!=null)
{
通知(本,e);
}
}
}
公共类header1i命令:ICommand
{
公共布尔CanExecute(对象参数)
{
返回true;
}
公共事件处理程序CanExecuteChanged;
public void Execute(对象参数)
{
var unused=CanExecuteChanged;
var notify=notify;
如果(通知!=null)
{
通知(这是新的CommandEventArgs(“Header1ICommand-”+(参数??“[null]”));
}
}
公共事件处理程序通知;
}
ICommand的替代解决方案-RelayCommand。有很多例子,但它的工作方式与ICommands几乎相同-它只是更简单,因为您必须编写更少的代码!选择最适合你的


最后一点注意:小心使用ContextMenu,因为我以前遇到过问题,
ContextMenu
DataContext
在对其余元素(文本块等)进行更改时不会更改。

不确定您要做什么,但您的单击/点击不应该在菜单项上吗?例如?嗨,谢谢你的回复。我希望这对我很有用。
private void Copy_Click(object sender, RoutedEventArgs e)
        {
               //handle the event here
        }
<toolkit:ContextMenuService.ContextMenu>
    <toolkit:ContextMenu>
        <toolkit:MenuItem Header="Header1" Command="{Binding Header1Command}"/>
    </toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
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;
    }