Xamarin Forms Working with Fonts - visual-studio-2013

I'm trying to reproduce this sample
https://github.com/xamarin/xamarin-forms-samples/blob/master/WorkingWithFonts
Especially these lines of code:
var labelBoldItalic = new Label {
Text = "BoldItalic",
Font = Font.SystemFontOfSize (14, FontAttributes.Bold | FontAttributes.Italic),
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
};
But VS 2013 says: The Name FontAttributes does not exist in the current context.
Xamarin Version:

Thank you Jason.
I solved via NUGET:
Get-Project -all | Install-Package Xamarin.Forms

Related

How to configure the zise tag when the device screen size is modified

In xamarin I would like a label text with a maximum length. When they modify the size of the device screen, all the labels are not displayed correctly
I am using xamarin forms 5.0.0.2244
var labelCountry = new Label
{
Text = $"Bienvenido a {Settings.Country} ",
FontSize = 14,
TextColor = Color.Blue
};
read the docs on Layout Options
HorizontalOptions = LayoutOptions.FillAndExpand
this will make the Label expand to fill the size of it's parent container

Xamarin Forms ScrollView scrolls automatically to the beginning when any child element is clicked

I created a Horizontal / Vertical Scrollview in Xamarin Forms that contains several types of childs like:
Labels
CustomCheckboxes
ImageViews
Pickers
Sliders
Here is my code:
ScrollView scrollTracks = new ScrollView
{
BackgroundColor = Color.Black,
VerticalOptions = LayoutOptions.StartAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
Content = layoutTracks,
Orientation = ScrollOrientation.Both
};
When I click some child elements like the CustomCheckboxes, or the Labels, or the ImageViews, the program scroll automatically the view to the beginning.
I tested this on the UWP platform.
How can I make the program to stop doing this?
Add this tag to the ScrollView solved my issue:
IsTabStop = false
Here the code:
ScrollView scrollTracks = new ScrollView
{
BackgroundColor = Color.Black,
VerticalOptions = LayoutOptions.StartAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
Content = layoutTracks,
Orientation = ScrollOrientation.Both,
IsTabStop = false
};

How to merge two image in Xamarin Forms?

I'm developing a Xamarin Forms iOS app. In the xaml file, there is a grid.
<Grid x:Name="QrCodeSite" HeightRequest="300" Margin="37, 37, 37, 0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
</Grid>
In the related .cs file, I use ZXing.Net.Mobile.Forms to generate a QR code and place it in the grid. And I put my logo in the same grid, which will finally appear at the center of the QR code.
var barcode = new ZXingBarcodeImageView
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
barcode.BarcodeFormat = ZXing.BarcodeFormat.QR_CODE;
barcode.BarcodeOptions.Width = 650;
barcode.BarcodeOptions.Height = 650;
barcode.BarcodeOptions.Margin = 1;
barcode.BarcodeValue = value;
var img = new Image
{
Source = "logo.png",
WidthRequest = 70,
HeightRequest = 70,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center
};
QrCodeSite.Children.Clear();
QrCodeSite.Children.Add(barcode);
QrCodeSite.Children.Add(img);
The problem is, maybe my phone (iPhone 6s plus) is too slow, sometimes the logo appears first and after a lag (around 1 second) the QR code is shown. How can I merge the QR code and the logo into one image and then add it to the grid?
You can using SkiaSharp to display Image or merge images . Having a look at how to Display SkiaSharp bitmaps to download sample project to research at it.
Based on Drawing on existing bitmaps reference , you can modify it as follow :
public partial class MonkeyMoustachePage : ContentPage
{
SKBitmap monkeyBitmap;
public MonkeyMoustachePage()
{
Title = "Monkey Moustache";
monkeyBitmap = BitmapExtensions.LoadBitmapResource(GetType(),
"SkiaSharpFormsDemos.Media.MonkeyFace.png");
SKBitmap iconImage = BitmapExtensions.LoadBitmapResource(GetType(),
"SkiaSharpFormsDemos.Media.GooglePlaylogo.png");
int offset = monkeyBitmap.Width / 2 - iconImage.Width / 2;
int offsetTop = monkeyBitmap.Height / 2 - iconImage.Height / 2;
// Create canvas based on bitmap
using (SKCanvas canvas = new SKCanvas(monkeyBitmap))
{
canvas.DrawBitmap(iconImage, SKRect.Create(offset, offsetTop, iconImage.Width, iconImage.Height));
}
// Create SKCanvasView to view result
SKCanvasView canvasView = new SKCanvasView();
canvasView.PaintSurface += OnCanvasViewPaintSurface;
Content = canvasView;
//save the new image
using (MemoryStream memStream = new MemoryStream())
using (SKManagedWStream wstream = new SKManagedWStream(memStream))
{
monkeyBitmap.Encode(wstream, imageFormat, quality);
byte[] data = memStream.ToArray();
// Check the data array for content!
bool success = await DependencyService.Get<IPhotoLibrary>().SavePhotoAsync(data, folder, filename);
// Check return value for success!
}
}
void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
{
SKImageInfo info = args.Info;
SKSurface surface = args.Surface;
SKCanvas canvas = surface.Canvas;
canvas.Clear();
canvas.DrawBitmap(monkeyBitmap, info.Rect, BitmapStretch.Uniform);
}
}
Then you will see a logo icon will display in original image :
If you want to save SkiaSharp bitmaps to files , have a look at this :https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/bitmaps/saving#exploring-the-image-formats
Note: BitmapExtensions.cs file is from sample project .By the way , when adding image to project , you need to set Build ACtion of image be Embedded resource .As follow :

ZXingBarcodeImageView (QR code) showing on iOS but not on Android (Xamarin.Forms)

This piece of code for showing a QR code in a Xamarin.Forms app works in iOS but not on Android:
let barCode = ZXingBarcodeImageView(HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
BarcodeFormat = ZXing.BarcodeFormat.QR_CODE,
BarcodeValue = foo)
barCode.BarcodeOptions.Width <- 500
barCode.BarcodeOptions.Height <- 500
mainLayout.Children.Add(barCode)
There's no error in the log, no exception thrown. Tried many heights and widths and different LayoutOptions to no avail. How can I debug this?
Luckily, I just had to play around with ZXing.Net.Mobile in my own Xamarin.Forms project. Where I managed to display the QRCode for both iOS and Android with the next C# code:
ZXingBarcodeImageView GenerateQR(string codeValue)
{
var qrCode = new ZXingBarcodeImageView
{
BarcodeFormat = BarcodeFormat.QR_CODE,
BarcodeOptions = new QrCodeEncodingOptions
{
Height = 350,
Width = 350
},
BarcodeValue = codeValue,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand
};
// Workaround for iOS
qrCode.WidthRequest = 350;
qrCode.HeightRequest = 350;
return qrCode;
}
Please note that there is a know issue in this library, and you have to set explicitly the WidthRequest & HeightRequest.
P.S.: More or less the same issue have been discussed here as well.

how to apply max height and enable scroll to Xamarin.Forms.Editor

I wanted chat application type user interface in my app & i am targeting android and iOS.
I am using Xamarin.Forms.Editor for reply
Editor _replyEntry = new Editor
{
HorizontalOptions = LayoutOptions.FillAndExpand,
TextColor = Color.Black
}
in this case the editor height remains constant and allows scrolling and editor does not expands
Then i used InvalidateMeasure()
_replyEntry .TextChanged += (sender, e) => { this.InvalidateMeasure(); };
in this case editor expands as when the text requires more space but does not allow scroll inside editor and if user types long message then editor does not allows scroll and text goes behind the keyboard and not visible to user
Is there any way to enable scroll and give max height to edit either in xamarin.forms of by writing custom renderer
Thanks
Here is my code
public class abc : ContentPage
{
public abc()
{
Image attchment = new Image
{
Source = "attachment.png",
HorizontalOptions = LayoutOptions.Start
};
Editor _replyEntry = new Editor
{
HorizontalOptions = LayoutOptions.FillAndExpand,
TextColor = Color.Black,
};
Button _sendButton = new Button
{
Text = "Send",
TextColor = Color.Black,
BackgroundColor = Color.Transparent,
HorizontalOptions = LayoutOptions.End
};
StackLayout replyStack = new StackLayout
{
Orientation = StackOrientation.Horizontal,
Padding = new Thickness(10),
Spacing = 10,
VerticalOptions = LayoutOptions.End,
Children = { attchment, _replyEntry, _sendButton }
};
Content = replyStack;
}
}
It looks like you will have to use a custom renderer to achieve what you are wanting.
There is a post here that has pretty much the same thing with what you are trying to achieve for Android.
In that demo it has an expanding multi-line EditText control (android:singleLine="false"), with only vertical scrollbars (android:scrollbars="vertical"), whilst disabling the horizontal scrollbars (android:scrollHorizontally="false").
You need to ensure the Editor' parent is expanding, then the editor will automatically expand too. If you make an empty contentpage and add an Editor, the is will just expand. If you place it inside a stacklayout, the you need to ensure that the stacklayout is expanding.

Resources