首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在平台标签上绑定ToolbarItem上的图像图标

如何在平台标签上绑定ToolbarItem上的图像图标
EN

Stack Overflow用户
提问于 2020-02-03 09:54:30
回答 2查看 2K关注 0票数 1

如何在ToolbarItem OnPlatform标记上绑定Xamarin.Forms中的图像图标

这是我的密码:

代码语言:javascript
复制
<ToolbarItem 
    Icon="{OnPlatform iOS='iconscalendarplus64.png', Android='iconscalendarplus64.png'}" 
    Priority="0"
    Order="Primary" 
    Command="{Binding PrikaziCommand}"

我正试图为Android OnPlatform绑定图像,但它不起作用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-03 12:30:55

因为AndroidiOS属性的OnPlatform不是BindableProperty

它们不支持数据绑定。因此,您不能绑定这些属性。

作为另一种选择,您可以将特定于平台的映像源设置为ViewModel中的一个属性,而不是设置它。

XAML

代码语言:javascript
复制
<ContentPage.ToolbarItems>
    <ToolbarItem IconImageSource="{Binding PlatformSpecificImage}"/>
</ContentPage.ToolbarItems>

视图模型

代码语言:javascript
复制
public ImageSource PlatformSpecificImage { get; set; }

public ViewModel()
{
    if(Device.RuntimePlatform == Device.Android)
    {
        PlatformSpecificImage = "android_image.png";
    }
    else
    {
        PlatformSpecificImage = "iOS_image.png";
    }
}

希望这能帮上忙!

票数 2
EN

Stack Overflow用户

发布于 2020-02-03 10:24:57

最简单的方法是:

代码语言:javascript
复制
                <ToolbarItem.Icon>
                <OnPlatform x:TypeArguments="ImageSource">
                    <OnPlatform.iOS><FileImageSource File=""icon.png""/></OnPlatform.iOS>
                    <OnPlatform.Android><FileImageSource File=""icon.png""/></OnPlatform.Android>
                    <OnPlatform.WinPhone><FileImageSource File=""Images/icon.png""/></OnPlatform.WinPhone>
                </OnPlatform>
                </ToolbarItem.Icon>

好的是您可以在这里使用所有4种类型的基于平台的ImageSource。

如果你有疑问的话,祝你好运。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60036788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档