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

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

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

问题

    <SearchBar x:Name="searchBar"
                   Placeholder="搜索"
                   TextChanged="OnSearchTextChanged"
                   TextColor="{DynamicResource NoteTextDark}">
                <SearchBar.BackgroundColor>
                    <OnPlatform x:TypeArguments="Color">
                            <On Platform="Android" Value="{DynamicResource NotesEditorColor}" />
                    </OnPlatform>      
                </SearchBar.BackgroundColor>
</SearchBar>
英文:
<SearchBar x:Name="searchBar"
               Placeholder="Search"
               TextChanged="OnSearchTextChanged"
               TextColor="{DynamicResource NoteTextDark}">
            <SearchBar.BackgroundColor>
                <OnPlatform x:TypeArguments="Color">
                        <On Platform="Android" Value="{DynamicResource NotesEditorColor}" />
                </OnPlatform>      
            </SearchBar.BackgroundColor>

</SearchBar>

Above code throws an invalid cast error on DynamicResource NotesEditorColor

Also, it works for DynamicResource NoteTextDark without any problem

答案1

得分: 0

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

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:

确定