I am making a Wp 8.1 app.
I have this "reader" page. Its constructor is -
public Reader()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Enabled;
initalizeTimers();
initAd();
}
and the onNvaigatedTo
protected override void OnNavigatedTo(NavigationEventArgs e)
{
HardwareButtons.BackPressed += Hardware_backPressedLocal;
var x = (Tuple<int, string, Global.FileType>)e.Parameter;
//load settings here
loadColorSchemes();
setStatusBarColor();
if (e.NavigationMode == NavigationMode.Back)
{
readerVm.refreshWebView();
return;
}
if (x != null)
{
initalizeReader(x);
}
}
The initalizeReader() is an async method. The page has a grid in front for showing the wait msg, and the initializeReader method hides that grid.
Problem:
On tapping a button which calls the Frame.Navigate() method to the reader page, sometimes the reader page loads with waiting grid which hides after sometime (expected) and at other times the UI freezes and then the reader page loads with waiting grid already hidden. And 70-80 % time the UI freezes.
Can you tell whats going wrong here? And how to fix it?
Try to call your method initalizeReader(x); when the event Loaded from the page raised.
Also, if this method calls some async functions, call them asynchronously also.
Related
I am learning Xamarin forms and use the plugin Rg.Plugins.Popup for popup Page.
My problem : In my PopUp page file .NET. I would like to use the function delayedWork() to close the popup automatically only when the user hasn't pressed on the background for 10 seconds.
My code does not work because the function delayedWork() is in OnAppearing() function so if I click on background before 10 seconds it creates the error : No Page in PopupStack (because the popup is already closed)
Question : How to create a listener to know when a popup is Opened or closed , so that I know if I need to use the delayedWork() function ?
Here is my code :
protected override void OnAppearing()
{
_ = delayedWork();
}
private async Task delayedWork()
{
await Task.Delay(10000);
this.doMyDelayedWork();
}
private async void doMyDelayedWork()
{
await PopupNavigation.Instance.PopAsync(true);
}
Thanks in advance
Update
The issue that you are trying to pop on an empty stack:
I used the following method in my Nativgation helper to pop popups and its never an issue :)
public static async Task PopPopupAsync()
{
if (Rg.Plugins.Popup.Services.PopupNavigation.Instance.PopupStack.Count > 0)
{
await Rg.Plugins.Popup.Services.PopupNavigation.Instance.PopAsync();
}
}
Original answer
The reason this is not working is that you are forcing it to become Synchronous what you need to do is actually await your method so something like the below:
protected async override void OnAppearing()
{
catchasTappedToClose = true;
await delayedWork();
}
I have a navigation page that sets up three pages. The first page loads, the user has to pick an option from a listview and then it loads the second page with PushAsync(). At this point the user can now navigate between the three pages by turning the clock face. If I call PopToRootAsync() on the second page it works fine. If the rotary clock face is turned clockwise it loads a third page via PushAsync().
The problem is if I call PopAsync() on that third page OR I change the PopToRootAsync() on the second page to a PopAsync(), the app crashes. I have no way to determine what the error is either as I just get segmentation fault and nothing is written to the Tizen log that is seemingly indicative of why it crashed.
Is there some reason that a PopAsync() would cause this? I know I saw some other articles this could occur if the MainPage is not loaded into a NavigationPage but I'm doing that. I've been looking through stuff and writing debug logs for days but have nothing to show for it. Any help would be more than appreciated. Thank you!
App.cs
public App()
{
MainPage = new NavigationPage(new ServerSelectionPage());
}
ServerSelection.cs
private void ServerSelection_OnItemTapped(object sender, ItemTappedEventArgs args)
{
App.SERVER = (Server)args.Item;
Navigation.PushAsync(new ArrowsPage());
}
PageBase.cs
public async void Rotate(RotaryEventArgs args)
{
Page _currentPage = Page.REMOTE_BUTTONS;
if (this.GetType() == typeof(ButtonsPage))
_currentPage = Page.REMOTE_BUTTONS;
else if (this.GetType() == typeof(ArrowsPage))
_currentPage = Page.REMOTE_ARROWS;
else
_currentPage = Page.SERVER_SELECTION;
// When rotating (previous rotation is ongoing, do nothing)
if (_rotating)
{
return;
}
_rotating = true;
if (!(App.SERVER is null))
{
if (_currentPage == Page.SERVER_SELECTION)
{
if (args.IsClockwise)
await Navigation.PushAsync(new ArrowsPage());
}
else if (_currentPage == Page.REMOTE_DIRECTIONAL)
{
if (args.IsClockwise)
await Navigation.PushAsync(new ButtonsPage());
else
await Navigation.PopToRootAsync();
}
else
{
try
{
if (!args.IsClockwise)
await Navigation.PopAsync(); // This will crash the app
}
catch(Exception ex)
{
Log.Debug(ex.Message);
}
}
}
_rotating = false;
}
After reading the comment by #vin about checking if the Navigation object is null I suddenly had the thought that it may not be the Navigation page but the RotaryFocusObject. So I changed
if (!args.IsClockwise)
await Navigation.PopAsync();
to
if (!args.IsClockwise)
{
RotaryFocusObject = null;
await Task.Delay(300);
await Navigation.PopAsync();
}
and it no longer crashes. The delay is necessary as if you call PopAsync right after setting the object to null it can still sometimes crash.
So apparently if you pop the current page it causes an error as the focus of the rotary dial is still set to the current navigation page. Why this error does not seemingly occur if you use the Navigation.PopToRootAsync() makes no sense to me.
Navigation.PopToRootAsync() and Navigation.PopToRootAsync() are causing destroy of Page renderer, and the Rotate handler is belong to Page Renderer,
If Page renderer was deleted before completion of Rotate Native Callback that is belong to Page renderer, it will be crashed.
await Task.Delay() let to returning native callback.
I was reading trough the documentation for UWP and I got stuck a little.
I have few pages that connect to WCF service taking some information of it few of them download pictures and take few seconds to load.
So I've decided to implement a loading screen while they load however when I try using
this.Frame.Navigate(typeof(page));
I get stuck in a deadlock state everything freezes while the new page is loading I've tried putting on pageloading event on the other page but this is not helping much since its still locked on the last form.
Does anyone know the right event that I need to call when calling this.Frame.Navigate() so I can initialize my loading control while the new frame is loaded?
Navigate to Loading Screen
this.Frame.Navigate(typeof(LoadingScreen));
In OnNavigatedTo event in LoadingScreen "download pictures"
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
await DownloadPictures();
//After downloading, navigate to the next page
this.Frame.Navigate(typeof(page));
}
try launching the view on a separate window like this
try
{
CoreApplicationView Nv= CoreApplication.CreateNewView();
var z = CoreApplication.MainView;
int id= 0;
await
Nv.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
Frame frame = new Frame();
frame.Navigate(typeof(page));
Window.Current.Content = frame;
// You have to activate the window in order to show it later.
Window.Current.Activate();
id= ApplicationView.GetForCurrentView().Id;
});
bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(id);
}
catch (Exception eee)
{
Windows.UI.Popups.MessageDialog errorBox =
new Windows.UI.Popups.MessageDialog("Couldn't Create New
Window: " + eee.Message);
await errorBox.ShowAsync();
}
This question is specific to Windows Phone 8.1 (WinRT); it may also be applicable Windows 8.1. I am using Caliburn.Micro 2.0.1
In my ViewModel's OnActivate I check whether an item is a database, if it isn't, I want to navigate back to the previous page.
The simplist solution will be just to call GoBack in the OnActivate method (this works in Windows Phone 8.0):
INavigationService _navigationService;
protected override void OnActivate()
{
_item = GetItemFromDB();
if(_item == null)
{
_navigationService.GoBack()
}
}
To navigate to the view model I call:
_navigationService.NavigateToViewModel<MyViewModel>(_param);
But it does not work, it ignores the GoBack call and stays on the page which I do not want to view.
When stepping through the code you can see that the GoBack code is called inside the NavigateToViewModel method; I expect this is the reason why it does not work (something to do with a queuing issue maybe?).
I have a very "hacky" solution that involves a timer (that works), but I really despise it since it is prone to threading issues and has the possibility of being called during the NavigateToViewModel call (if it takes long to finish), which will then again not work:
protected override void OnActivate()
{
_item = GetItemFromDB();
if(_item == null)
{
DispatcherTimer navigateBackTimer = new DispatcherTimer();
navigateBackTimer.Interval = TimeSpan.FromMilliseconds(300);
navigateBackTimer.Tick += GoBackAfterNavigation;
navigateBackTimer.Start();
}
}
public void GoBackAfterNavigation(object sender, object e)
{
_navigationService.GoBack();
(sender as DispatcherTimer).Stop();
}
Is there a better way to navigate back? Why doesn't the GoBack work in OnActivate? Is there a way to get it to work in OnActivate?
You can use
Execute.OnUIThreadAsync(() => /* navigationCode */);
instead of a timer to queue the action immediately after the processing of the current stack has finished.
I want to make a simple app that will allow me to check few parameters of every frame of preview, but I got stuck at running and stopping preview.
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
MediaCapture _MediaCapture;
bool _recording;
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
var rearCamera = devices[0];
if (devices.Count > 0)
{
rearCamera = devices.Single(currDev =>
currDev.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
);
}
_MediaCapture = new MediaCapture();
await _MediaCapture.InitializeAsync(new MediaCaptureInitializationSettings() { VideoDeviceId = rearCamera.Id });
// this is CaptureElement
xCapture.Source = _MediaCapture;
_recording = false;
}
protected override async void OnNavigatedFrom(NavigationEventArgs e)
{
if(_MediaCapture != null)
{
await _MediaCapture.StopPreviewAsync();
await _MediaCapture.StopRecordAsync();
_MediaCapture.Dispose();
_MediaCapture = null;
xCapture.Source = null;
}
base.OnNavigatedFrom(e);
}
// button click handler
private async void StartMeasure(object sender, RoutedEventArgs e)
{
if (_recording)
{
//await _MediaCapture.StopPreviewAsync();
_MediaCapture.VideoDeviceController.TorchControl.Enabled = false;
_recording = false;
}
else
{
//await _MediaCapture.StartPreviewAsync();
_MediaCapture.VideoDeviceController.TorchControl.Enabled = true;
_recording = true;
}
}
}
In this form it works perfectly.
If I uncomment those preview lines it works, but only once.
If I press the button three times: on, off and on again I get exception at line with enabling TorchControl.
System.Exception: Exception from HRESULT: 0xE801000D at Windows.Media.Devices.TorchControl.put_Enabled(Boolean value) at Pulsometr3.MainPage.d__d.MoveNext()
The HRESULT varies.
Whats even more weird, it sometimes freezes the phone (like 2 out of 3 times) and I need to hold Power + Volume Down.
I tried decorating all methods with [STAThread], but it didn't help (http://technet.microsoft.com/en-ca/br226599).
What's even more more interesting, when I hold operations by debbuger using F10 to step over lines I am able to toggle preview as many times as I possibly want. It's werid, since debugger hold all threads, right? So in theory there is no difference?
Also, phone sometimes freezes on deploy... And that's just annoying.
Any ideas?
I've got exactly into this...for some reason microsoft does not care much for it's successor OS to WP8, which makes me really sad. But it was also a half year ago during summer, I've tried this, maybe you can give a shot to googling on application consents and also double check your app manifests, if you have front/rear camera and webcam ticked in :) Besides that if it won't work, then bad luck, you are ought to stick with wp 8.0 version, which works exactly the same on wp 8.1 so do not worry :) also other libs like facebook stuff or parse.com won't work on wp 8.1 C# :)
I think your problem is the page cache enabled. Try to remove this line in your code this.NavigationCacheMode = NavigationCacheMode.Required;
if I understand correctly the button has a handler StartMeasure which is an async method and awaits for Start/StopPreviewAsync().
The problem might be that if you click the button more than once the one action might be still awaited(in progress) and the other one is also called, this might cause some issues because it will try to start and stop the preview at the same time which will probably lead to some race conditions.
You could check this by adding a lock to manage the access to the capture manager in order to test this. Also checking the bool and assigning it after an awaited operation is for sure not an atomic operation so that could lead to race conditions too.
private object locker;
private async void StartMeasure(object sender, RoutedEventArgs e)
{
lock (locker)
{
if (_recording)
{
await _MediaCapture.StopPreviewAsync();
}
else
{
await _MediaCapture.StartPreviewAsync();
}
_recording = !_recording;
_MediaCapture.VideoDeviceController.TorchControl.Enabled = _recording;
}
}