The Xamarin application has the following page structure: (to the center by content) Frame - StackLayout - ScrollView -Grid with two rows and one column - both an Image element in the first line and lable in the second label. Here is the actual code:
InitializeComponent(); Label lb = new Label { Text="pasta"}; img = new Image {Source ="pasta.jpg" }; Frame fr = new Frame { HeightRequest=250, WidthRequest=250 }; StackLayout st = new StackLayout {HeightRequest=fr.HeightRequest , WidthRequest =fr.WidthRequest , Orientation=StackOrientation.Vertical }; sc = new ScrollView { WidthRequest=st.WidthRequest , HeightRequest = st.HeightRequest }; Grid gr = new Grid { RowDefinitions = { new RowDefinition { Height =new GridLength(sc.HeightRequest) }, new RowDefinition { Height = new GridLength(sc.HeightRequest) } } , ColumnDefinitions = { new ColumnDefinition { Width = new GridLength(sc.WidthRequest)} } }; gr.Children.Add(img,0,0); gr.Children.Add(lb, 0, 1); sc.Content = gr; sc.Scrolled += On_scroll; st.Children.Add(sc); fr.Content = st; this.Content = fr; Now, what is actually required: it is necessary that the transparency of the image would decrease when scrolling. For this, I am Scrolled to the Scrolled event Scrolled
sc.Scrolled += On_scroll; stp = new Stepper { Increment=0.5}; img.BindingContext = Img_Pr;//ΠΏΡΠΈΠ²ΡΠ·ΡΠ²Π°Π΅ΠΌ Img_Pr ΠΊ img.BindingContext private void On_scroll(object sender, ScrolledEventArgs e) { stp.ValueChanged += Scroll_Step_img; //Π²ΡΠ·ΡΠ²Π°Π΅ΠΌ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΡ stepper } to control the degree of transparency - I use Stepper - the handler of which I call in the event handler for the Scrolled event. On the Stepper object - stp - I call the ValueChanged event and subscribe a handler to it.
In order to control the transparency of the image - it does not have such a property, I decided to create a BindableProperty
public class Image_Property_Class : Xamarin.Forms.Image { public static readonly BindableProperty transperentProperty = BindableProperty.Create("Transparent", typeof(Color), typeof(Xamarin.Forms.Image), new Color(255, 255, 255, 0)); public Color Transparent { set { SetValue(transperentProperty,value); } get { return (Color)GetValue(transperentProperty); } } } -For this, I created a class that is examined from the Image class and already in it created BindableProperty. In theory, through it, I want to change the value of the alpha channel of the original image - making it more transparent.
public static Image_Property_Class Img_Pr = new Image_Property_Class { };//ΠΎΠ±ΡΠ΅ΠΊΡ ΠΏΡΠΈΠ²ΡΠ·ΠΊΠΈ In the event handler of the ValueChanged event of the ValueChanged object
public static void Scroll_Step_img(object sender, ValueChangedEventArgs e) { //Π½Π°ΡΡΡΠ°ΠΈΠ²Π°Π΅ΠΌ ΠΎΠ±ΡΠ΅ΠΊΡ ΠΏΡΠΈΠ²ΡΠ·ΠΊΠΈ Img_Pr.SetBinding(Image_Property_Class.transperentProperty, "img.BackgroundColor"); //ΡΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅ΠΌ Π½ΠΎΠ²ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ Img_Pr.Transparent = new Color(img.BackgroundColor.R, img.BackgroundColor.G, img.BackgroundColor.B, e.NewValue); } I set up a binding by setting the dependency and passing the Transperent property itself β a new value in the form of the -e event argument. But at the same time when scrolling the image does not become more transparent. Here is the complete code in order:
ScrollView sc; public static Image img; Stepper stp; public MainPage() { InitializeComponent(); Label lb = new Label { Text="pasta"}; img = new Image {Source ="pasta.jpg" }; Frame fr = new Frame { HeightRequest=250, WidthRequest=250 }; StackLayout st = new StackLayout {HeightRequest=fr.HeightRequest , WidthRequest =fr.WidthRequest , Orientation=StackOrientation.Vertical }; sc = new ScrollView { WidthRequest=st.WidthRequest , HeightRequest = st.HeightRequest }; Grid gr = new Grid { RowDefinitions = { new RowDefinition { Height =new GridLength(sc.HeightRequest) }, new RowDefinition { Height = new GridLength(sc.HeightRequest) } } , ColumnDefinitions = { new ColumnDefinition { Width = new GridLength(sc.WidthRequest)} } }; gr.Children.Add(img,0,0); gr.Children.Add(lb, 0, 1); sc.Content = gr; sc.Scrolled += On_scroll; st.Children.Add(sc); fr.Content = st; this.Content = fr; stp = new Stepper { Increment=0.5}; img.BindingContext = Img_Pr;//ΠΏΡΠΈΠ²ΡΠ·ΡΠ²Π°Π΅ΠΌ Img_Pr ΠΊ img.BindingContext } public static Image_Property_Class Img_Pr = new Image_Property_Class { };//ΠΎΠ±ΡΠ΅ΠΊΡ ΠΏΡΠΈΠ²ΡΠ·ΠΊΠΈ private void On_scroll(object sender, ScrolledEventArgs e) { stp.ValueChanged += Scroll_Step_img; //Π²ΡΠ·ΡΠ²Π°Π΅ΠΌ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΡ stepper } public static void Scroll_Step_img(object sender, ValueChangedEventArgs e) { //Π½Π°ΡΡΡΠ°ΠΈΠ²Π°Π΅ΠΌ ΠΎΠ±ΡΠ΅ΠΊΡ ΠΏΡΠΈΠ²ΡΠ·ΠΊΠΈ Img_Pr.SetBinding(Image_Property_Class.transperentProperty, "img.BackgroundColor"); //ΡΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅ΠΌ Π½ΠΎΠ²ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ Img_Pr.Transparent = new Color(img.BackgroundColor.R, img.BackgroundColor.G, img.BackgroundColor.B, e.NewValue); } } BindablePeoperty
public class Image_Property_Class : Xamarin.Forms.Image { public static readonly BindableProperty transperentProperty = BindableProperty.Create("Transparent", typeof(Color), typeof(Xamarin.Forms.Image), new Color(255, 255, 255, 0)); public Color Transparent { set { SetValue(transperentProperty,value); } get { return (Color)GetValue(transperentProperty); } } }
Opacityis responsible for transparency. - LLENN