

Xamarin Forms Tips Part 3: Change Randomly A View’s Color At Run-time
source link: https://www.tuicool.com/articles/hit/ArmA7zQ
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.


Hi everyone, here is the third part of the Xamarin Forms Tips series. Today, we will look at how to change a view’s color randomly at run time. I implemented this feature in the “My Expenditures” application. As you can see on the screenshots above, the first letter of the user’s name changes color every time he runs the app. And it does so randomly. This post is all about implementing this functionality.
The app’s name is : My Expenditures. It is available on Play store and Microsoft store . It provides you a smart way to manage you expenses and income. And very detailed stats about how you spend or make money. You can download and use it. You will also find in the app all the features I talk about here.
Change Randomly A View’s Color At Run-time
We will change the color of the text of a label at run-time. This feature is implemented using markup extensions. We predefined a set of colors which we randomly assign to the view. This is extremely simple and here is the code for it.
[ContentProperty("TextColor")] public class RandomColorExtension : IMarkupExtension { /// <summary> /// Send a Random Color to be displayed by the user. /// </summary> /// <param name="serviceProvider"></param> /// <returns></returns> public object ProvideValue(IServiceProvider serviceProvider) { var colors = new List<Color> { Color.SkyBlue, Color.Salmon, Color.SandyBrown, Color.SeaGreen, Color.Turquoise }; return colors[new Random().Next(0,4)]; } }
After implementing the markup extension, applying it to a label is very easy.
<Label FontAttributes="Bold" FontSize="{StaticResource ExtraHugeSize}" HorizontalOptions="Center" Text="{Binding UserInitial, Mode=TwoWay}" TextColor="{markupExtensions:RandomColorExtension}" VerticalOptions="Center" />
Conclusion
You can randomly change the color of your controls easily at runtime. This feature can help you improve UX. I applied it to a label, but you could apply it to any other control.
You may be interestedby this post about numeric entry recipes. Learn more about markup extensions here .
Follow me on social media and stay updated
Recommend
-
18
Color the iOS Status Bar in Xamarin.Forms307 views•Jan 2, 2021 After publishi...
-
11
I have recently been exploring DynamicData and since I started using it, I can say that it has been a game changer in my applications. It is quite easy to use and provides a lot of flexibility when working with collections. So, I decided to w...
-
11
“To err is human…”-Alexander PopeIt doesn’t matter how long you’ve been a developer; many mistakes occur while developing an application. We can only strive to do better every day. So, to help y...
-
19
Xamarin Forms – Set entry clear icon color on iOS Here is a small piece of code that you can use as a Xamarin Forms effect to hook into your entry. It allows changing the color of the clear icon that is available inside...
-
11
Creating a custom control is a common thing to do when developing a Xamarin Forms application. In many cases, the main purpose of creating one is to have a reusable control that we can use across all our application. In this articl...
-
8
In the first part of this series about State Machine, we covered the basics about it: how to set up and how to use the main features. In this part, w...
-
10
Using State Machine in Xamarin Forms (Part 3)
-
8
How to randomly permutate an Array? Part 1The Dark Art, the Black Magic, the Forbidden Spell2019-09-25 • 11 min readThis is going to be a blog series of me discussing how to permutate a ra...
-
10
In the first part of this series, we covered how to build a task sequence. In this part I’m going to show you how to start the sequence from a specific task, run tasks conditionally and pass parameters between tasks.
-
9
This post will be focused on some hints that can help you to make the migration from Xamarin.Forms to .NET MAUI a smoother process.If you’re reading this post after the
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK