Is it OnNavigatedTo or Loaded Event? I've been able to use both interchangeably but I would like to know explicitly which comes first.
OnNavigatedTo fires first -- source - A simple experiment
See code below
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
}
// Simple button Click event handler to take us to the second page
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));
}
}
in OnNavigatedTo:
System.Diagnostics.Debug.WriteLine("in OnNavigatedTo");
in Loaded:
System.Diagnostics.Debug.WriteLine("in Loaded");
Run in Debug mode, see which one writes first.
Related
My app has multiple pages.
When I press the Close key in the toolbar, how can I detect the OnClosing event in the single page to avoid closing the app instead of going back to the MainWindow with "this.Frame.GoBack();" ?
I can only catch OnClosing on MainWindow
You could store a reference to the window in a property or field in your App.xaml.cs class as suggested here and then handle the Closing event of the window in the Page class something like this:
public sealed partial class MainPage : Page
{
private Window _parentWindow;
public MainPage()
{
this.InitializeComponent();
this.Loaded += OnLoaded;
this.Unloaded += OnUnloaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
_parentWindow = (Application.Current as App)?.m_window;
if (_parentWindow != null)
_parentWindow.Closed += OnWindowClosed;
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
if (_parentWindow != null)
_parentWindow.Closed -= OnWindowClosed;
}
private void OnWindowClosed(object sender, WindowEventArgs args)
{
// Prevent the window from being closed based on some logic of yours...
args.Handled = true;
}
}
You can use the Unloaded event on Pages.
*.xaml
<local:TestPage Unloaded="TestPage_Unloaded" />
*.xaml.cs
private void TestPage_Unloaded(object sender, RoutedEventArgs e)
{
// Do your page closing work here...
}
I'm calling a button click in the form_load like this:
public void Form1_Load(object s, EventArgs e)
{
button.PerformClick();
}
But upon loading the button does not get clicked, what am I doing wrong?
You can write whatever you want to do inside of click in another function and call that from inside the click handler or programmatically like this -
public void Form1_Load(object s, EventArgs e)
{
//button.PerformClick();
PerformClickAction();
}
void button_click(object sender,EventArgs e)
{
PerformClickAction();
}
void PerformClickAction()
{
// Write what you need to do on click
}
This works for me:
public void Form1_Load(object s, EventArgs e){
button.PerformClick();
}
Looks like you didn't register the Form1_Load as event handler for the Load event of your form. Try this:
public Form1(){
InitializeComponent();
Load += Form1_Load;//Register the event handler so that it will work for you.
}
To get the button clicked on form load, you need to fire an event after the form is loaded, try this
public Form1()
{
InitializeComponent();
//Event fired
this.Load += new System.EventHandler(this.button1_Click);
}
//Event Handler
private void button1_Click(object sender, EventArgs e)
{
//do something
}
I've created a custom user control for a windows form that will operate similar to a button (and please don't suggest that I just use a button, because I will be storing data in this user control), but I can't figure out how to get the OnClick() event to fire. I've sifted through a handful of tutorials and looked at a few similar questions on the site, but I can't seem to get the event to fire off - so I'm either doing something wrong or everyone posted incorrect code (I hope it isn't the latter)
In my custom control.cs,
namespace MobCreator {
public partial class MOBSample : UserControl {
public MOBSample() {
InitializeComponent();
}
protected override void OnMouseUp(MouseEventArgs e) {
this.BorderStyle = BorderStyle.FixedSingle;
base.OnMouseUp(e);
}
protected override void OnMouseDown(MouseEventArgs e) {
this.BorderStyle = BorderStyle.Fixed3D;
base.OnMouseDown(e);
}
public event EventHandler ButtonClick;
private void OnButtonClick(object sender, EventArgs e) {
// invoke UserControl event here
if (this.ButtonClick != null) this.OnButtonClick(sender, e);
}
}
}
And in my form.cs,
private void MobCreatorForm_Load(object sender, EventArgs e) {
UserControl1.ButtonClick += new EventHandler(this.CustomEvent_Handler);
}
private void CustomEvent_Handler(object sender, EventArgs e) {
Console.WriteLine("Click");
}
However, when I run the program my console never outputs "Click".
Check this link on MSDN: it is a simple Event tutorial, you should be able to adapt it to your scenario.
At a first look, what you are probably missing is a Delegate for your event.
Try this
private void MobCreatorForm_Load(object sender, EventArgs e)
{
CustomEvent_Handler(null,null);
}
private void CustomEvent_Handler(object sender, EventArgs e)
{
Console.WriteLine("Click");
}
I'm building a Windows Phone app and I can't make a dynamically created Button associate with its event handler.
The code I'm using is:
public partial class MainPage : PhoneApplicationPage
{
Button AddButton
public MainPage()
{
CreateInterestEntry();
}
private void CreateInterestEntry()
{
EntrySegment = getEntrySegment();
ContentPanel.Children.Add(EntrySegment);
}
private void AddButton_Click(Object sender, RoutedEventArgs e)
{
//Stuff to do when button is clicked
}
private Grid getEntrySegment()
{
Grid EntrySegment = new Grid();
//Creating the grid goes here
AddButton = new Button();
AddButton.Content = "Add";
AddButton.Click += new EventHandler(AddButton_Click);
return EntrySegment;
}
}
}
With this code it complains that no overload for AddButton_Click matches delegate "System.EventHandler".
It's virtually identical to the code under Examples here at msdn, with the exception I've changed the argument type in AddButton_Click from EventArgs to RoutedEventArgs as otherwise Visual Studio tells me that it cannot implicitly convert from System.EventHandler to System.RoutedEventHandler.
Thanks in advance
Try this:
AddButton.Click += new RoutedEventHandler(AddButton_Click);
And the Click Event Handler:
void AddButton_Click(object sender, RoutedEventArgs e)
{
}
I've been trying to template control panels in my site so I can take a panel and populate it fully. I'm good up until the point where my event handling needs to access functions on my page. My current test will take me to a login redirect page. So how can I get this event handler to perform a redirect?
public class DebugButton : Button
{
public string msg;
public DebugButton()
{
this.Click += new System.EventHandler(this.Button1_Click);
this.ID = "txtdbgButton";
this.Text = "Click me!";
msg = "not set";
}
protected void Button1_Click(object sender, EventArgs e)
{
msg = "Event handler clicked";
}
}
*on the Page*
protected void Page_Load(object sender, EventArgs e)
DebugButton btnDebug = new DebugButton();
PnlMain.Controls.Add(btnDebug);
Really appreciate the help. Thanks!
To do a redirect you can use:
Note:
Assuming that your login page is named login.aspx and it is located in root folder of your website.
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/login.aspx");
}
or
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("login.aspx");
}
If you want the event to have access to the page, then the page needs to subscribe to the click event.
Aka:
on the Page
protected void Page_Load(object sender, EventArgs e)
{
DebugButton btnDebug = new DebugButton();
btnDebug.Click += new System.EventHandler(Button1_Click);
PnlMain.Controls.Add(btnDebug);
}
protected void Button1_Click(object sender, EventArgs e)
{
// access whatever you want on the page here
}
I just found out that that System.Web.HttpContext.Current will get me the current context of the page. So long as the custom class is part of the app(this one is in the apps folder of course) I'm good to go. Heres a sample of my quick TestTemplate that I used to make a custom button.
public class TestTemplate : Button
{
public TestTemplate()
{
this.Text = "Click Me";
this.ID = "btnClickMe";
this.Click += new System.EventHandler(this.EventHandler);
//
// TODO: Add constructor logic here
//
}
public void EventHandler(object sender, EventArgs e)
{
//System.Web.HttpContext.Current.Server.Transfer("Default.aspx");
System.Web.HttpContext.Current.Response.Write("This is a test!");
}
}