2017-12-31 197 views
2

私はフォーマットされた文字列を持っています。コードでは、これ以上だが、これはあくまでも一例です:Xamarin.Formsで.alertにFormattedTextを含める方法はありますか?

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

は、私は、アラートにフォーマットされたテキストを使用することができる方法はありますか?

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

答えて

3

私は答えが「いいえ」だと思います。私は、任意のサードパーティ製のプラグインなしで頻繁にこれを行う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

のようないくつかのパッケージを使用して、独自の「popupAlert」を作成することをお勧めします。技術的には、新しいナビゲーションビューをスタックにポップしているだけでは、「アラート」ではありません。あなたのニーズにうまく合っているかもしれません。

私はここで/ OKなし/キャンセルボタンや書式付きテキストなど様々なポップアップを扱う私のPCLプロジェクトにFormHelpersクラスを持っているが、あなたは上で構築することができ、私の一般的なフォーマットされた警告ポップアップです:

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

関連する問題