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); } } } 
  • Why do images have no transparency property? Opacity is responsible for transparency. - LLENN

1 answer 1

You can use the Opacity property and Binding in this way.

for example

Viewmodel

  class ViewModel : INotifyPropertyChanged { double _opacity; public double Opacity { get => _opacity; set { _opacity = value; OnPropertyChanged(nameof(Opacity)); } } //Impement INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChangedInvocator] protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } 

Xaml

 <Image Opacity="{Binding Opacity}" Source="image.png" /> 

In your case, you can do:

 public static void Scroll_Step_img(object sender, ValueChangedEventArgs e) { Device.BeginInvokeOnMainThread(() => { Img_Pr.Opacity = e.NewValue/255; }); } 
  • Welcome to Stack Overflow in Russian! Please try to leave a little more detailed answers. you can add an answer by clicking edit - aleksandr barakin
  • What about Xaml? - 0xdb
  • @ 0xdb XAML (eXtensible Application Markup Language) is a markup language - user227049