【问题标题】:Xamarin android fail to load imagesXamarin android无法加载图像
【发布时间】:2018-03-08 09:46:17
【问题描述】:

我是 Xamarin 的新开发人员。我正在尝试构建一个简单的按钮图像,问题是 图像,它没有出现在我的手机中。 我还使用 VS2017 和我的手机实时调试 xamarin。 当我实时构建和运行时,没有任何警告或错误。所以很难看出哪里出了问题。

到目前为止我做了什么:

我检查了 Android 文件夹资源中的图像,并检查了图标的尺寸是否正确。

我执行以下步骤:清理解决方案,重建解决方案,构建解决方案并重新启动 VS2017

App.xaml.cs:

public App ()
        {
            InitializeComponent();

            MainPage = new MainPage();
        }

MainPage.xaml:

<StackLayout VerticalOptions="Center" Spacing="50">

        <Button Text="Hello" BackgroundColor="Wheat" BorderRadius="20" TextColor="Black">
            <Button.Image>
                <OnPlatform x:TypeArguments="FileImageSource" Android="icon.png"/>
            </Button.Image>
        </Button>

        <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
            <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand">
                <Button Text="Left" BackgroundColor="IndianRed" BorderRadius="22" TextColor="Black">
                    <Button.Image>
                        <OnPlatform x:TypeArguments="FileImageSource" Android="icon.png"/>
                    </Button.Image>
                </Button>
            </StackLayout>
            <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand">
                <Button Text="Right" BackgroundColor="IndianRed" BorderRadius="22" TextColor="Black">
                    <Button.Image>
                        <OnPlatform x:TypeArguments="FileImageSource" Android="icon.png"/>
                    </Button.Image>
                </Button>

            </StackLayout>

        </StackLayout>
    </StackLayout>

我加载了默认 icon.png 并检查了 hdpi(72x72)、mdpi(48x48)、xhdpi(96x96)、xxhdpi(144x144)、xxxhdpi(192x192) 的尺寸是否正确。

我看到按钮位置正确,里面有文字但没有图像。有什么问题?

【问题讨论】:

    标签: android xamarin


    【解决方案1】:

    在你的 xaml 中试试这个:

    <Button Image="icon.png" Text="Hello" BackgroundColor="Wheat" BorderRadius="20" TextColor="Black"/>
    

    或者在你的代码中:

    yourButton.Image = "icon.png";
    

    yourButton.Image = ImageSource.FromFile("icon.png");
    

    【讨论】:

    • 不,是一样的。我看到 xamarin live 有问题,因为在 xamarin.forms 预览中我可以看到图标。
    • 是的,在我的设备上有效。只有在 Xamarin live 中它没有。我不知道为什么。
    • 您的问题中的代码在 Xamarin Live 中运行?
    • Not mine and not yours in Xamarin Live only on my physical device and xamarin forms preview.
    猜你喜欢
    • 2020-02-16
    • 2022-01-20
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 2017-08-03
    相关资源
    最近更新 更多