I used this method (from msdn) to extend splashscreen in my app but while navigating after showing extended splash screen it is throwing error.
Error:
The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
Code:
void DismissedEventHandler(SplashScreen sender, object e)
{
dismissed = true;
/*
Here I will load data from web
*/
//Here i am unable to navigate on MainPage
rootFrame.Navigate(typeof(MainPage), LoadedData);
Window.Current.Content = rootFrame;
}
If you need more code then its same as of above mention link.
So how can navigate to MainPage?
You can dispatch the navigation to run on the UI thread with Dispatcher.RunAsync https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.core.coredispatcher.runasync.aspx
Related
I am expermenting with video recording on Windows Phone 8. I want to handle the situation when user is putting my app to background, while it is recording a video. I would like to save the already recorded video before quitting.
I am handling this situation using the code from this example:
https://msdn.microsoft.com/en-us/library/windows/apps/mt243896.aspx
private async Task StopRecordingAsync()
{
try
{
Debug.WriteLine("Stopping recording...");
_isRecording = false;
await _mediaCapture.StopRecordAsync();
Debug.WriteLine("Stopped recording!");
}
catch (Exception ex)
{
Debug.WriteLine("Exception when stopping video recording: {0}", ex.ToString());
}
}
I am calling this method from the:
protected async override void OnNavigatingFrom(NavigatingCancelEventArgs e)
But the video is not being saved. In the debug console I get only the first message: "Stopping recording...", but there is no "Stopped recording!" message logged. It seems like the resources are being destroyed before I can handle them.
When your app is moved to the background you only have a short amount of time to run code.
Instead of having saving be triggered when you navigate from the page, instead look at the Application.Suspending event which allows you to use a deferral to try and run your code for a bit longer so you can finish tidying up before your app loses it's resource allocation.
Something like:
async protected void OnSuspending(object sender, SuspendingEventArgs args)
{
SuspendingDeferral deferral = args.SuspendingOperation.GetDeferral();
await StopRecordingAsync();
deferral.Complete();
}
I have the following page navigation in my app:
AnyPage -> Login -> Register
When the user gets registered he is automatically logged to. So I want the Login page to be closed automatically if the user go back to it and is logged.
I tried to add some code to the LoginPage.onNavigatedTo method but it doesn't work.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.navigationHelper.OnNavigatedTo(e);
if(AccountController.isLogged()){
Frame.GoBack();
}
}
How can I do it?
The OnNavigatedTo and OnNavigatedFrom methods are part of the ongoing navigation operation. When you try to start a new navigation while there is one in progress, navigation methods will stop and return false.
Simple trick is: Make your OnNavigatedTo async and add a delay before navigating back.
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
this.navigationHelper.OnNavigatedTo(e);
if(AccountController.isLogged()){
await Task.Delay(1);
Frame.GoBack();
}
}
The reason this works is: Navigation is handled on the UI Thread. As the Event Methods are void returning, they are not awaited and if you return a task inside of them, the current caller finishes his current task. Whatever comes after the await is queued to the Dispatcher and finished once he has time for it (which is immediately after the navigation operation finishes).
I need to develop an app to make a call from the Windows Phone 8 app using Visual Studio.
But I couldn't find any resources to do it.
When a button is clicked I need to call to a mobile number which is already given.
By clicking that button I must call only to that mobile number.
This is what I coded. When a given button is clicked, I this method is calling...
private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
{
PhoneCallTask phoneCallTask = new PhoneCallTask();
phoneCallTask.PhoneNumber = "0719957868";
phoneCallTask.DisplayName = "Gage";
phoneCallTask.Show();
}
But I get an unhandled exception.
Unhandled exception.
// Code to execute on Unhandled Exceptions
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if (Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
Debugger.Break();
}
}
When you use PhoneCallTask, you have to specify a new Capability of your app in WMAppManifest.xaml: ID_CAP_PHONEDIALER
source
This is how you should do it:
PhoneCallTask phoneCallTask = new PhoneCallTask();
phoneCallTask.PhoneNumber = "2065550123";
phoneCallTask.DisplayName = "Gage";
phoneCallTask.Show();
Remember that the call is not automatically started but it prompts the user to confirm that action.
Here is how to initiate a call on windows phone
http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh394025(v=vs.105).aspx
Ok for future reference i must add this.
If you get an Unauthorized Access Exception then you need to enable ID_CAP_PHONEDAILER from the Capabilities section in the WMAppManifest.xml file.
See here
I have the simplest possible app. The UI is a page with nothing on it but a Tap method on a grid.
The code-behind looks like this...
public partial class MainPage : PhoneApplicationPage
{
private PhotoChooserTask _photoChooser;
// Constructor
public MainPage()
{
InitializeComponent();
_photoChooser.Completed += OnPhotoChosen;
}
private void OnTap(object sender, System.Windows.Input.GestureEventArgs e)
{
_photoChooser.Show();
}
private void OnPhotoChosen(object sender, PhotoResult result)
{
}
}
Now, what happens is that when I debug this application ON THE DEVICE, it briefly shows the photo chooser but then immediately deactivates...I assume because the photo chooser has taken focus. But from everything I've read, this should NOT be happening because the PhotoChooserTask's Completed event has been wired up in the constructor for my page, which should explicitly prevent my app from deactivating when the photochooser is active.
What's even more confusing is that the app seems to work when I'm NOT debugging it. Once I've selected a photo in this scenario, my app regains the foreground.
Is this a bug with the debugger or something else?
So it turns out the solution is to debug using "Start New Instance" in Visual Studio rather than just hitting F5.
I have a client-server application and i am using swing in the client side. My swing client has one main window (jframe) and lots of panels, toolbars and menubar in it.
I want to remove all client action/mouse events (or simply grab and do nothing) while client is waiting response from server by means of glasssPane.
Here is the code i wrote:
private final static MouseAdapter mouseAdapter = new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
System.out.println("MouseClicked..!");
}
};
private static Cursor WAIT_CURSOR = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
private static Cursor DEFAULT_CURSOR = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
and
public static void startWaitCursor(JComponent comp)
{
MainWindow root = ((MainWindow) comp.getTopLevelAncestor());
root.getGlassPane().setCursor(WAIT_CURSOR);
root.getGlassPane().addMouseListener(mouseAdapter);
root.getGlassPane().setVisible(true);
}
public static void stopWaitCursor(JComponent comp)
{
MainWindow root = ((MainWindow) comp.getTopLevelAncestor());
root.getGlassPane().setCursor(DEFAULT_CURSOR);
root.getGlassPane().setVisible(false);
}
but i am not able to manage the grab mouse events. Changing cursors at the glassPane is working fine but either i am not able to add mouseAdapter or am not able to make glasssPane become to the top level component.
Any idea?
Thanks.
I realized that my code is working but my problem is threading related. My code was something like:
startWaitCursor();
work(); // server request that takes time
stopWaitCursor();
and changed it to:
startWaitCursor();
SwingUtilities.invokeLater(new Runnable() {
poblic void run() {
try
{
work(); // server request
}
finally
{
stopWaitCursor();
}
by doing this modification i could see the settings i made in the startWaitCursor() method while client is waiting response from the server.
But stil there is a small problem. In startWaitCursor() method i desabled key, mouse and focus events for the glass pane but events are still captured by main frame even glassPane is displayed.
addMouseListener(new MouseAdapter() {});
addMouseMotionListener(new MouseMotionAdapter() {});
addKeyListener(this);
setFocusTraversalKeysEnabled(false);
After server response reached to client and stopWaitCursor() method is invoked the events handled in the main frame.
If i disable the main frame of my application while client is waiting than cursor is not being changed to wait_cursor, if i am not disable the main frame then cursor is being changed but the events are queued.
cheers...
After digging swing threads issues couple of days, i finally found the real answer: SwingWorker
Now my final code is something like,
startWaitCursor();
SwingWorker worker = new SwingWorker() {
public Object doInBackground()
{
doWork(); // time consuming server request
return null;
}
public void done()
{
stopWaitCursor();
}
};
worker.execute();
In startWaitCursor() method i set the glasspane visible (with alpha valued background), display a message to warn the user time consuming job is doing, set the cursor to wait_cursor (hourglass) and consume all the key, mouse events. That is it.
And by using SwingWorker my client is actually responsive (it is working as if no server request is made) but since i display the glasspane and consume all key and mouse events it feels like irresponsive.
What a relief.. SwingWorker rocks...
cheers..