I am using the following alert in the mvvmcross, I wonder how could I able to add a cancel button.
var alertConfig = new AlertConfig {
Message = "it is not valid",
OkText = "Okely",
OnOk = () => { Debug.WriteLine("ok pressed"); }
};
Mvx.Resolve<IUserDialogs>().Alert(alertConfig);
You probably want to use Confirm instead. Alert, as the name implies, is to Alert the user to something, and the only action is to dismiss the Alert.
Related
I want when a user clicks an inline button, a message sent to another user!
Here is my code:
//creating inline keyboard
InlineKeyboardButton accept= new InlineKeyboardButton();
InlineKeyboardButton reject = new InlineKeyboardButton();
InlineKeyboardButton[] buttons = new InlineKeyboardButton[]
{
accept, reject
};
InlineKeyboardMarkup inline = new InlineKeyboardMarkup(buttons);
//giving inline buttons text and callback data
accpet.Text = "accept";
reject.Text = "reject";
accept.CallbackData = "accept";
reject.CallbackData = "reject";
//instantiation "CallbackQuery" class
CallbackQuery callbackQuery = new CallbackQuery();
//send a text message to someone else if "callbackQuery.Data" was same a "accept" button callback data.
//This Part Doesn't Works. When I click accept button it does nothing!
if (callbackQuery.Data == "accept")
{
await botClient.SendTextMessageAsync(
chatid,
"Hello World."
);
}
Thanks A lot For Your Helps :)
You're doing wrong, You should wait until receiving CallbackQuery update, Not creating new CallbackQuery(); And then try processing.
Telegram has something called Updates, Which means events when new message sent Or edited, Button pressed, User joined, etc.
So, You should create a OnCallbackQuery event, To handle any callback button pressed like this:
First, Creating the handler method:
botClient.OnCallbackQuery += botClient_OnCallbackQuery;
private void botClient_OnCallbackQuery(object sender, CallbackQueryEventArgs e)
{
// Send the message to any one you want
ChatId chatId = /*Put any chat ID you want*/;
await botClient.SendTextMessageAsync(chatId, "Hello World.");
}
Second, You should handle any message via OnMessage event like this:
botClient.OnMessage += botClient_OnMessage;
private async void botClient_OnMessage(object sender, MessageEventArgs e)
{
var message = e.Message;
if (message.text == '/start')
{
var accept = InlineKeyboardButton.WithCallbackData("Accept", "accept");
var reject = InlineKeyboardButton.WithCallbackData("Reject", "reject");
await botClient.SendTextMessageAsync(message.Chat.Id, "Accept Or Reject..", replyMarkup: new InlineKeyboardMarkup([accept, reject]));
}
}
how to disable user from exit page?
here await doesnt work. it just goes back to previous page and than display message
protected async override void OnDisappearing()
{
var answer = await DisplayAlert("Changes un-saved",
"You must tap on 'save' button", "Exit", "Stay on this page");
if (!answer || String.Compare(answer.ToString(), "Exit") == 0)
base.OnDisappearing();
else if(string.Compare(answer.ToString(), "Stay on this page") == 0)
//here i want to stay on same page
}
I tried this but i dont want page to load
var route = $"//{ nameof(EditPage)}";
await Shell.Current.GoToAsync(route);
I also tried this, but onbackbuttonpress only works if user hit back button on mobile. not back button in app
protected override bool OnBackButtonPressed()
{
bool myResult = PromptForExit().Result;
if (myResult)
return true; // dont go back
return false;
}
private async Task<bool> PromptForExit()
{
var answer = await DisplayAlert("Changes un-saved",
"You must tap on 'save' button",
"Exit",
"Stay on this page");
if (string.Compare(answer.ToString(), "Stay on this page") == 0)
return true;
else
return false;
}
Set the attached property Shell.PresentationMode in the page you want to restrict use to stay on it this will show the page as a modal page (navigation bar will be hidden..), and at the same time override the back button like you did, but be sure to allow navigation out of that page somehow if required, thru a button on that page for example that calls Shell.GotoAsync("..")
<ContentPage ...
Shell.PresentationMode="ModalAnimated"
You may also set it value to Modal or ModalNotAnimated.
Docs Shell presentation mode
You can try something like this
protected override bool OnBackButtonPressed()
{
Device.BeginInvokeOnMainThread(async() => {
bool myResult = await PromptForExit();
if (myResult) await this.Navigation.PopAsync();
});
return true;
}
Let me know if it work for you or not.
This is a tricky question since Apple is determined to state that the "back" action must not be interrupted by the application. You can interrupt the android hardware backbutton, you can just yet interrupt the NavigationPage software backbutton, but you cannot in any way interrupt the MasterDetailPage software backbutton.
Used to, if you want to prevent the user from going back, you had to use something like this:
var loginPage = new LoginPage();
var navigationPage = new NavigationPage(loginPage);
NavigationPage.SetHasNavigationBar(loginPage, true);
// Disable the back button
NavigationPage.SetHasBackButton(loginPage, false);
await Navigation.PushModalAsync(navigationPage);
The recommended way of interacting with the "back" action was to use a modal page using Navigation.PushModalAsync(). It's a lot less painful.
Have you already tried using the new Shell. Microsoft introduced it last year just for this reason.
This seems to be an interesting investigation into the Shell: https://theconfuzedsourcecode.wordpress.com/2020/06/09/overriding-back-button-in-xamarin-forms-shell/
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
Is there a way to wrap the UWP async function for creating dialog boxes in such a way that they can be called from a normal method without the async keyword? Example:
var msgbox = new ContentDialog
{
Title = "Error",
Content = "Already at the top of the stack",
CloseButtonText = "OK"
};
await msgbox.ShowAsync();
No, there is no way to do this at the moment. If you try to block waiting for the dialog to close, your app will deadlock.
Here is how I do it (I saw this in some live demo back when UWP was just introduced):
var msgbox = new ContentDialog
{
Title = "Error",
Content = "Already at the top of the stack",
CloseButtonText = "OK"
};
var ignored = msgbox.ShowAsync();
This works as expected in a non-async void method.
public Task<ContentDialogResult> MsgBox(string title, string content)
{
Task<ContentDialogResult> X = null;
var msgbox = new ContentDialog
{
Title = title,
Content = content,
CloseButtonText = "OK"
};
try
{
X = msgbox.ShowAsync().AsTask<ContentDialogResult>();
return X;
}
catch {
return null;
}
}
private void B1BtnBack_Click(object sender, RoutedEventArgs e)
{
MsgBox("Beep", "Already at the top of stack");
return;
// ^^^ Careful here. MsgBox returns with an active task
// running to display dialog box. This works because
// the next statement is a return that directly
// returns to the UI message loop. And the
// ContentDialog is modal meaning it disables
// the page until ok is clicked in the dialog box.
}
I have the following scenario:
When the user click on button next, I have to do some validation that must be done in the controller.
If there is an error, it will show the error messages in a popup with a Yes to continue or No to cancel and close the popup.
But if there are no errors, user will continue to next page without showing a popup.
Here is what I have:
[ActionName(PA.FlowActionName), AcceptPost("nextAanvullen")]
[NoAsyncTimeout]
public ActionResult Pagina_nextAanvullen(OffrerenFlow flow)
{
var medewerkers = ((MedewerkersAanvullenPakket)(flow.CurrentBusinessObject)).Medewerkers;
//validate here
if (true) // for test use true
{
var blok = medewerkers.Deelnemers;
return View("DeelnemersFout", medewerkers); // This doesnt open a popup
}
else
{
// Go to next page
DoWorkEventHandler handler = (o, e) =>
{
AsyncManager.Parameters["navigation"] = Navigate(new NavigationData<OffrerenFlow>((OffrerenFlow)flow), PA.Next,
true);
};
ExecuteAsyncMethod(handler);
}
}
I am not getting an popup, the view just display the view "DeelnemersFout". I need it to be in a popup. Any suggestions are welkom
Assign a value in ViewBag inside IF block
if (true) // for test use true
{
ViewBag.ShowPopup = true;
var blok = medewerkers.Deelnemers;
return View("DeelnemersFout", medewerkers); // This doesnt open a popup
}
and on Cshtml capture this value in a javascript variable and check if there is a value in this variable or not, and show the popup accordingly.