Xamarin – Development

How to update content of a label UI?

  • Set Name to label element
<Label x:Name="Label_Home_Info" Text="Default Text" FontSize="14" HorizontalOptions="Center" VerticalOptions="Center" HorizontalTextAlignment="Center" LineBreakMode="WordWrap" Margin="25,0"/>
  • Set text programmatically
Label_Home_Info.Text = "haha"

How to display image from base64 string?

<Image x:Name="Image_Logo" Grid.Row="0" Grid.Column="0" Source="default_image.png" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="36"/>
var byteArray = Convert.FromBase64String(base64String);
Stream stream = new MemoryStream(byteArray);
var imageSource = ImageSource.FromStream(() => stream);

Image_Logo.Source = imageSource;

How to bind base64 data to Image tag?

xaml file

<Image Source="{Binding Base64Image}" HorizontalOptions="Center" VerticalOptions="Center" WidthRequest="203" HeightRequest="160"/>

ViewModel file

public ImageSource Base64Image
{
    get
    {
        if (AppState.Image.url != "")
        {
            var byteArray = Convert.FromBase64String(AppState.Image.url);
            Stream stream = new MemoryStream(byteArray);
            var imageSource = ImageSource.FromStream(() => stream);
            return imageSource;
        }
        else
        {
            // Display default image
            return "default_image.png";
        }
    }
}

public void UpdateState()
{
    OnPropertyChanged(nameof(Base64Image));
}

How to display rich text?

<v:HtmlLabel Text="<strong>Title</strong><p>ABCDEF</p>" FontSize="14" HorizontalOptions="Center" VerticalOptions="Center" HorizontalTextAlignment="Center" LineBreakMode="WordWrap" Margin="25,0"/>

How to check null and empty?

How to add a new resource image files?

Android

  • Right click on Android\Resources\drawable, drawable-hdpi, drawable-xhdpi, drawable-xxhdpi, drawable-xxxhdpi
  • Add existing file
  • Copy file

iOS

  • Open iOS\Assets.xcassets
  • Add new image set
  • Select image files for 1x, 2x, 3x

How to align text center in Label?

<Label Text="Trouble logging in?" TextColor="#1e3c66" FontAttributes="Bold" XAlign="Center"/>

Be the first to comment

Leave a Reply

Your email address will not be published.


*