Xamarin white screen bug in QR Scanner - c#

When I open the application it works perfect. But when I use my scan button again it's stuck on a white screen and not passing until I press the "Back" Button on my phone.
How can I remove that white screen bug?
Video about my bug: https://www.youtube.com/watch?v=k8-haOmCzm0
QR Code Scanner Code:
public async void Scan()
{
var scan = new ZXingScannerPage();
await Navigation.PushAsync(scan);
scan.OnScanResult += (result) =>
{
Device.BeginInvokeOnMainThread( () =>
{
Navigation.PopAsync();
codes.Text = result.Text;
Application.Current.Properties["codes"] = codes.Text;
Navigation.PushAsync(new MainPage());
});
};
}
My Scan again button Code:
private void Button_Clicked_1(object sender, EventArgs e)
{
Navigation.PushAsync(new ScanPage());
}

From shared code, you have pushed twice to the scan page, however you only use one time PopAsync after get result.
But also used the third push in Device.BeginInvokeOnMainThread:
Navigation.PushAsync(new MainPage());
You could modiy this line with following code to check whether it works.
Device.BeginInvokeOnMainThread( () =>
{
Navigation.PopAsync();
codes.Text = result.Text;
Application.Current.Properties["codes"] = codes.Text;
Navigation.PopAsync();
});
============================Update================================
If you scan code when lauching application, when receiving results, you could use Application.Current.MainPage = new MainPage(); to back to Root Page of Applicatuon.
Code as follows:
Device.BeginInvokeOnMainThread( () =>
{
Navigation.PopAsync();
codes.Text = result.Text;
Application.Current.Properties["codes"] = codes.Text;
Application.Current.MainPage = new MainPage();
});

Related

Xamarin.Forms Error when navigate to another page in button_clicked

In my Xamarin.Forms project I have a SplashScreen for Android and an AppDelegate for IOS that check some conditions and then pass some data to App.xaml.cs . Then :
public App(string isLogin)
{
InitializeComponent();
if (isLogin == "Nologin")
App.Current.MainPage = new LoginPage();
else
{
switch (isLogin)
{
case "Update":
App.Current.MainPage = new MessagePage("Some Message");
break;
case "Internet":
App.Current.MainPage = new MessagePage("Some Message");
break;
default:
App.Current.MainPage = new MainPage(isLogin);
break;
}
}
}
Now in the MessagePage I have this :
private async void RefreshButton_Clicked(object sender, EventArgs e)
{
string result = await AppStart.DoStartAsync().ConfigureAwait(false);
if (result == "Login")
await Navigation.PushAsync(new LoginPage());
}
that show this error :
System.InvalidOperationException: 'PushAsync is not supported globally on Android, please use a NavigationPage.'
and if I use this :
if (result == "Login")
Application.Current.MainPage = new NavigationPage(new LoginPage());
Show this error :
Android.Util.AndroidRuntimeException: 'Only the original thread that created a view hierarchy can touch its views.'
What can I do ?
Based on your error message.
Android.Util.AndroidRuntimeException: 'Only the original thread that created a view hierarchy can touch its views.
This issue is related to the you update the view not in the UI thread(Main thread), you should wrap you update view code by Device.BeginInvokeOnMainThread(Action) to make sure any UI updates are occurring on the correct thread.
Device.BeginInvokeOnMainThread(
async () => {
await Navigation.PushAsync(new LoginPage());
});

How can I click button on popup? (pupeteer-sharp)

When I click to button, page of pop-up is opening. I have to click button on it. How can I do it?
await page.GoToAsync("https://.....");```
await page.WaitForTimeoutAsync(7000 * 2);```
await page.WaitForSelectorAsync("a[class='visit_button']");```
await page.ClickAsync("a[class='visit_button']"); //open popup```
await page.WaitForTimeoutAsync(3000);```
// I click to button on pop-up```
This works for me:
var alertMessage = "";
//attach to page during entire page life-cycle (until closed).
//handles the case where an javscript alert comes up during login.
page.Dialog += new EventHandler<DialogEventArgs>(async (sender, args) =>
{
alertMessage = args.Dialog.Message;
await args.Dialog.Accept(); //this closes it..
Log.Information("Popup squashed in Login(): {0}", alertMessage);
Thread.Sleep(500);
});
According to PageEventsPopupTests :
await page.GoToAsync("https://.....");
await page.ClickAsync("a[class='visit_button']"); //Open Popup
var popupTaskSource = new TaskCompletionSource<Page>();
page.Popup += (_, e) => popupTaskSource.TrySetResult(e.PopupPage);
await popupTaskSource.Task;
var popupPage = popupTaskSource.Task.Result; // Popup Page
await popupPage.ClickAsync("a[class='btn']"); //Click on button in popup page

How to close secondary view in uwp

I'm building an UWP app that spawn second view besides the main view.
I want to close the second view from the main view.
I already try to google it, but most of it only show me how to spawn but not to close.
Below is the code block I use to open a new view, and at its bottom is how I try to close the newly created view that always ends up fail.
public async void OpenNew(Type type, string title, string uniqueId)
{
CoreApplicationView newView = null;
if (!IsExists(uniqueId))
{
newView = CoreApplication.CreateNewView();
int newViewId = 0;
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Frame frame = new Frame();
Window.Current.Content = frame;
Window.Current.Activate();
frame.Navigate(type);
newViewId = ApplicationView.GetForCurrentView().Id;
ApplicationView.GetForCurrentView().Title = title;
ApplicationView.GetForCurrentView().Consolidated += OnConsolidated;
});
await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
_uniqueIds.Add(uniqueId, newViewId);
}
// Below code testing on how I try to close newly spawned view but always throw error
await Task.Delay(5000).ContinueWith(async _ =>
{
await Window.Current.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
newView.CoreWindow.Close();
});
});
}
This code always throws
Exception thrown: 'System.NullReferenceException'
Any link or tutorial on how to do it?
Nvm, guys I got the answer. Below is my changes on the part where I try to close the 2nd view from the above code:
await Task.Delay(5000).ContinueWith(async _ =>
{
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
CoreWindow.GetForCurrentThread().Close();
});
});

EvaluateScriptAsync waiting in Cefsharp

I am having a problem about using EvaluateScriptAsync in Cefsharp. I want to run a javascript code with EvaluateScriptAsync and then, after this is completed I want my code continue to work, but ContinueWith isn't waiting to EvaluateScriptAsync complete. Here is my code, and don't know what's the problem:
private void WebBrowserFrameLoadEnded(object sender, FrameLoadEndEventArgs e)
{
if (e.Frame.IsMain)
{
var task = browser.EvaluateScriptAsync("(function() { function xx() { $('body').animate({scrollTop: $('body').prop(\"scrollHeight\")}, 8000, function() { var win = $(window); if ($(document).height() - win.height() == win.scrollTop()) { return true; } else { xx(); } }); }; xx(); })();");
task.ContinueWith(taskHtml =>
{
if (taskHtml.IsCompleted)
{
/* Some code working */
}
this.DialogResult = DialogResult.OK;
});
}
}
You Should use the ExecuteSciptAsyc() method of the Browser Tab Control.
1st you have to get the Current tab page Control and then call the ExecuteSciptAsyc() method from browser control.
var control = GetCurrentTabControl();
control.Browser.ExecuteScriptAsync("alert('It's Working')");

SystemMediaTransportControls Not Working

I'm having issues with the new SystemMediaTransportControls that replace MediaControl.
Currently, I have my app set up with:-
systemControls = SystemMediaTransportControls.GetForCurrentView();
systemControls.IsPlayEnabled = true;
systemControls.IsStopEnabled = true;
systemControls.IsPauseEnabled = true;
systemControls.ButtonPressed += SystemControls_ButtonPressed;
And
async void SystemControls_ButtonPressed(SystemMediaTransportControls sender, SystemMediaTransportControlsButtonPressedEventArgs args)
{
System.Diagnostics.Debug.WriteLine(args.Button);
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
switch (args.Button)
{
case SystemMediaTransportControlsButton.Play:
if (mediaElement1.CurrentState != MediaElementState.Playing)
{
restartSource();
}
else
{
completeClosure();
}
break;
case SystemMediaTransportControlsButton.Pause:
case SystemMediaTransportControlsButton.Stop:
completeClosure();
break;
default:
break;
}
});
}
And:
private async void completeClosure()
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
mediaElement1.Stop();
mediaElement1.Source = null;
timer.Stop();
});
}
private async void restartSource()
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
mediaElement1.Source = new Uri(holdThisSource, UriKind.Absolute);
mediaElement1.Play();
timer.Start();
});
}
When a user presses the Pause Button, args.Button shows up as "Play", hence the need for the checking for MediaElement's state. However, when I attempt to resume to media, it successfully resumes in restartSource() and updates the app accordingly but the icon on the Volume Control does not change from the Play sign, although hardware buttons still work.
Along with this, pressing the hardware Stop button NEVER works, and fails to even show up in Debug.WriteLine.
This is an online streaming app where the source does not allow resuming and thus I have to close the stream this way.
I'd love some help on this.
Since you did not update the systemControls.PlaybackStatus, the control button on transport control will not auto change to correct status.
You should always update the systemControls.PlaybackStatus property when the playback state has changed.
May this could solve your problems.

Categories