I'm using Zxing barcode scanner and Rg Pop up pages in Xamarin forms. The whole idea is that when the camera catches a barcode it displays a pop up. The problem is that I want to wait for the user to close the pop up before it scans again. Now if I keep the camera towards a barcode it keeps showing multiple alerts.
public async void Scan(Result result)
{
//random linq to get product
if (product != null)
{
await PopupNavigation.PushAsync(new DisplayDialog(product));
}
}
Method for when user closes the window
private async void Button_OnClicked(object sender, EventArgs e)
{
await PopupNavigation.RemovePageAsync(this);
}
I tried using a flag to stop the scan and setting isScanning or isAnalyzing to false but it didnt work. Any ideas? Thank you!
use a bool to control the flow
bool popup = false;
public async void Scan(Result result)
{
if (popup) return;
//random linq to get product
if (product != null)
{
popup = true;
await PopupNavigation.PushAsync(new DisplayDialog(product));
}
}
then whenever your popup is dismissed, reset the popup flag to false
Related
I have a Xamarin.Forms app and I am using FreshMvvm framework.
If I do this from ViewIsAppearing method of FirstPageModel:
CoreMethods.PushPageModel<SecondPageModel>();
I go the "SecondPageModel". Then, when I am in the "SecondPageModel" if I do:
CoreMethods.PopPageModel();
or press hard back button, or press title bar back button not works in Android (anything happens). I am using FreshMasterDetailNavigationContainer.
In iOS it works OK, I get back to FirstPageModel.
This is because ViewIsAppearing will always be called when the page starts displaying on the screen. When you pop the second page then go to the first page, the first page's ViewIsAppearing will fire again. It caused a dead cycle and prohibited your app from returning to the first page.
Add a property to avoid that:
bool isInitialized;
public FirstPageModel()
{
// ...
isInitialized = true;
}
protected async override void ViewIsAppearing(object sender, EventArgs e)
{
base.ViewIsAppearing(sender, e);
if (isInitialized)
{
await Task.Delay(100);
await CoreMethods.PushPageModel<SecondPageModel>();
isInitialized = false;
}
}
iOS may optimize this process, but I still recommend you to add this judgment statement.
Update:
Call it when your app has reached the main thread.
protected override void ViewIsAppearing(object sender, EventArgs e)
{
base.ViewIsAppearing(sender, e);
if (isInitialized)
{
Device.BeginInvokeOnMainThread(() =>
{
CoreMethods.PushPageModel<SecondPageModel>();
isInitialized = false;
});
}
}
I want the user to be able to go into settings of my app and select from the images provided or select a photo from their internal or external storage to be applied to the background of the MainActivity. Also I want this image to stay even after the app has been killed until the user decides to change the background again. I have tried multiple codes and I run into errors every time and the main problem I'm having is sending this intent from one Activity to another. I have used code that allow me to apply an image to an ImageView within the same Activity but I cannot take that code and get it to send across to another Activity.
Targets:
LinearLayout - backgroundtop (MainActivity)
Imageview(onClick) - gallery (DxsettingsActivity)
Imageview(onClick) - dx (DxsettingsActivity)
"Gallery" and "dx" are in a customview dialog.
When "gallery" is clicked internal/external storage is opened to select background resource of "backgroundtop" and save that image until user changes it again.
When "dx" is clicked it opens DxWallpaperActivity which will have the images to be selected and saved as the background of "backgroundtop".
I hope I have explain well enough. I'll provide more info if needed. Thank you in advance. I have been struggling with this for days to no avail.
Update: I have managed to accomplish setting and saving images from in-app images to the background. I have tried everything under the sun to pull image from gallery and do the same. I have managed to open gallery and select an image but I can't figure out onActivityResult then send that to the background. Please help.
This is the only code I've got to work but it doesn't have shared preferences and it for an imageview. How can I change it for backgroundresource on linearlayout?
}
private static final String STATE_IMAGE_URI =
"STATE_IMAGE_URI";
private Uri imageUri;
public void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
if (imageUri != null ) {
state.putParcelable(STATE_IMAGE_URI, imageUri);
}
}
public void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
if(state == null || !state.containsKey(STATE_IMAGE_URI))
return;
setImage((Uri) state.getParcelable(STATE_IMAGE_URI));
}
private static final int IMAGE_REQUEST_CODE = 9;
private void chooseImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select picture"), IMAGE_REQUEST_CODE);
}
public void onActivityResult(int requestCode, int.
resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode != IMAGE_REQUEST_CODE) {
return;
}
if (resultCode != Activity.RESULT_OK) {
return;
}
setImage(data.getData());
}
private void setImage(Uri uri) {
imageUri = uri;
imageview1.setImageURI(uri);
}
private void nothing() {
And for the button clicked
chooseImage();
Hey Everyone I am using Autofac(MVVM) my problem is how to redirect to another page from DisplayAlert Popup by clicking ok button in popup screen? As the display alert is in my Views and in my ViewModels the ICommand navigate to another page is navigator.
PushAsync<PageViewModel>();
Some of my Code snip:
ViewModels
public class HomePageViewModel : ViewModelBase
{
private readonly INavigator _navigator;
public HomePageViewModel(INavigator navigator)
{
_navigator = navigator;
CmdInvites = new Command(Cmdinvites);
}
public ICommand CmdInvites { get; private set; }
//Invite PhoneBook
private void Cmdinvites()
{
_navigator.PushAsync<PhoneContactViewModel>();
}
}
zxing.OnScanResult += (result) =>
Device.BeginInvokeOnMainThread(async () => {
// Stop analysis until we navigate away so we don't keep reading barcodes
zxing.IsAnalyzing = false;
// Show an alert
await DisplayAlert("Scanned Barcode", result.Text, "OK");
// Navigate away
await Navigation.PopAsync();
<-------------this is where i want to redirect the {Binding CmdInvites}
});
Thank you for the time reading my post.
You can use another DisplayAlert method constructor which will be like this:
Device.BeginInvokeOnMainThread(async () => {
// Stop analysis until we navigate away so we don't keep reading barcodes
zxing.IsAnalyzing = false;
// Show an alert
if(await DisplayAlert("Scanned Barcode", result.Text, "Accept","Cancel"))
{
// Navigate away
await Navigation.PopAsync();
<-------------this is where i want to redirect the {Binding CmdInvites}
}
});
When the user clicks on back button, I've implemented the following code that works very well for links inside of my webview:
#Override
public void onBackPressed() {
if (this.webViewFragment != null && this.webViewFragment.canGoBack()) {
this.webViewFragment.goBack();
} else {
super.onBackPressed();
}
}
My problem is that I have native menu in Android, and when the user clicks on the menu Profile for example and then clicks on the menu Dashboard, the back button doesn't work. Nothing happens. As I said before, just works for links clicked inside of the webview.
Anyone knows a solution for that?
or try this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
I'm trying to create a loading screen (in this case a PopUp Panel) everytime I click on a button and to hide the screen when the processing is done.
But the problem is that the loading screen never appears.
If I delete the line that hides the loading screen:
//HIDES THE LOADING POPUP
closeLoadingFilter();
The loading screen appears but only until everything finished processing. (not inmediately after I click the button).
I think all the processing in the button handler must be completed before anything appears on the screen. So when the processing is complete the popup is hidden before it was ever showed on the screen.
How can I solve this problem. By the way there is no Async call in the processing, everything is in-memory processing and the drawing of google visualizations with the in-memory information
Thank you all
Here is the code:
buttonFilter.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
//SHOWS THE LOADING POPUP
showLoadingFilter();
//ALL THIS TAKES AROUND 4 SECONDS
//THERE IS NO ASYNC CALL
//EVERYTHING IS IN-MEMORY PROCESSING
//AND DRAWING OF GOOGLE VISUALIZATION
listValues.clear();
listFields.clear();
CreateListFiltersSingle();
GetFilterSingleOptions();
RunFilter(dashboardProductos);
DrawVisualizations2();
//HIDES THE LOADING POPUP
closeLoadingFilter();
}
})
public void showLoadingFilter() {
popupFilterLoading.clear();
popupFilterLoading.add(new Label("Please wait"));
popupFilterLoading.setGlassEnabled(true);
popupFilterLoading.center();
isFilterLoading = true;
popupFilterLoading.show();
}
public void closeLoadingFilter() {
if (isFilterLoading) {
popupFilterLoading.hide();
isFilterLoading = false;
}
}
You can call methods asynchronously using a scheduler. The code below might solve your problem.
submit.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
showLoadingFilter();
// Waits for the loading pop-up to be displayed before starting anything
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
#Override
public void execute() {
listValues.clear();
listFields.clear();
CreateListFiltersSingle();
GetFilterSingleOptions();
RunFilter(dashboardProductos);
DrawVisualizations2();
closeLoadingFilter();
}
});
}