Share via

System.Reflection.TargetInvocationException / Lineagradientbrush

Miguel 21 Reputation points
2020-12-29T07:07:47.227+00:00

Hi Everyone, I'm having a hard time trying to implement a Gradient. In Xamarin Forms I'm using this code, but when I try to run I get System.Reflection.TargetInvocationException
Message=Exception has been thrown by the target of an invocation.
It only happens when I try to implement a gradient and at runtime. I haven't coded anything in the codebehind
Has anyone faced this problem?

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Servicalle.Views.LoginPage">
<ContentPage.Content>
<StackLayout>
<StackLayout.Background>
<LinearGradientBrush EndPoint="0.511,0.013" StartPoint="0.532,0.647">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FF2372A8" Offset="0.513"/>
</LinearGradientBrush>

        </StackLayout.Background>
        <Label Text="Welcome to Xamarin.Formsx!"
            VerticalOptions="CenterAndExpand" 
            HorizontalOptions="CenterAndExpand" />
    </StackLayout>
</ContentPage.Content>

</ContentPage>
The version I'm using is Microsoft Visual Studio Enterprise 2019
Version 16.8.3
VisualStudio.16.Release/16.8.3+30804.86
Microsoft .NET Framework
Version 4.8.04084

Xamarin 16.8.000.260 (d16-8@fd405a2)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Developer technologies | .NET | Xamarin
0 comments No comments

Answer accepted by question author

JarvanZhang 23,971 Reputation points
2020-12-29T08:29:16.217+00:00

Hello,​

Welcome to our Microsoft Q&A platform!

The Brush library is currently experimental in Xamarin.Forms and can only be used by setting the Brush_Experimental flag. Please add the flag on each platform project.
For example on Android:

   public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity  
   {  
       protected override void OnCreate(Bundle savedInstanceState)  
       {  
           ...  
           Forms.SetFlags(new string[] { "Brush_Experimental" });  
     
           global::Xamarin.Forms.Forms.Init(this, savedInstanceState);  
           LoadApplication(new App());  
       }  
   }  

Tutorial:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/brushes/
Best Regards,

Jarvan Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. AUU 1 Reputation point
    2021-01-26T17:46:23.293+00:00

    Did I hear that correct in one of the MS sessions that _ALL_ experimental content will be withdrawn ??

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.