Silverlight/WP7 - Navigation and events triggering on other pages - c#

My basic issue is this, I have events firing on pages I've left based on network activity that are causing problems when I thought the old forms were being destroyed.
More detailed information: I am writing a windows phone app that communicates with a network player. I have a static instance of my communication class in my App class so all the forms can share the connection, and all my forms subscribe to it and process results within that form. From a main menu you can choose one type of source and it opens a file browsing form that refreshes a listbox as you navigate, cancels the back button and refreshes the new contents to simulate file navigation until you are the root folder. The app doesn't know if you're clicking on a file or folder, it gets a network message when media starts playing and watch for that and then navigate to a "play" form. I had been using all .Navigate's for this until now and it worked great until I added another branch off the main menu for a new source. Although the new source is completely different, the device sends a lot of the same generic commands which just mean something else in the current context. After visiting the my file browser form and going to my new source, a play command from the network, which means something else now, would cause my to jump into my old "play" form from the previous source as if I was still on the file browser form, which isn't intended.
So I've tried many things and have it kind of working now but it's message and I lose some features. Currently I changed from using all .navigates, also in the back button override, to trying to use the stack and navigate.goback's. I pass variables when needed using globals in App and unhook my net listeners from the form, goback, and then connect them in the new form's listeners in it' navigatedto. I think there is timing issue though as in some cases I needed to send a command to the media box as it's changing and it ended up triggering the wrong event handler again. I think the easiest solution, if possible, and they way I though it would work is if each time I navigated from the form it old one, it's handlers, etc were all destroyed and I didn't have to use the stack at all, handling all the back buttons myself.
I know that's a long description and thanks if you made it this far, hopefully it made some kind of sense. Does anyone have any suggestions on what I can do?
As a side note I'm a long time self-taught VB programmer who has been stuck in .net 2.0/winforms and I've just now made the move to C#, OOPs, and XAML to write my first Windows Phone app so it's likely I'm doing something stupid or overlooking something obvious...

It is likely because something has retained reference to the form. The most common cause is event handlers.
So, if your static class exposes an event, and you subscribe to that event in a form, you must unsubscribe from the event when your form closes / navigates, otherwise the form will remain in memory....
If that isn't the case, look for something else that is acquiring a reference to your form and not releasing it.

Most likely the problem is based on a bad application architecture, when it comes to handling commands send from the UI.
When you say 'sends a lot of the same generic commands which just mean something else in the current context.' you most likely reveal the source of the problem.
As a workaround, you can define an interface, that your communication class implements. Each form has it's own method it calls on a communication class instance.
If you indeed receive a command from a phone page, that is no longer in view, just don't process it.
You can store the navigation history to always know what page is the only one allowed to send commands to a communication class.

Related

Page state in Windows Runtime

My problem is that I want to know when the user navigated to the page and when navigated away. So I can perform some page specific tasks like subscribing and unsubscribing to events.
Previously it was obvious and we could override OnNavigatedTo and OnNavigatingFrom methods. But in Windows Phone Runtime, there is no guarantee those methods occur when the user switches between apps for example.
In Windows Phone Runtime, how to know when the user is opening a page and when going away (to another page, start screen or another app)?
When you navigate to another page - it's simple, you handle NavigatingFrom/NavigatedTo. When not - it's also simple - you shouldn't be getting these events and so you should do nothing in most cases. I've seen NavigatingFrom being raised though in certain circumstances when the app was getting suspended and navigation state was being read from the Frame even though technically in-app navigation wasn't occurring and there would be no matching NavigatedTo event on resume. You might want to filter that out by setting and clearing some flag in the Suspending event (or Application.OnSuspending override) before and after NavigatingFrom is triggered and then ignoring it in the page NavigatingFrom handler for certain code. You would probably still want to save the page state in that event, but not unsubscribe from other events in case the app gets resumed before it gets terminated.
Application.Suspending/Resuming are some events you might also be interested in for saving/loading overall app state or refreshing data, though maybe less so for subscribing/unsubscribing from events since most of these should be fine to leave alone when the app gets suspended which happens when you switch to another app.
Page.Loaded/Unloaded events are ones you would handle if you want to touch parts of the UI that need to actually be loaded in the visual tree, since NavigatedTo happens before the page is loaded.

Event when dragging over valid drop target outside of app?

I have a very simple method that writes a file locally but I only want to fire it if the user looks like they are going to drop outside of the app because firing it every time they start dragging would result in lots of unnecessary files being written.
So my question is: if a user drags something within the app outside of the app, is it possible to detect when they drag over a valid drop target (e.g. the desktop or windows explorer)?
EDIT: At a more general level, my question is: how can I respond to mouse/drag events that occur outside of my app?
Not entirely sure what it is you're exactly trying to achieve, but this may help:
WPF: Drag and drop virtual files into Windows explorer
For the most part the drag / drop events should fire regardless of where you're dropping to (I think), but you can certainly be notified when a drop has been performed.
As #Quarzy stated, unless you're in communication with the other app, there may be no direct way of testing for data that the underlying windows drag / drop system doesn't expose.
More specifically that question points to this article: http://blogs.msdn.com/b/delay/archive/2009/11/04/creating-something-from-nothing-asynchronously-developer-friendly-virtual-file-implementation-for-net-improved.aspx
I post this purely because I wonder if maybe it may lead to other things, apart from that you might be able to get the Hwnd of the control under the cursor - possibly http://social.msdn.microsoft.com/Forums/windows/en-US/3df9fc84-4020-4ae1-8b4f-942bce65568f/find-the-object-under-the-mouse?forum=winforms as a starting point.
There may then be a way to query whether that particular control is a valid drop target through interop as well.
Good luck!
This might be a possible answer for your question: Register a global hook to detect whether mouse dragging files/text
How ever the following suggestion might help (Require you to create c++ external lib):
Capture other possible processes window message (Global hook WH_GETMESSAGE) (See this link How to Create a global WH_GETMESSAGE HOOK without DLL)
Listen for WM_DROPFILES
see the following link: http://www.experts-exchange.com/Programming/Languages/CPP/Q_10203575.html
You have also an helping answer here How can I capture mouse events that occur outside of a (WPF) window?
But I do not see any way to detect if the target is valid, as long as you have no communication with the potential targets.

How to get and handle form changes in a different form that is accessible from Application.OpenForms?

I'm currently using a delegate to poll the current active control from the currently focused external form every few milliseconds, which works badly.
The title of the question is somewhat confusing, as this does not actually have anything to do with Application.OpenForms. That property just exposes a collection of all the currently open forms. If a new form is opened that had not previously been opened, it will be added to the collection. But the operating system allows only one active window at a time on the desktop, so what you really want is to get notified when the active window changes. Polling will work for this, but it isn't a good solution.
You will either need to install a global hook (WH_CBT is likely the one you want), or take advantage of the WinEvents infrastructure (via the SetWinEventHook function) intended for accessibility tools. Both of these will provide you with a notification when the active window changes. Of course, their scope won't be limited to the application that you are testing. You'll get notification when the active window on the desktop changes to any other window. It will be up to you to filter that down to the application you care about.
I don't have time at the moment to translate any of the required code to C#/.NET, but you can probably find it somewhere online, once you know what to search for. Additionally, WinEvents is already wrapped for you in the System.Windows.Automation namespace.

how to make windows startup project with many methods running at the same time in the background

I have explained my project below and asked some questions with "My Question-" tag.
I have working on a project. At the time of windows logon page if I enter a wrong password my cam should take the picture, If I open regedit my system should take a screen shot and save these images in C:\Windows\system32\new folder (I tried a lot making this work with the help of manifest files but failed everytime) and emails it whenever finds an internet connection
I have a form based app because I didn't find any other way to capture image from webcam directly but taking input from pictureBox1.Image.
My cam, screenshot,email (didn't find a way to autocheck if has internet connection available or not) and 3 events checker for "firewall enable/disable, windows logon failure , regedit event called" are done and they are working good.
What I need to do is to assemble these codes to work as an app and running in the background continuously from the time of windows startup to shutdown
To validate positive events I need to make a desktop based db ("My Question"- still figuring out either to choose sql or localdatabase in c#. Please also tell me a suitable solution.I have to delete all the entries from the db once a day is over). The db would contain the following columns (event id, event name, event timestamp).
I want my app to check if this very event exists in the db then it should ignore the event generated on windows event log else it should make a new row with the db columns and it should do the following actions based on the event like taking webcam pic or screenshot.
"My Question"- I want my app to be live at the time of windows logon page. A lot of programs start later when you are authenticated but I need my program to be live at the time of logon page. Do I have to make 1 or many services? or multi-threaded? because in the typical form based app you can only call one function at a time and wait for it to return something or perform some task/action and then you call the second third whatever.
"My Question"- Do I need to use the backgroundworker in c#
Please help!
You have a lot of things going on here for one question.
You can put all your code in a background service that gets started at boot time. There is a walkthrough here to show you how to do that (along with a million other sites).
Addressing some of the other issues you listed:
Google is your friend...
Webcam - Found a quick reference here and here
File Modification - Another SO thread here
SQL vs. Other Database - Not sure you need anything elaborate here, probably something you can put together pretty quickly. Another SO thread addressing that here
Good Luck!

Implementing a Windows script

Hi
I would like to create a script which loads a program on my windows based computer, clicks on one of its buttons, and checks the data inside it (it gets its data from the web). Should I do that in C#? Any example out there?
The program contacts the web and displays information. I would like to get notified when that data has changed.
UPDATE: I've learned that the application doesn't contact a web-service using Charles. This means I have to load the windows application, click the button and look there. How can I do such a thing? I know it is disruptive, and still I would like to do that.
You need to download Wireshark to see what http(s) the program is using.
And once you come to know about http(s) used, you can use the WebRequest and WebResponse classes for making request to the server.
The method you described in checking for changes is quite disruptive: the script you intend to write needs to load the program and clicks the button. The loading and clicking is enough to disrupt whatever you are doing when the script runs.
I suggest changing the method to access the web directly to check for changes, and only displays a notification (WPF or whatever method you are comfortable with) when the data changes.

Categories

Resources