How do I use the event VisibilityChanged? - c#

I need to re-run the code contained within dela method OnNavigatedTo () when the app resumes from background.
To do this I need the event VisibilityChanged:
Link MSDN
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
//My code
}
With this event, each time the app is opened again from the background, the code contained within dell'OnNavigatedTo runs again. How can I use that event? I can not.

Visibility changed is only relevant if the page isn't in the background. Every time the app opens it goes to OnNavigatedTo(), you can make a bool or counter to check if it is the first time you entered the page and then decide what to do based on that inside OnNavigatedTo. for Example:
private override void OnNavigatedTo(NavigationEventArgs e)
{
if(hasBeenHere) Repeat_Visit(args);
else First_Visit(args);
}

The Application_Activated Event gets fired when the application is resumed from the background.
From the App.xaml.cs of the WP8.1 Template:
// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
// your code
}
Edit : to call methods on MainPage from app.xaml.cs has already been answered here : How to use a method in MainPage from app.xaml.cs with variable appbar pivot

Related

c# windows form- How to start my application automatically by click Button1

Working in windows form application, and I need to close my work.exe then wait 4 second time and then start it again with click button1.
is that possible?
Application.Exit();
delay(400);
System.Diagnostics.Process.Start("C:\\Users\\Administrator\\Desktop\\application.exe");
delay(300);
button1.PerformClick();
When you call the Exit method the application closes, so the rest of the code is never executed.
What you can do is to hide it instead of closing it.
you could use the override method and do something like this for instance
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
this.button1(null, null);
}

How can I access to e.handled for a button in silverlight?

I have some buttons in master page (mainpage) and I use the event click of its in other page by map like:
MainPage mp = (MainPage)(((BusinessApplication8.Controls.BusyIndicator)Application.Current.RootVisual).Content);
it's being run more than one and I can not use a e.Handdled to prevent them.
How can I do to control it?
for example we want our code inside below event(ListPish_Emza_Click) runs only once per click :
void ListPish_Emza_Click(object sender, RoutedEventArgs e)
{
if (sender == e.OriginalSource)
return;
Approve_Click(sender, e);
}
but above code runs more than one per click.
Firing an event manually is not recommended approach.
I can't realize your application architecture. But, you should put your code in a method in static class and use it.

Code in Application_Activated not running when phone wakes up

I've done some reseach on the life cycle of Windows Phone apps and I've gathered that when the phone is locked whilst an app is still running, and you unlock the phone the 'Application_Activated' function is called in the App.xaml.cs file.
// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
//Code to run
MessageBox.Show("Hello there!");
}
Now in the above example, the simple 'MessageBox' call doesn't get run. Like I said, if you have your app running and you lock the phone, and then unlock the phone the above code is expected to run, in this case display a MessageBox as soon as you unlock the phone.
Any help would really be appreciated! Thanks.
You can not do that
If you call Show(String) method from the app Activated and Launching event
handlers an InvalidOperationException is thrown with the message Error
Displaying MessageBox.
it is in msdn
if you wanna show same message my suggestion is to use OnNavigatedTo event
EDIT
if i understood correctly you wanna change default page navigation
1.One way to do this:
In WMAppManifest.xml replace the property of Navigation Page with your desire page
An alternative:
In WMAppManifest.xml remove the property of Navigation Page
private void Application_Launching(object sender, LaunchingEventArgs e)
{
RootFrame.Navigate(new Uri("YourPage.xaml", UriKind.Relative));
}
private void Application_Activated(object sender, ActivatedEventArgs e)
{
RootFrame.Navigate(new Uri("YourPage.xaml", UriKind.Relative));
}
This way you can "play" with IsolatedStorageSettings for example
if (boolvariable)
{
RootFrame.Navigate(new Uri("YourPage.xaml", UriKind.Relative));
boolvariable = false;
}
else
{
RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
It just an idea, let me know how it goes (:

Capture Activated event for an App

I am referring to http://msdn.microsoft.com/en-us/library/windows/apps/hh464925.aspx#app_activation
// App is an Application
public App()
{
this.InitializeComponent();
// Doesn't compile
//this.Activated += OnActivated;
this.Suspending += OnSuspending;
}
protected override void OnActivated(IActivatedEventArgs args)
{
System.Diagnostics.Debug.WriteLine("OnActivated");
}
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
deferral.Complete();
}
Note, OnActivated will never be triggered. OnSuspending will be triggered, after I quit the app around 30 seconds.
How can I capture Activated event? It is weird that I do not find Activated event in App, although the documentation says so.
The activation is a little confusing. Take a look at the documentation here:
http://msdn.microsoft.com/en-us/library/windows/apps/br242324.aspx
What you'll find is that when the user taps the tile, only the OnLaunched event is fired (see documentation here: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.application.onactivated.aspx )
The OnActivated is only for special cases outside of the ordinary launch. That would be any one of the following:
OnFileActivated
OnSearchActivated
OnShareTargetActivated
OnFileOpenPickerActivated
OnFileSavePickerActivated
OnCachedFileUpdaterActivated
So if you truly want something that is called anytime the app is activated regardless of how, I'd suggest making your own private method, then calling it from both OnLaunched and OnActivated. That should hit all cases for activation.
Maybe the problem is that you launch the app normally for example, by tapping the app tile(without facts, I'm just guessing). In this case, only the OnLaunched method is called.
msdn

Calling my own event code and custom event code in c#

I have made a WinForms application with a custom richtextbox control.
I am referring it as a Windows control (a dll file) in my project. In my program for the richtextbox I have written functionality inside it's textchanged event.
I want to do additional work only after the textchanged event is fired or in other ways once the text is added to the textbox. Something like I want to call a function foo() in the text_changedevent. It only calls foo and fails to process the underlying textchanged event.
Any way in C# I can make it process the internal textchanged event first, and then look into my text changed event?
think of the scenario I have written the code for mytextbox_textchanged
private void txt_code_TextChanged(object sender, EventArgs e)
{
//some code which will always be called whenever textchanged event occurs.
}
Now I inherit this control in my project say MyApp1. Here I have a label where I want to display the number of lines contained inside my textbox. So I would write
private void my_inherited_txt_code_TextChanged(object sender, EventArgs e)
{
//code to update the label with my_inherited_txt_code.lines.length
}
so my problem was, I first wanted the txt_code_TextChanged event to be called and then do the code written inside my_inherited_txt_code_TextChanged. Which was solved by writing
private void my_inherited_txt_code_TextChanged(object sender, EventArgs e)
{
base.OnTextChanged(e);
MessageBox.Show("foo");
}
Do you mean:
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
// your code here...
}
I cant see why you shouldnt be able to call a method from the text_changed event?
Do you get any errors or what happens exactly?

Categories