我有一个格式化的字符串。在代码中它不仅仅是这个,但这只是一个例子:
var fs = new FormattedString();
fs = fs.Spans.Add(new Span { Text = "ABC, ForegroundColor = Color.FromHex("555555") });
有没有办法可以在警报中使用格式化文本?
var answer = await DisplayAlert ("Test", alertText, "Yes", "No");
答案 0 :(得分:3)
我认为答案是"没有"。我建议创建自己的" popupAlert"使用像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);
}
}
答案 1 :(得分:1)
我经常这样做而没有任何第三方插件。它不是一个“警报”,技术上我只是将新的导航视图弹出到堆栈上。它可能很适合您的需求。
我的PCL项目中有一个FormHelpers类,可以使用/不使用OK / Cancel按钮和格式化文本来处理各种弹出窗口。这是我可以构建的通用格式化警报弹出窗口:
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);
}
}
从按钮单击窗体视图调用示例:
private async void button_Test_Clicked(object sender, EventArgs e)
{
await FormHelpers.FormattedPoppup(this.Navigation, "Some alert displayed here");
}
我从这篇文章中得到了这个想法,并且出于不同的目的有许多不同的版本/变更:
https://forums.xamarin.com/discussion/35838/how-to-do-a-simple-inputbox-dialog