BackgroundColor="{Binding …." 不起作用

huangapple go评论106阅读模式
英文:

BackgroundColor="{Binding ...." does not work

问题

BackgroundColor="{Binding VARHaute.NiveauColor}" --> 图像消失,没有颜色

英文:

BackgroundColor="{Binding [item from 'Models']" does not work

based on sample: 'Shell Mixed Navigation'

when using:
BackgroundColor="red" or "#f2a82f" --> it works

when using:
BackgroundColor="{Binding VARHaute.NiveauColor}" --> Image disappears, no color

In 'Models' :
NiveauColor = "#f2a82f", other items in 'Models' work correctly !)

PS, see github: https://github.com/edikaufmann/RandoGIT
'VARHauteDetailPage.xaml'

? what in the world I'm not seeing ?

答案1

得分: 0

如果您查看BackgroundColor属性,您会看到它接受一个Color参数。

public Color BackgroundColor { get; set; }

您的模型试图将字符串传递给它。因此,它不理解应该如何处理它。幸运的是,有一种很好的方法可以解决这个问题。我们可以创建一个转换器,在将值传递给属性之前可以对其进行操作。

我们必须告诉视图在哪里找到资源,所以在顶部添加以下内容:

<ContentPage.Resources>
    <ResourceDictionary>
        <converters:ColorHexStringToMAUIColorConverter x:Key="ColorHexStringToMAUIColorConverter" />
    </ResourceDictionary>
</ContentPage.Resources>

我们创建一个简单的转换器,如下所示:

public class ColorHexStringToMAUIColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string hexColor = (string)value;
        ColorTypeConverter converter = new ColorTypeConverter();

        return (Color)converter.ConvertFromInvariantString(hexColor);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

更新

完整的ContentPage如下:

<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
    x:Class="Randoff.Views.VARHauteDetailPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:converters="clr-namespace:Randoff.Converters"
    Title="VAR-Haute">

    <ContentPage.Resources>
        <ResourceDictionary>
            <converters:ColorHexStringToMAUIColorConverter x:Key="ColorHexStringToMAUIColorConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <ScrollView>
        <StackLayout Margin="0">
            <Label
                HorizontalOptions="Center"
                Style="{DynamicResource TitleStyle}"
                Text="{Binding VARHaute.Name}" />
            <!--
            <Label Text="{Binding VARHaute.Location}"
                   FontAttributes="Italic"
                   HorizontalOptions="Center" />
            -->
            <Image
                HeightRequest="240"
                HorizontalOptions="Center"
                Source="{Binding VARHaute.Image}"
                WidthRequest="300" />
            <Image
                Aspect="AspectFill"
                HeightRequest="100"
                HorizontalOptions="Center"
                Source="{Binding VARHaute.Profil}"
                WidthRequest="200" />
            <Label
                HorizontalOptions="Center"
                Style="{DynamicResource BodyStyle}"
                Text="{Binding VARHaute.Distance, StringFormat='Distance: {0}km'}" />
            <Label
                HorizontalOptions="Center"
                Style="{DynamicResource BodyStyle}"
                Text="{Binding VARHaute.Denivele, StringFormat='Dénivelé: {0}m'}" />
            <Image
                BackgroundColor="red"
                HeightRequest="30"
                HorizontalOptions="Center"
                Source="hiking.png"
                WidthRequest="50" />
            <Image
                BackgroundColor="{Binding VARHaute.NiveauColor, Converter={StaticResource ColorHexStringToMAUIColorConverter}}"
                HeightRequest="30"
                HorizontalOptions="Center"
                Source="hiking.png"
                WidthRequest="50" />

            <Button
                BackgroundColor="{StaticResource Primary}"
                Command="{Binding TapCommand}"
                CommandParameter="{Binding VARHaute.Map}"
                Text="Map"
                TextColor="White"
                WidthRequest="150" />
        </StackLayout>

    </ScrollView>
</ContentPage>

转换器在其自己的文件中:

using Microsoft.Maui.Graphics.Converters;
using System.Globalization;

namespace Randoff.Converters
{
    public class ColorHexStringToMAUIColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string hexColor = (string)value;
            ColorTypeConverter converter = new ColorTypeConverter();

            return (Color)converter.ConvertFromInvariantString(hexColor);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
英文:

If you look at the property of BackgroundColor you will see that it takes a Color parameter.

public Color BackgroundColor { get; set; }

Your model is trying to feed it a string. So it doesn't understand what it should do with that. Fortunate there is a good way to go about this. We can make a converter where you can manipulate the value before it goes into the property.

&lt;Image
    BackgroundColor = &quot;{Binding VARHaute.NiveauColor, Converter={StaticResource ColarHexStringToMAUIColorConverter}}&quot;
    HeightRequest = &quot;30&quot;
    HorizontalOptions = &quot;Center&quot;
    Source = &quot;hiking.png&quot;
    WidthRequest = &quot;50&quot; /&gt;

We have to tell the view where to find resourses so at the top you have to add:

&lt;ContentPage.Resources&gt;
    &lt;ResourceDictionary&gt;
        &lt;converters:ColorHexStringToMAUIColorConverter x:Key=&quot;ColorHexStringToMAUIColorConverter&quot; /&gt;
    &lt;/ResourceDictionary&gt;
&lt;/ContentPage.Resources&gt;

We make a simple converter like so:

public class ColorHexStringToMAUIColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string hexColor = (string)value;
        ColorTypeConverter converter = new ColorTypeConverter();

        return (Color)converter.ConvertFromInvariantString(hexColor);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

UPDATE

The comeplete ContentPage

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;ContentPage
    x:Class=&quot;Randoff.Views.VARHauteDetailPage&quot;
    xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
    xmlns:converters=&quot;clr-namespace:Randoff.Converters&quot;
    Title=&quot;VAR-Haute&quot;&gt;

    &lt;ContentPage.Resources&gt;
        &lt;ResourceDictionary&gt;
            &lt;converters:ColorHexStringToMAUIColorConverter x:Key=&quot;ColorHexStringToMAUIColorConverter&quot; /&gt;
        &lt;/ResourceDictionary&gt;
    &lt;/ContentPage.Resources&gt;

    &lt;ScrollView&gt;
        &lt;StackLayout Margin=&quot;0&quot;&gt;
            &lt;Label
                HorizontalOptions=&quot;Center&quot;
                Style=&quot;{DynamicResource TitleStyle}&quot;
                Text=&quot;{Binding VARHaute.Name}&quot; /&gt;
            &lt;!--&lt;Label Text=&quot;{Binding VARHaute.Location}&quot;
                   FontAttributes=&quot;Italic&quot;
                   HorizontalOptions=&quot;Center&quot; /&gt;--&gt;
            &lt;Image
                HeightRequest=&quot;240&quot;
                HorizontalOptions=&quot;Center&quot;
                Source=&quot;{Binding VARHaute.Image}&quot;
                WidthRequest=&quot;300&quot; /&gt;
            &lt;Image
                Aspect=&quot;AspectFill&quot;
                HeightRequest=&quot;100&quot;
                HorizontalOptions=&quot;Center&quot;
                Source=&quot;{Binding VARHaute.Profil}&quot;
                WidthRequest=&quot;200&quot; /&gt;
            &lt;Label
                HorizontalOptions=&quot;Center&quot;
                Style=&quot;{DynamicResource BodyStyle}&quot;
                Text=&quot;{Binding VARHaute.Distance, StringFormat=&#39;Distance: {0}km&#39;}&quot; /&gt;
            &lt;Label
                HorizontalOptions=&quot;Center&quot;
                Style=&quot;{DynamicResource BodyStyle}&quot;
                Text=&quot;{Binding VARHaute.Denivele, StringFormat=&#39;D&#233;nivel&#233;: {0}m&#39;}&quot; /&gt;
            &lt;Image
                BackgroundColor=&quot;red&quot;
                HeightRequest=&quot;30&quot;
                HorizontalOptions=&quot;Center&quot;
                Source=&quot;hiking.png&quot;
                WidthRequest=&quot;50&quot; /&gt;
            &lt;Image
                BackgroundColor=&quot;{Binding VARHaute.NiveauColor, Converter={StaticResource ColorHexStringToMAUIColorConverter}}&quot;
                HeightRequest=&quot;30&quot;
                HorizontalOptions=&quot;Center&quot;
                Source=&quot;hiking.png&quot;
                WidthRequest=&quot;50&quot; /&gt;

            &lt;Button
                BackgroundColor=&quot;{StaticResource Primary}&quot;
                Command=&quot;{Binding TapCommand}&quot;
                CommandParameter=&quot;{Binding VARHaute.Map}&quot;
                Text=&quot;Map&quot;
                TextColor=&quot;White&quot;
                WidthRequest=&quot;150&quot; /&gt;
        &lt;/StackLayout&gt;

    &lt;/ScrollView&gt;
&lt;/ContentPage&gt;

The converter in its own file

using Microsoft.Maui.Graphics.Converters;
using System.Globalization;

namespace Randoff.Converters;

public class ColorHexStringToMAUIColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string hexColor = (string)value;
        ColorTypeConverter converter = new ColorTypeConverter();

        return (Color)converter.ConvertFromInvariantString(hexColor);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

huangapple
  • 本文由 发表于 2023年8月5日 14:19:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76840386.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定