如何使用xamarin在c#代码中设置BackgroundImageSource的不透明度

问题描述

我使用以下代码在屏幕上设置了 BackgroundImageSource

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="WeatherLocationInfo.MainPage"
         BackgroundImageSource="Images/Scattering.jpg"
         xmlns:controls="clr-namespace:marcTron.Plugin.Controls;assembly=Plugin.MtAdmob"
         xmlns:local="clr-namespace:WeatherLocationInfo"
         xmlns:forms="clr-namespace:microcharts.Forms;assembly=microcharts.Forms">

在我的 MainPage.xaml.cs 中的 c# 代码中,我检查天气状况并使用以下代码根据天气状况设置不同的背景:

string description = weatherBindingData.WeatherDataForecastHourly.List[0].WeatherForecast[0].DescriptionForecast;
                
                if (description == "clear sky")
                {
                    this.BackgroundImageSource = "Images/ClearSky.jpg";
                }
                else if (description == "few clouds")
                {
                    this.BackgroundImageSource = "Images/FewClouds.jpg";
                }
                else if (description == "scattered clouds")
                {
                    this.BackgroundImageSource = "Images/Scattering.jpg";
                }
                else if (description == "broken clouds")
                {
                    this.BackgroundImageSource = "Images/brokenClouds.jpg";
                }
                else if (description == "light rain")
                {
                    this.BackgroundImageSource = "Images/LightRain.jpg";
                }
                else if (description == "rain")
                {
                    this.BackgroundImageSource = "Images/Rain.jpg";
                }
                else if (description == "thunderstorm")
                {
                    this.BackgroundImageSource = "Images/Thunderstorm.jpg";
                }
                else if (description == "sNow")
                {
                    this.BackgroundImageSource = "Images/SNow.jpg";
                }
                else if (description == "mist")
                {
                    this.BackgroundImageSource = "Images/Mist.jpg";
                }
                else if (description == "overcast clouds")
                {
                    this.BackgroundImageSource = "Images/OverCastClouds.jpg";
                }
                else if (description == "moderate rain")
                {
                    this.BackgroundImageSource = "Images/Moderaterain.jpg";
                }

我想在每个背景图片上设置 opacity 0.3。 在 c# 中没有属性 this.BackgroundImageSource.opacity。 有没有办法设置图像的不透明度?

解决方法

您可以使用网格并添加图像以设置为不透明度的背景。

  <Grid>
    <Image
        Aspect="AspectFill"
        Opacity="0.3"
        Source="pink.jpg" />
    <StackLayout Padding="15">
        <Image
            Aspect="AspectFit"
            HeightRequest="100"
            Source="truck_black.png"
            WidthRequest="130" />
    </StackLayout>
    <ScrollView>
        <StackLayout Spacing="0">
            <StackLayout Padding="15,7,15,15" VerticalOptions="StartAndExpand">
                <StackLayout VerticalOptions="StartAndExpand">
                    <Label
                        FontSize="14"
                        Text="Company Code"
                        TextColor="Black" />
                    <Entry
                        x:Name="companyCodeEntry"
                        FontSize="14"
                        Text=""
                        TextColor="Black" />
                    <Label
                        FontSize="14"
                        Text="Username"
                        TextColor="Black" />
                    <Entry
                        x:Name="usernameEntry"
                        FontSize="14"
                        Text=""
                        TextColor="Black" />
                    <Label
                        FontSize="14"
                        Text="Password"
                        TextColor="Black" />
                    <Entry
                        x:Name="passwordEntry"
                        FontSize="14"
                        IsPassword="true"
                        Text=""
                        TextColor="Black" />
                </StackLayout>
            </StackLayout>
        </StackLayout>
    </ScrollView>
</Grid>

enter image description here