C# 使按钮的事件处理程序单击启动一个名为MainPage_的新事件
因此,目前我的页面正在使用cs文件中的以下代码。我需要更改的是添加一个事件处理程序,以便在单击按钮时,仅在单击该按钮后才会加载主页面 我知道这条线C# 使按钮的事件处理程序单击启动一个名为MainPage_的新事件,c#,visual-studio,xaml,windows-phone-7,event-handling,C#,Visual Studio,Xaml,Windows Phone 7,Event Handling,因此,目前我的页面正在使用cs文件中的以下代码。我需要更改的是添加一个事件处理程序,以便在单击按钮时,仅在单击该按钮后才会加载主页面 我知道这条线 this.Loaded += new RoutedEventHandler(MainPage_Loaded); 当前正在调用此方法以启动,但当我尝试将此代码放入button1_click事件处理程序时,它不起作用 public MainPage() { InitializeComponent(); thi
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
当前正在调用此方法以启动,但当我尝试将此代码放入button1_click事件处理程序时,它不起作用
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
public void button1_Click(object sender, RoutedEventArgs e)
{
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var ctx = new HazchemEntities(new Uri("http://devweb.compsci.chester.ac.uk/0808092/CO6009/HazchemService.svc/"));
App.coll = new DataServiceCollection<tblChemical>(ctx);
Lst.ItemsSource = App.coll;
App.coll.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(coll_LoadCompleted);
var qry = "/tblChemicals?$filter UNNumber = eq'" + Search.Text + "'";
App.coll.LoadAsync(new Uri(qry, UriKind.Relative));
if (!App.ViewModel.IsDataLoaded)
{
App.ViewModel.LoadData();
}
}
public主页()
{
初始化组件();
this.Loaded+=新的RoutedEventHandler(主页面_-Loaded);
}
公共无效按钮1\u单击(对象发送者,路由目标)
{
}
已加载私有void主页(对象发送方、路由目标)
{
var ctx=新的HazchemEntities(新Uri(“http://devweb.compsci.chester.ac.uk/0808092/CO6009/HazchemService.svc/"));
App.coll=新数据服务收集(ctx);
Lst.ItemsSource=App.coll;
App.coll.LoadCompleted+=新事件处理程序(coll_LoadCompleted);
var qry=“/tblChemicals?$filter UNNumber=eq'”+Search.Text+“”;
App.coll.LoadAsync(新Uri(qry,UriKind.Relative));
如果(!App.ViewModel.IsDataLoaded)
{
App.ViewModel.LoadData();
}
}
下面是我正在使用的XAML代码
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
<TextBox x:Name="Search" Height="72" Margin="-4,0,50,0" Text="TextBox" VerticalAlignment="Top" Width="350" HorizontalAlignment="Left"/>
<Button Content="Go" Height="72" HorizontalAlignment="Left" Margin="350,0,0,0" Name="button1" VerticalAlignment="Top" Width="100" Click="button1_Click" />
</StackPanel>
我需要在button1\u Click事件中添加什么内容才能使其仅在单击此按钮时启动加载的主页面
感谢您将EventHandler分配给主页加载的事件,而不是按钮单击
在构造函数中,尝试
this.button1.OnClick+=newroutedeventhandler(主页已加载)代码>将EventHandler分配给主页加载的事件,而不是按钮单击
在构造函数中,尝试this.button1.OnClick+=newroutedeventhandler(主页已加载)代码>你似乎误解了什么。。。这一行:
this.Loaded+=新的RoutedEventHandler(主页面_-Loaded)
未导致立即调用加载的void主页(对象发送方,RoutedEventArgs e)
函数。它正在为该事件注册一个处理程序——当主页完成加载后,它会在内部触发加载的事件,然后调用事件处理程序
在button1\u Click
中设置该处理程序也没有意义-当可以单击某个按钮时,您的页面已经完全加载完毕
如果你想让按钮1\u单击也使用主页中加载的函数中的某些功能,那么你应该将其重构为一个单独的函数,它们都可以调用。你似乎误解了一些东西。。。这一行:
this.Loaded+=新的RoutedEventHandler(主页面_-Loaded)
未导致立即调用加载的void主页(对象发送方,RoutedEventArgs e)
函数。它正在为该事件注册一个处理程序——当主页完成加载后,它会在内部触发加载的事件,然后调用事件处理程序
在button1\u Click
中设置该处理程序也没有意义-当可以单击某个按钮时,您的页面已经完全加载完毕
如果在MainPage\u Loaded
函数中有一些功能,您希望按钮1\u Click
也使用这些功能,那么您应该将其重构为一个单独的函数,它们都可以调用。将以下代码添加到按钮1\u Click中
MainPage_Loaded(sender,e);
将以下代码添加到按钮1\u单击
MainPage_Loaded(sender,e);
它可以完成任务,但不是一个干净的解决方案。它可以完成任务,但不是一个干净的解决方案。