Setting FontFamily of Button to StaticResource from code - xamarin

Looking for the code-behind equivilant of FontFamily={StaticResource FontAwesomeBrands}.
I've tried setting it directly to that string and was not too suprised when that didn't work. If I use that FontFamily in the XAML it works fine, just not sure how to do this from the code-behind.

You can call any resource from the Resources file just with the resource name.
var OnPlatformDic = (OnPlatform<string>) App.Current.Resources["FontAwesomeBrands"];
var fontFamily = OnPlatformDic.Platforms.FirstOrDefault((arg) => arg.Platform.FirstOrDefault() == Device.RuntimePlatform).Value;
YourLabel.FontFamily = fontFamily.ToString();

Related

How can I link a CarouselView and Indicator when using C# markup in my Xamarin forms app?

Here is the XAML Code from the documentation:
<StackLayout>
<CarouselView ItemsSource="{Binding Monkeys}"
IndicatorView="indicatorView">
<CarouselView.ItemTemplate>
<!-- DataTemplate that defines item appearance -->
</CarouselView.ItemTemplate>
</CarouselView>
<IndicatorView x:Name="indicatorView"/>
</StackLayout>
I am using C# Markup so my code looks like this (styling code and some layout settings removed to keep simple for SO question):
CarouselView ScrollingImageView() => new CarouselView
{
ItemTemplate = new DataTemplate(() =>
{})
}
.Bind(ItemsView.ItemsSourceProperty, nameof(_vm.ListOfItems))
.Bind(CarouselView.PositionProperty, nameof(_vm.ScrollingImageViewerPosition));
IndicatorView IndicatorView() => new IndicatorView
{
IndicatorsShape = IndicatorShape.Circle,
};
Grid ButtonIndicatorGrid() => new Grid
{
Children = { IndicatorView().Row(0).Column(1) }
};
void Build() => Content =
new Grid
{
Children = {
ScrollingImageView().Row(0),
ButtonIndicatorGrid().Row(1),
}
};
In this example, the IndicatorView is rendered beneath the
CarouselView, with an indicator for each item in the CarouselView. The
IndicatorView is populated with data by setting the
CarouselView.IndicatorView property to the IndicatorView object. Each
indicator is a light gray circle, while the indicator that represents
the current item in the CarouselView is dark gray. Setting the
CarouselView.IndicatorView property results in the
IndicatorView.Position property binding to the CarouselView.Position
property, and the IndicatorView.ItemsSource property binding to the
CarouselView.ItemsSource property.
Now I cannot find an example of how to do this with C#. In XAML it works by setting the name but how can I do the same thing in C# as from what I understand (please correct me if wrong), I cannot set the name in C# in the same way as in XAML.
you need to maintain a reference to your UI objects when you create them
var iv = IndicatorView();
var cv = ScrollingImageView();
cv.IndicatorView = iv;

How to use binding in a label in code behind

I want to display a binding field in a label. It's easy in XAML but how to do it in code-behind? In XAML,
<Label Text="{Binding LastName}" Style="{StaticResource MyLabel}"/>.
In my code behind, I have tried:
Label ln = new Label();
ln.BindingContext = "ContactsModel";
ln.SetBinding = "LastName";
which does not work and I have no clue how to set the Style.
If you read the Basic Binding documentation, then it clearly states that to bind a view you have to.
Specify a BindingContext
Use the SetBinding method to specify the target property to be bound to which ViewModel source property.
The BindingContext may be inferred from the parent element and does not always have to be specified, but your binding should look more like:
var label = new Label();
label.SetBinding(Label.TextProperty, "LastName");
This will bind the Text property on the label to LastName in the ViewModel.

get default FontFamily

In Xamarin.Forms I change FontFamily from myButton. Now I want to set default FontFamily to it. Now I use myButton.FontFamily = (new Button()).FontFamily. Is there a more appropriate way to do this?
you can reset the value to it's Default:
myButton.ClearValue(Button.FontFamilyProperty);

Xamarin Forms button with loading indicator

I'm into Xamarin since about week and have a question about the possibility of making loading icon inside a button.
I would like to achieve something similiar to this:
https://jsfiddle.net/mr8fwtcv/
I was trying to achieve this effect with FontAwesome but had a problem animating it, so it would be not bad to use Activity Indicator in the solution.
My button is placed inside Stack Layout, and here is the code:
var loginButton = new Button
{
BackgroundColor = Color.FromHex("689F38"),
TextColor = Color.White,
HeightRequest = 46,
Text = "Log in",
FontAttributes = FontAttributes.Bold,
FontSize = 18,
Margin = margin
};
I was already looking for some plugins / solutions but couldn't find any.
Thanks in advance.
You will have to use a Layout to have more than one control in it. If you are not looking for CustomRenderers then what you could do is have a RelativeLayout and inside that both the Button and the Activity Indicator. You will also have to write a Converter for the button text that will set the text to string.Empty when the IsBusy is true.
For example:
<StackLayout>
<ActivityIndicator IsVisible="{Binding IsBusy}" IsRunning="{Binding IsBusy}" />
<Button Text="{Binding ButtonText, Converter={StaticResource ButtonTextConverter}, ConverterParameter={Binding IsBusy}}" />
</StackLayout>
Also if you are planning to use this in multiple places you can create this as a custom control.
You can check out the custom control code here.
The full project is available here.

Add Elements dynamically to The XAML page in Windows Phone 7

I'm new to WP7 and I want to know if there is any way to add items like a TextBlock to
a page dynamically using the .cs part??
Try this
var textBlock = new TextBlock();
// set some properties
YourMainContainer.Children.Add(textBlock); //
If you need more details just comment this
If you know the controls that you'd like to appear on the page dynamically, then I'd approach the problem by including those controls in the XAML and using the Visibility property on the controls to show and hide them. In Silverlight, the Visibility enumeration is limited to the values Visible and Collapsed, so when it isn't visible the it doesn't take up any space. You can control Visibility with data-binding by using a converter (search on "visibility bind converter") if you are intersted in pursuing that avenue. You can show/hide groups of controls by changing the Visibility of their parent control, such as StackPanel or custom control.
Try this one,
TextBlock txtmsg = new TextBlock();
txtmsg.Text = "New Program.";
txtmsg.Margin = new Thickness(10, 20, 10, 10);
txtmsg.TextWrapping = TextWrapping.Wrap;
txtmsg.FontSize = 28;
txtmsg.TextAlignment = TextAlignment.Center;
ContentPanel.Children.Add(txtmsg);

Resources