如何在使用动态资源时在OnPlatform中设置搜索栏的背景颜色?

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

How to set the background colour of searchbar in OnPlatform using dynamic resource?

问题

  1. <SearchBar x:Name="searchBar"
  2. Placeholder="搜索"
  3. TextChanged="OnSearchTextChanged"
  4. TextColor="{DynamicResource NoteTextDark}">
  5. <SearchBar.BackgroundColor>
  6. <OnPlatform x:TypeArguments="Color">
  7. <On Platform="Android" Value="{DynamicResource NotesEditorColor}" />
  8. </OnPlatform>
  9. </SearchBar.BackgroundColor>
  10. </SearchBar>
英文:
  1. <SearchBar x:Name="searchBar"
  2. Placeholder="Search"
  3. TextChanged="OnSearchTextChanged"
  4. TextColor="{DynamicResource NoteTextDark}">
  5. <SearchBar.BackgroundColor>
  6. <OnPlatform x:TypeArguments="Color">
  7. <On Platform="Android" Value="{DynamicResource NotesEditorColor}" />
  8. </OnPlatform>
  9. </SearchBar.BackgroundColor>

</SearchBar>

Above code throws an invalid cast error on DynamicResource NotesEditorColor

Also, it works for DynamicResource NoteTextDark without any problem

答案1

得分: 0

  1. if (DeviceInfo.Platform == DevicePlatform.Android)
  2. {
  3. searchBar.SetDynamicResource(SearchBar.BackgroundColorProperty, "NotesEditorColor");
  4. }
英文:
  1. if (DeviceInfo.Platform == DevicePlatform.Android)
  2. {
  3. searchBar.SetDynamicResource(SearchBar.BackgroundColorProperty, &quot;NotesEditorColor&quot;);
  4. }

Instead of the xaml, writing this in the xaml.cs code behind file worked

huangapple
  • 本文由 发表于 2023年6月13日 03:50:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459867.html
匿名

发表评论

匿名网友

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

确定