So at the moment my page is currently working with the following code in my cs file. What I need to change is to add an event handler so that when a button is clicked the MainPage_Loaded happens only after this button is clicked.
I understand that the line
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
is currently calling this method to start but when I try and put this code into the button1_click event handler it does not work.
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();
}
}
Here is the XAML code that I am using.
<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>
What do I need to put into the button1_Click event to make it start the mainpage_loaded only when this button is clicked?
Thanks
You seem to have misunderstood something… This line:
is not causing the
void MainPage_Loaded(object sender, RoutedEventArgs e)function to be called immediately. It is registering a handler for that event – when the MainPage has finished loading it internally fires the Loaded event, in turn the event handler gets called.Setting up that handler in the
button1_Clickalso makes no sense – your page has already well and truly loaded by the time a button can be clicked.If you have some functionality in the
MainPage_Loadedfunction that you want thebutton1_Clickto also use then you should refactor it out into a separate function that they can both call.