如何在Windows Phone中自动引发事件?例如,我有一个元素<Image name = "image" .... />
。我想在加载MainPage时,它会自动在该元素上引发tap事件
答案 0 :(得分:1)
如果要动态声明点击事件(在页面加载时加载点击事件),您可以按以下方式声明它。这是你的xaml。
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Image Name="image1"/>
</Grid>
在构造函数中,
public MainPage()
{
InitializeComponent();
image1.Tap += image1_Tap;
}
void image1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
//Perform your action here
//This method invokes only when you tap on image
}
否则,请尝试另一种方式。
Loaded += (s, e) =>
{
//Actions that are performed when image is tapped
}
在构造函数中添加上面的行。
答案 1 :(得分:0)
对于你想要完成的任何事情,这可能不是一个好的解决方案。可以使用反射来完成 - How to manually invoke an event?但是,我只是将Tap
事件代码提取到方法中,然后在Loaded
事件中调用此方法,并在Tap
中调用此方法。