Xamarin – Development

How to update content of a label UI?

  • Set Name to label element
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<Label x:Name="Label_Home_Info" Text="Default Text" FontSize="14" HorizontalOptions="Center" VerticalOptions="Center" HorizontalTextAlignment="Center" LineBreakMode="WordWrap" Margin="25,0"/>
<Label x:Name="Label_Home_Info" Text="Default Text" FontSize="14" HorizontalOptions="Center" VerticalOptions="Center" HorizontalTextAlignment="Center" LineBreakMode="WordWrap" Margin="25,0"/>
<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
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Label_Home_Info.Text = "haha"
Label_Home_Info.Text = "haha"
Label_Home_Info.Text = "haha"

How to display image from base64 string?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<Image x:Name="Image_Logo" Grid.Row="0" Grid.Column="0" Source="default_image.png" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="36"/>
<Image x:Name="Image_Logo" Grid.Row="0" Grid.Column="0" Source="default_image.png" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="36"/>
<Image x:Name="Image_Logo" Grid.Row="0" Grid.Column="0" Source="default_image.png" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="36"/>
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var byteArray = Convert.FromBase64String(base64String);
Stream stream = new MemoryStream(byteArray);
var imageSource = ImageSource.FromStream(() => stream);
Image_Logo.Source = imageSource;
var byteArray = Convert.FromBase64String(base64String); Stream stream = new MemoryStream(byteArray); var imageSource = ImageSource.FromStream(() => stream); Image_Logo.Source = imageSource;
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<Image Source="{Binding Base64Image}" HorizontalOptions="Center" VerticalOptions="Center" WidthRequest="203" HeightRequest="160"/>
<Image Source="{Binding Base64Image}" HorizontalOptions="Center" VerticalOptions="Center" WidthRequest="203" HeightRequest="160"/>
<Image Source="{Binding Base64Image}" HorizontalOptions="Center" VerticalOptions="Center" WidthRequest="203" HeightRequest="160"/>

ViewModel file

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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));
}
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)); }
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?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<v:HtmlLabel Text="<strong>Title</strong><p>ABCDEF</p>" FontSize="14" HorizontalOptions="Center" VerticalOptions="Center" HorizontalTextAlignment="Center" LineBreakMode="WordWrap" Margin="25,0"/>
<v:HtmlLabel Text="<strong>Title</strong><p>ABCDEF</p>" FontSize="14" HorizontalOptions="Center" VerticalOptions="Center" HorizontalTextAlignment="Center" LineBreakMode="WordWrap" Margin="25,0"/>
<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?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<Label Text="Trouble logging in?" TextColor="#1e3c66" FontAttributes="Bold" XAlign="Center"/>
<Label Text="Trouble logging in?" TextColor="#1e3c66" FontAttributes="Bold" XAlign="Center"/>
<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.


*