mgierlasinski / MagicGradients

Draw breathtaking backgrounds in your Xamarin.Forms application. It's a kind of magic.
MIT License
369 stars 21 forks source link

How do I create a magicGradient from stylesheet string source, in C# programatically? #137

Closed tdbe closed 3 years ago

tdbe commented 3 years ago

There is no example of this on the internet.

Tried:

            MagicGradients.GradientView gradientView = new MagicGradients.GradientView();
            gradientView.VerticalOptions = verticalOptions;
            gradientView.HeightRequest = heightRequest;
            CssGradientSource cssGradientSource = new CssGradientSource();
            cssGradientSource.Stylesheet = StyleSheet.FromString(AppManager._Instance.getAGradientStylesheetAsString(index));

            gradientView.GradientSource = cssGradientSource;

doesn't work. .Stylesheet is a string, but if you give it a string it throws an error saying it can't convert from "<![CDATA[linear-gradient.... into Xamarin.Forms.Color. And I can't give it a StyleSheet either. So how do?

My app absolutely needs to spawn some components that have gradient backgrounds, and the number of components or their properties depends on user input. So pls don't tell me to just use xaml not c# :)

mgierlasinski commented 3 years ago

@tdbe Try skipping CDATA part and just use gradient:

cssGradientSource.Stylesheet = "linear-gradient(...)";

EDIT: To make life easier, we will add with next release something like:

gradientView.GradientSource = CssGradientSource.Parse("linear-gradient(...)");

Most of the time GradientView is created in XAML or styled with CSS so we focus on this API, but if you think C# API is lacking some other features, please let us know :)

tdbe commented 3 years ago

That worked! Thanks. I had no idea. The main reason to use C# is if you need to generate some ui components at runtime, say from a json that comes from the internet or from user interaction, and if you want those ui elemetns to have magic gradients.