I would like to make my Xamarin.Forms cross-platform app have the ability to resume after it sleeps.
I have been googling for a while but haven't found any solutions.
I tried reading this Xamarin tutorial but it doesn't seem to have anything for what I am trying to accomplish.
I tried looking for something like base.OnResume() but there is no base variable or any methods.
Xamarin Forms has an OnSleep and an OnResume method in the Application class
These can be used to save and restore variables when the app is backgrounded and resumed.
It is up to you to save the details you need such as page to load and state to restore. Look at the persist data section.
Here is a blog post about doing this.
You can find this methods in your app.xml file. Handle your code inside that file whatever you want to do onresume
Related
I am using xamarin forms and i came across this plugin https://github.com/thudugala/Plugin.LocalNotification in order to show notifications to user. But how can a progress bar be shown on notification area?
I cant find an example of how this is done.
This is the code i use for sending notification
CrossLocalNotifications.Current.Show("title", "body");
Is there a cross platform solution as the above example? Or a correct implementation using Dependency Services?
What you're trying to achieve is not possible with the local notification plugin. However, it should be rather trivial to extend the library to add an additional argument for the progress data shown on the progress bar.
Basically, you just need to pass two additional values from the Notification Builder by calling the SetProgress(int max, int progress, bool intermediate) method. This is best explained in Google's Android documentation here.
The method were you should add the call to SetProgress() is ShowNow() in the Android specific class /Platform/Droid/NotificationServiceImpl.cs. Of course you also need to make changes elsewhere so that you can provide the max and progress values from the cross-platform code.
If the above solution seems too complex and you're not using the plugin extensively, perhaps you can just construct the notifications yourself in the Android project and execute that code using the dependency service.
Edit: I removed the Google Books link which doesn't seem to work for everyone. Instead, here is an article from Microsoft Docs, detailing how the local notifications can be created. The only additional thing that is missing from the guies is the SetProgress method which is required to show the progress bar.
Also, notice that you need to submit the notification again and again to show progress in the progress bar. Check the third reply (from Cheesebaron) on this thread in the Xamarin Forums for a short explanation and bits of code on how it works.
I'm trying to close Xamarin Forms App from a logout button. However the code I find (example below) leaves the app in a second plan without terminate it completely.
Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
Is it possible to completely close the app without leave it running in a second plan? If so, please provide example code.
There is no approved way of doing this. At the very least not for iOS. There are private APIs or workarounds but if your plan is to have your app in the App Store(s) it will never pass the review process.
A simple way to make it work cross platform is by this command:
System.Diagnostics.Process.GetCurrentProcess().Kill();
However, Apple strongly discourages terminating apps. From here.So suggest that not doing this in IOS.^.^
I just started learning UWP and i'm really confused on how it works. I already saw tens of posts that talk about my problem but can't figure out how to do what I want.
So I want to make an app that runs on windows startup, I want the app to be not visible so it needs a background Task, how can I trigger this background task without getting to the app UI ?
The app is supposed to have the Background Task always running, and its interface is supposed to be used as "settings" so I don't need the app to be shown on startup.
Thanks.
I found my way here after a lot of googling. to be honest I have come to the same conclusion as Motaz. But as of writing this I am way too invested in what I have already. While what I have here is not the perfect answer to his question. I wanted to come back and post what I've learned for anyone else who ends up here.
My need is a app that when started will monitor a third party USB device until the app is closed (regardless of whether the app is minimized or not)
Windows Template Studio is good, but the docs not so much. Especially when it comes to Background tasks.
I started here: https://github.com/Microsoft/Windows-universal-samples. But there is two problems.
They combined all the examples and some of the code is shared, which makes it difficult to pull a project out and hack it apart without breaking the original examples.
Following their background task example I perpetually had issue with the manifest and and it wanting an audio task
I went back to the template studio and created the simplest version with a background task possible. After a lot of trial and error I got something that works. I have posted it here: https://github.com/PillarOfSociety/WindowsTemplateStudio-BackgroundTask
Some things to note:
I am no expert on UWP and while this runs I have no intent on putting it in the store nor did I try.
If you do download my project and run it, you should be able to just hit the "Add events" and the "Start Back" button and the task should run and update the Progress bar.
I used an ApplicationTrigger. the original example from the template uses a TimeTrigger which takes time in MINUTES (took me too long to figure that out). Supposedly Application triggers have a 30sec timeout.. but on my laptop they live for much longer.. I don't know how long. This page is useful: https://learn.microsoft.com/en-us/windows/uwp/launch-resume/support-your-app-with-background-tasks
The template studio generated BackgroundTaskService will leave background tasks registered after the app is closed, but will NOT make the connection back to them once its rerun, so on a rerun either the task appears not to run, or will crash the app when triggered.
Important Code I discovered:
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
TestText += task.Value.Name; //gets the typename of the task
//task.Value.Unregister(true); //will unregister tasks
}
await Task.CompletedTask;
The tasks in BackgroundTaskRegistration.AllTasks are not the BackgroundTask class that the template studio uses. IN my example I unregister all of them each time it runs so that you have a reference to the task as an instance of BackgroundTask.
Hopefully this helps someone and Good luck!
If you're just starting out use Windows Template Studio, it will be perfect for you. It is an extension of Visual Studio which lets you create new uwp projects with a lot of built in features, and you can only choose the features you want. It will save you a lot of time on basic stuff.
https://github.com/Microsoft/WindowsTemplateStudio
I have put some code inside of the public MainWindow() {} but I kept getting some obscure XAML parsing errors as soon as I did that (not on my computer but on 3 others I've tried it on - yep!)
Is there the preferred way to run code AS SOON as t he application starts?
The theory is I want it to call home and ask it it's ok to start. If it's not, I want the app to close out. Call it a makeshift copy-protection :)
Under normal circumstances, WPF creates the Main method (the entrypoint of the application) for you. Your options
Create a handler for the Application.Startup event and put your code there. Alternatively, you can override the OnStartup() method.
If that's too late for you, put your code in the App's parameterless constructor (it probably doesn't exist, but you can create it).
If even that's too late, you can create your own Main() method. There are several ways how to do that. Probably the easiest is to put it in another class and tell Visual Studio you want to use this method in the project's properties.
On the other hand, you said you're getting some obscure XAML parsing errors. Maybe you should figure out what exactly do they mean?
You have Window.Loaded event in WPF.
But if if you want to check for run permission before application loads ( due some resource consuption or some business strategy) use a bootstrapper a separate small executable that first launched by mainexe and after if everything ok a bootstrapper runs main exe
I have a small calculation system that will be installed in multiple PCs. Those PCs have a program installed in them and this program is always open (but minimized).
I need to get the value of a 'RadioButton' in this program but I can't access it!
I've tried to get an API for this application but the company developed it refused to co-operate. I've got a crazy idea, you can refer to my question regarding this idea Click It and the last thing is to get the cache for the button but I think this is going to work with WebApplications only!
Any help would be Great! =) .. And btw I decided to build my application as web application but then I changed my mind to build it as a WinForms because of the screenshot. But if I got a better and a more clean solution I'd go back to WebApp
Screen grabbing is not a good option. You should be able to use FindWindow based methods to get hold of the radio button if the app uses windowed controls. If not then UI Automation may very well be able to get the value of the radio button. Whatever it's usually possible to read UI state out of another application with methods like this, and much easier than screen scraping.