Is there a way that I can include FormattedText in a .alert with Xamarin.Forms?

妖精的绣舞 提交于 2021-02-08 06:24:19

问题


I have a formatted string. In the code it's more than this but this is just an example:

var fs = new FormattedString();
fs = fs.Spans.Add(new Span { Text = "ABC, ForegroundColor = Color.FromHex("555555") });

Is there a way that I can use formatted text in an alert?

var answer = await DisplayAlert ("Test", alertText, "Yes", "No");

回答1:


I think the answer is "no". I suggest to create your own "popupAlert" using some packages like Rg.Plugins.Popup

// Use these methods in PopupNavigation globally or Navigation in your pages

// Open new PopupPage
Task PushAsync(PopupPage page, bool animate = true) // Navigation.PushPopupAsync

// Hide last PopupPage
Task PopAsync(bool animate = true) // Navigation.PopPopupAsync

// Hide all PopupPage with animations
Task PopAllAsync(bool animate = true) // Navigation.PopAllPopupAsync

// Remove one popup page in stack
Task RemovePageAsync(PopupPage page, bool animate = true) // Navigation.RemovePopupPageAsync
<?xml version="1.0" encoding="utf-8" ?>
<pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
             xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
             x:Class="Demo.Pages.MyPopupPage">
  <!--Animations use example-->
  <pages:PopupPage.Animation>
    <animations:ScaleAnimation 
      PositionIn="Center"
      PositionOut="Center"
      ScaleIn="1.2"
      ScaleOut="0.8"
      DurationIn="400"
      DurationOut="300"
      EasingIn="SinOut"
      EasingOut="SinIn"
      HasBackgroundAnimation="True"/>
  </pages:PopupPage.Animation>
  <!-- Content -->
</pages:PopupPage>
public partial class MyPopupPage : PopupPage
    {
        public MyPopupPage()
        {
            InitializeComponent();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
        }

        protected override void OnDisappearing()
        {
            base.OnDisappearing();
        }

        // Method for animation child in PopupPage
        // Invoced after custom animation end
        protected override Task OnAppearingAnimationEnd()
        {
            return Content.FadeTo(0.5);
        }

        // Method for animation child in PopupPage
        // Invoked before custom animation begin
        protected override Task OnDisappearingAnimationBegin()
        {
            return Content.FadeTo(1);
        }

        protected override bool OnBackButtonPressed()
        {
            // Prevent hide popup
            //return base.OnBackButtonPressed();
            return true; 
        }

        // Invoced when background is clicked
        protected override bool OnBackgroundClicked()
        {
            // Return default value - CloseWhenBackgroundIsClicked
            return base.OnBackgroundClicked();
        }
    }

    // Main Page

    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        // Button Click
        private async void OnOpenPupup(object sender, EventArgs e)
        {
            var page = new MyPopupPage();

            await Navigation.PushPopupAsync(page);
            // or
            await PopupNavigation.PushAsync(page);
        }
    }



回答2:


I do this frequently without any 3rd party plugins. It is not an "Alert" per-say, technically I am just popping a new nav view onto the stack. It may suit your needs nicely.

I have a FormHelpers class in my PCL project which handles various popups with / without OK/Cancel buttons and formatted text etc. Here is my generic formatted alert popup which you could build on:

public class FormHelpers
{
public static async Task FormattedPoppup(INavigation navigation, string message)
        {
            var lblTitle = new Label { Text = "Alert", HorizontalOptions = LayoutOptions.Center, FontAttributes = FontAttributes.Bold };
            var lblMessage = new Label { Text = message };

            var btnOk = new Button
            {
                Text = "Ok",
                WidthRequest = 100,
                BackgroundColor = Color.FromRgb(0.8, 0.8, 0.8),
            };
            btnOk.Clicked += async (s, e) =>
            {
                // close page
                await navigation.PopModalAsync();
            };

            var layout = new StackLayout
            {
                Padding = new Thickness(0, 40, 0, 0),
                VerticalOptions = LayoutOptions.StartAndExpand,
                HorizontalOptions = LayoutOptions.CenterAndExpand,
                Orientation = StackOrientation.Vertical,
                Children = { lblTitle, lblMessage, btnOk },
            };

            var page = new ContentPage();
            page.Content = layout;
            await navigation.PushModalAsync(page);
        }
    }

Example calling from a button click on a Form View:

private async void button_Test_Clicked(object sender, EventArgs e)
{
    await FormHelpers.FormattedPoppup(this.Navigation, "Some alert displayed here");
}

I got this idea from this post, and have many different versions / alterations for different purposes:

https://forums.xamarin.com/discussion/35838/how-to-do-a-simple-inputbox-dialog



来源:https://stackoverflow.com/questions/48042687/is-there-a-way-that-i-can-include-formattedtext-in-a-alert-with-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!