如何在 Xamarin.Forms 中添加/生成按钮单击事件

问题描述:

我正在开发 Xamarin 表单项目,我在我的 Xaml 页面之一上添加了按钮,但我无法为此按钮生成单击事件,但 Visual Studio 未显示任何智能感知.

I am developing Xamarin forms project, I have added button on one of my Xaml page but I am not able to generate click event for this button but Visual studio is not showing any intellisense.

我在另一篇文章中读到建议为此问题安装 Resharper,但对我来说不可行,因为我没有安装权限.

I have read in another post that suggest installing Resharper for this issue but it is not feasible for me because I do not have installation permission.

我使用的是 Visual Studio 企业版 2017.

I am using Visual Studio Enterprise edition 2017.

使用 Intellisense

在 VisualStudio 中 ctrl+space 启动智能感知.如下图所示,将光标向右移动到引号之间,然后按 ctrl+space.现在应该会弹出 Intellisense 建议创建一个事件处理程序.

Working with Intellisense

In VisualStudio ctrl+space to kick intellisense. Move your cursor right between the quotes, as seen in the image below, and hit ctrl+space. Intellisense should pop up now suggesting to create an event handler.

通常,您可以使用 Xamarin 文档找出事件处理程序方法应该是什么样子.

In general you could use the Xamarin documentation to find out how the event handler method should look like.

公共事件 EventHandler Clicked

public event EventHandler Clicked

该事件使用 .NETs EventHandler,因此它将获得一个 object sender 和一个 EventArgs args 作为参数.

The event uses .NETs EventHandler so it will get an object sender and an EventArgs args as parameter.

//You may have to change the Name depending on how you name that handler in xaml

void OnButtonClicked(object sender, EventArgs args)
{

}

更新:

当然要加上

using System;

在文件顶部以使用 EventArgs.

at the top of your file in order to use EventArgs.