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

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

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; }

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

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

  1. <ContentPage.Resources>
  2. <ResourceDictionary>
  3. <converters:ColorHexStringToMAUIColorConverter x:Key="ColorHexStringToMAUIColorConverter" />
  4. </ResourceDictionary>
  5. </ContentPage.Resources>

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

  1. public class ColorHexStringToMAUIColorConverter : IValueConverter
  2. {
  3. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  4. {
  5. string hexColor = (string)value;
  6. ColorTypeConverter converter = new ColorTypeConverter();
  7. return (Color)converter.ConvertFromInvariantString(hexColor);
  8. }
  9. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  10. {
  11. throw new NotImplementedException();
  12. }
  13. }

更新

完整的ContentPage如下:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <ContentPage
  3. x:Class="Randoff.Views.VARHauteDetailPage"
  4. xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  5. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  6. xmlns:converters="clr-namespace:Randoff.Converters"
  7. Title="VAR-Haute">
  8. <ContentPage.Resources>
  9. <ResourceDictionary>
  10. <converters:ColorHexStringToMAUIColorConverter x:Key="ColorHexStringToMAUIColorConverter" />
  11. </ResourceDictionary>
  12. </ContentPage.Resources>
  13. <ScrollView>
  14. <StackLayout Margin="0">
  15. <Label
  16. HorizontalOptions="Center"
  17. Style="{DynamicResource TitleStyle}"
  18. Text="{Binding VARHaute.Name}" />
  19. <!--
  20. <Label Text="{Binding VARHaute.Location}"
  21. FontAttributes="Italic"
  22. HorizontalOptions="Center" />
  23. -->
  24. <Image
  25. HeightRequest="240"
  26. HorizontalOptions="Center"
  27. Source="{Binding VARHaute.Image}"
  28. WidthRequest="300" />
  29. <Image
  30. Aspect="AspectFill"
  31. HeightRequest="100"
  32. HorizontalOptions="Center"
  33. Source="{Binding VARHaute.Profil}"
  34. WidthRequest="200" />
  35. <Label
  36. HorizontalOptions="Center"
  37. Style="{DynamicResource BodyStyle}"
  38. Text="{Binding VARHaute.Distance, StringFormat='Distance: {0}km'}" />
  39. <Label
  40. HorizontalOptions="Center"
  41. Style="{DynamicResource BodyStyle}"
  42. Text="{Binding VARHaute.Denivele, StringFormat='Dénivelé: {0}m'}" />
  43. <Image
  44. BackgroundColor="red"
  45. HeightRequest="30"
  46. HorizontalOptions="Center"
  47. Source="hiking.png"
  48. WidthRequest="50" />
  49. <Image
  50. BackgroundColor="{Binding VARHaute.NiveauColor, Converter={StaticResource ColorHexStringToMAUIColorConverter}}"
  51. HeightRequest="30"
  52. HorizontalOptions="Center"
  53. Source="hiking.png"
  54. WidthRequest="50" />
  55. <Button
  56. BackgroundColor="{StaticResource Primary}"
  57. Command="{Binding TapCommand}"
  58. CommandParameter="{Binding VARHaute.Map}"
  59. Text="Map"
  60. TextColor="White"
  61. WidthRequest="150" />
  62. </StackLayout>
  63. </ScrollView>
  64. </ContentPage>

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

  1. using Microsoft.Maui.Graphics.Converters;
  2. using System.Globalization;
  3. namespace Randoff.Converters
  4. {
  5. public class ColorHexStringToMAUIColorConverter : IValueConverter
  6. {
  7. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  8. {
  9. string hexColor = (string)value;
  10. ColorTypeConverter converter = new ColorTypeConverter();
  11. return (Color)converter.ConvertFromInvariantString(hexColor);
  12. }
  13. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  14. {
  15. throw new NotImplementedException();
  16. }
  17. }
  18. }
英文:

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.

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

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

  1. &lt;ContentPage.Resources&gt;
  2. &lt;ResourceDictionary&gt;
  3. &lt;converters:ColorHexStringToMAUIColorConverter x:Key=&quot;ColorHexStringToMAUIColorConverter&quot; /&gt;
  4. &lt;/ResourceDictionary&gt;
  5. &lt;/ContentPage.Resources&gt;

We make a simple converter like so:

  1. public class ColorHexStringToMAUIColorConverter : IValueConverter
  2. {
  3. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  4. {
  5. string hexColor = (string)value;
  6. ColorTypeConverter converter = new ColorTypeConverter();
  7. return (Color)converter.ConvertFromInvariantString(hexColor);
  8. }
  9. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  10. {
  11. throw new NotImplementedException();
  12. }
  13. }

UPDATE

The comeplete ContentPage

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
  2. &lt;ContentPage
  3. x:Class=&quot;Randoff.Views.VARHauteDetailPage&quot;
  4. xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
  5. xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
  6. xmlns:converters=&quot;clr-namespace:Randoff.Converters&quot;
  7. Title=&quot;VAR-Haute&quot;&gt;
  8. &lt;ContentPage.Resources&gt;
  9. &lt;ResourceDictionary&gt;
  10. &lt;converters:ColorHexStringToMAUIColorConverter x:Key=&quot;ColorHexStringToMAUIColorConverter&quot; /&gt;
  11. &lt;/ResourceDictionary&gt;
  12. &lt;/ContentPage.Resources&gt;
  13. &lt;ScrollView&gt;
  14. &lt;StackLayout Margin=&quot;0&quot;&gt;
  15. &lt;Label
  16. HorizontalOptions=&quot;Center&quot;
  17. Style=&quot;{DynamicResource TitleStyle}&quot;
  18. Text=&quot;{Binding VARHaute.Name}&quot; /&gt;
  19. &lt;!--&lt;Label Text=&quot;{Binding VARHaute.Location}&quot;
  20. FontAttributes=&quot;Italic&quot;
  21. HorizontalOptions=&quot;Center&quot; /&gt;--&gt;
  22. &lt;Image
  23. HeightRequest=&quot;240&quot;
  24. HorizontalOptions=&quot;Center&quot;
  25. Source=&quot;{Binding VARHaute.Image}&quot;
  26. WidthRequest=&quot;300&quot; /&gt;
  27. &lt;Image
  28. Aspect=&quot;AspectFill&quot;
  29. HeightRequest=&quot;100&quot;
  30. HorizontalOptions=&quot;Center&quot;
  31. Source=&quot;{Binding VARHaute.Profil}&quot;
  32. WidthRequest=&quot;200&quot; /&gt;
  33. &lt;Label
  34. HorizontalOptions=&quot;Center&quot;
  35. Style=&quot;{DynamicResource BodyStyle}&quot;
  36. Text=&quot;{Binding VARHaute.Distance, StringFormat=&#39;Distance: {0}km&#39;}&quot; /&gt;
  37. &lt;Label
  38. HorizontalOptions=&quot;Center&quot;
  39. Style=&quot;{DynamicResource BodyStyle}&quot;
  40. Text=&quot;{Binding VARHaute.Denivele, StringFormat=&#39;D&#233;nivel&#233;: {0}m&#39;}&quot; /&gt;
  41. &lt;Image
  42. BackgroundColor=&quot;red&quot;
  43. HeightRequest=&quot;30&quot;
  44. HorizontalOptions=&quot;Center&quot;
  45. Source=&quot;hiking.png&quot;
  46. WidthRequest=&quot;50&quot; /&gt;
  47. &lt;Image
  48. BackgroundColor=&quot;{Binding VARHaute.NiveauColor, Converter={StaticResource ColorHexStringToMAUIColorConverter}}&quot;
  49. HeightRequest=&quot;30&quot;
  50. HorizontalOptions=&quot;Center&quot;
  51. Source=&quot;hiking.png&quot;
  52. WidthRequest=&quot;50&quot; /&gt;
  53. &lt;Button
  54. BackgroundColor=&quot;{StaticResource Primary}&quot;
  55. Command=&quot;{Binding TapCommand}&quot;
  56. CommandParameter=&quot;{Binding VARHaute.Map}&quot;
  57. Text=&quot;Map&quot;
  58. TextColor=&quot;White&quot;
  59. WidthRequest=&quot;150&quot; /&gt;
  60. &lt;/StackLayout&gt;
  61. &lt;/ScrollView&gt;
  62. &lt;/ContentPage&gt;

The converter in its own file

  1. using Microsoft.Maui.Graphics.Converters;
  2. using System.Globalization;
  3. namespace Randoff.Converters;
  4. public class ColorHexStringToMAUIColorConverter : IValueConverter
  5. {
  6. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  7. {
  8. string hexColor = (string)value;
  9. ColorTypeConverter converter = new ColorTypeConverter();
  10. return (Color)converter.ConvertFromInvariantString(hexColor);
  11. }
  12. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  13. {
  14. throw new NotImplementedException();
  15. }
  16. }

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:

确定