I have a background task that I want to behave differently if the application is running. How can I detect if my Windows 8 store application is running?
Assuming that you want your app to behave differently when it is in focus, here is how I handled it in my code -
In your class constructor -
CoreWindow.GetForCurrentThread().Activated += CoreWindowOnActivated;
Then, add the implementation -
private void CoreWindowOnActivated(CoreWindow sender, WindowActivatedEventArgs args)
{
if (args.WindowActivationState == CoreWindowActivationState.CodeActivated)
{
//add your app specific behavior here
}
}
You will also need to detach the event when appropriate.
If I was doing this in WinForms I would use a Mutex,
foreground app can lock it
When the foreground app exits for any reason, then windows will unlock the Mutex
The background app can check if the Mutex is locked.
Sorry I don’t know I this work for a windows store app.
Related
Is there a specific way to run long for loops without causing UI freezes on desktop applications using WinRT/C++ libraries?
The goal is that users can still switch pages without having to wait for the previous page using a long for loop to finish loading.
You can use winrt::resume_background to offload the code following into the Windows threadpool, e.g.
winrt::Windows::Foundation::IAsyncAction ExampleAsync()
{
winrt::apartment_context uiThread;
// run the following code in a Windows threadpool thread.
co_await winrt::resume_background();
// long-running code here
for (...)
{
...
}
// if you want to switch to the UI thread again:
co_await uiThread;
// do things on the UI thread
...
}
I have a UWP application.
And i have a need to change locale on the fly, so i have this for language changing:
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = language.FourDigitCode;
ResourceContext.GetForViewIndependentUse().Reset();
ResourceContext.GetForCurrentView();
But there is a problem that system features language doesn't switch ( only after application relaunch ) how can i fix it?
Here is an example:
Now i run this code:
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "lv-LV";
ResourceContext.GetForViewIndependentUse().Reset();
ResourceContext.GetForCurrentView();
The UI gets localized, but system features still remain unlocalized:
But when i restart the app, all is OK:
Any ideas how can i fix it?
I'm afraid there is no fix for this and what you've seen is by design. Ref Remarks of PrimaryLanguageOverride property:
When you set the PrimaryLanguageOverride, this is immediately reflected in the Languages property. However, this change may not take effect immediately on resources loaded in the app UI. To make sure the app responds to such changes, you can listen to the QualifierValues property on a default resource context and take whatever actions may be needed to reload resources. Those requirements may vary depending on the UI framework used by the app, and it may be necessary to restart the app.
For your scenario, a restart is needed. I'd suggest that you can add a tip to tell users to restart the app and also a button to close the app like what used in News App.
And to close the app, we can call Application.Exit method like the following.
Application.Current.Exit();
Maybe page reloading can fix it? Try to re-navigate to the same page.
Found the example below here.
//like this
private bool Reload(object param = null)
{
var type = Frame.CurrentSourcePageType;
Frame.Navigate(type, param);
Frame.BackStack.Remove(Frame.BackStack.Last());
}
// or like this
private bool Reload(object param = null)
{
var frame = Window.Current.Content as Frame;
frame.Navigate(frame.CurrentSourcePageType, param);
frame.BackStack.Remove(frame .BackStack.Last());
}
I have overridden the OnActivated event in the app.xaml, but the handler is never executed
protected override void OnActivated (IActivatedEventArgs args)
{
//Test Code
}
I think you mix Resuming and Activation. In WP8.1 RT it works little different that in WP8.0. Please take a look at App Lifecycle. There is a list in which cases your app is being activated. For example you will have OnActivated launched after FileOpenPicker but you won't get it fired after Suspension (then the Resuming event is fired).
You won't be able to test your OnActivated by using lifecycle event dropdown. To test it you will have to get it invoked (take a look at these lifecycle events at msdn), also open project properties, go to Debug and mark a tick Do not launch, but debug my code when it starts.
Please also remeber that your app can be terminated by the OS (for example due to lack of resources) while it's deactivated/suspended.
I'm trying to use NSUserNotificationCenter. I'm able to deliver notifications successfully. I'm using the ShouldPresentNotification callback on NSUserNotificationCenterDelegate to present notifications, even when the app is running in the foreground.
This works great, except on one of my machines!
I have stripped the code down to it's most basic example. All my machines are running 10.8.3 and Mono 2.10.12. On my 2008 Macbook Pro and a colleague's 2012 rMBP, everything works as excepted. However, on my identical 2012 rMBP the notification is not presented if the app is in the foreground. In fact, on this machine, and this machine only, none of the NSUserNotificationCenterDelegate methods are invoked.
Note that the notification is still delivered on this machine - the notification works - it just doesn't get presented when the app is in the foreground (because the delegate methods are never invoked).
I would really appreciate if anyone has some insight on what settings or configuration might causes this behaviour, or if there is some mechanism I can use to debug this behaviour.
Here is my code:
UNCShouldPresentNotification ShouldPresent = (a, b) => { return true; };
// Shared initialization code
void Initialize()
{
NSUserNotificationCenter.DefaultUserNotificationCenter.ShouldPresentNotification = this.ShouldPresent;
}
partial void notify(NSObject sender)
{
DoNotify();
}
[Export("doNotify")]
private void DoNotify()
{
NSUserNotification notification = new NSUserNotification();
notification.Title = notificationText.StringValue;
NSUserNotificationCenter.DefaultUserNotificationCenter.DeliverNotification(notification);
}
Ok we had exactly the same bug.
Firstly we contacted Xamarin and they've fixed it in the most recent code.
Secondly it was due to overriding BOTH the Delegate delegate (great name I know) and the ShouldPresent setting.
If you want to override the ShouldPresent setting, do this in your NSUserNotificationCenter.Delegate instead.
I hope that's clear. We've a resolved bugzille entry at https://bugzilla.xamarin.com/show_bug.cgi?id=11456
This now works, somewhere between updating MonoMac, Mono and OS X on my machine.
Is there any way to call App Expose in Lion programmatically, for example on an event tap, etc?
If you don't mind using a TOTALLY UNDOCUMENTED API, which might change at any point without notice:
void CoreDockSendNotification(CFStringRef, void *);
(...)
CoreDockSendNotification(#"com.apple.expose.front.awake", NULL);
Other known arguments are #"com.apple.expose.awake" and #"com.apple.dashboard.awake", which activate Mission Control and Dashboard, respectively. #"com.apple.showdesktop.awake" used to activate Show Desktop, but no longer works on current versions of macOS.
Note that most applications should not use these calls -- these actions are intended to be invoked directly by the user.
Expose does not exist in Lion, it has been merged with Spaces into the Mission Control application.
You can launch Mission Control:
[[NSWorkspace sharedWorkspace] launchApplication:#"Mission Control"];