Thread ID: |
Created: |
Updated: |
Platform: |
Replies: |
150718 | Jan 17,2020 01:48 AM UTC | Jan 31,2020 01:47 PM UTC | Xamarin.Forms | 12 |
![]() |
Tags: SfPopup |
protected bool DisplayAlert(string title, string text, string acceptButtonText, string declineButtonText = null) { SfPopupLayout popupMessage = new SfPopupLayout { PopupView = { HeaderTitle = title, AcceptButtonText = acceptButtonText, AcceptCommand = new Command(() => _displayAlertAcceptDecline = true ), DeclineButtonText = declineButtonText, DeclineCommand = new Command( () => _displayAlertAcceptDecline = false), AppearanceMode = declineButtonText == null ? AppearanceMode.OneButton : AppearanceMode.TwoButton, AutoSizeMode = AutoSizeMode.Height, WidthRequest = 250, ShowCloseButton = false, ContentTemplate = new DataTemplate(() => { Label label = new Label { HorizontalTextAlignment = TextAlignment.Center, VerticalTextAlignment = TextAlignment.Center, Text = text, Padding = 25 }; return label; }) } }; popupMessage.Show(); return _displayAlertAcceptDecline; }
private static bool _displayAlertAcceptDecline; //true = accept, false = decline
[Android]
[MainActivity.cs]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
....
....
SfPopupLayoutRenderer.Init();
LoadApplication(new App(new AndroidInitializer()));
}
}
[iOS]
[AppDelegate]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
…..
…..
SfPopupLayoutRenderer.Init();
}
}
|
[ViewModelBase]
SfPopupLayout popupMessage = new SfPopupLayout
{
PopupView =
{
......
AcceptButtonText = declineButtonText,
AcceptCommand = new Command(() => _displayAlertAcceptDecline = false ),
DeclineButtonText = acceptButtonText,
DeclineCommand = new Command( () => _displayAlertAcceptDecline = true),
.....
.....
}
};
popupMessage.Show();
|
private void ShowPopup(object obj) { var returnbool = DisplayAlert("Testing", "Testing accept decline", "Accept", "Decline"); ChangeLabel = returnbool.ToString(); }
public class MainPageViewModel : ViewModelBase
{
…. public string ChangeLabel {
get;
set;
}
…. private void ShowPopup(object obj) {
var popupLayout = DisplayAlert("Testing", "Testing accept decline", "Decline", "Accept"); (popupLayout as SfPopupLayout).Closed += MainPageViewModel_Closed;
}
private void MainPageViewModel_Closed(object sender, System.EventArgs e) {
if (_displayAlertAcceptDecline == true)
{
ChangeLabel = "True";
}
else
{
ChangeLabel = "False";
}
}
… }
public class ViewModelBase : BindableBase, IInitialize, INavigationAware, IDestructible {
… public bool _displayAlertAcceptDecline; …. protected SfPopupLayout DisplayAlert(string title, string text, string acceptButtonText, string declineButtonText = null) {
SfPopupLayout popupMessage = new SfPopupLayout
{
…. } return popupMessage; } … } |
….
PopupView =
{
….
AcceptCommand = new AcceptButtonCustomCommand(),
DeclineCommand = new DeclineButtonCustomCommand(),
….
}
public class AcceptButtonCustomCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
// You can write your set of codes that needs to be executed
}
}
public class DeclineButtonCustomCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
// You can write your set of codes that needs to be executed
}
} |
private void ShowAlertBox(object obj) { DisplayAlertBox = true; // Here I want to set what was returned from the user pressing Accept/Decline //var response = value returned from DisplayAlertBox // if(response == true) // TextFromPopup = "Accept Pressed" // else // TextFromPopup = "Decline Pressed" }
public Command AcceptCommand => new Command(AcceptPressed); public Command DeclineCommand => new Command(DeclinePressed); private void AcceptPressed(object obj) { //return string 'Accept' } private void DeclinePressed(object obj) { // return string 'Decline' }
<popuplayout:PopupView.ContentTemplate> <DataTemplate> <Label x:Name="messageLabel" Text="New Message" /> DataTemplate> popuplayout:PopupView.ContentTemplate>
This post will be permanently deleted. Are you sure you want to continue?
Sorry, An error occured while processing your request. Please try again later.
This page will automatically be redirected to the sign-in page in 10 seconds.