Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Setting Icon File Names as Resources

$
0
0

I was trying to get all of my icon file definitions in one place so that it would be easy to swap icon sets in the future. Since I use XAML for my page definitions, I thought I'd make a ResourceDictionary with all of my icons. Just trying to get it to work, I put one in my App.xaml file:

<?xml version="1.0" encoding="utf-8"?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:MyApp"
    x:Class="MyApp.App">

    <Application.Resources>
        <ResourceDictionary>       
            <x:String x:Key="SettingsIcon">"outline_settings_white_48.png"</x:String>
            <!-- rest of file deleted for brevity -->
        </ResourceDictionary>
    </Application.Resources>
</Application>

Then in my xaml, I access it like this:

<ImageButton
    x:Name="SettingsButton"
    Source="{StaticResource SettingsIcon}"
    Clicked="GoSettings"/>

This generates a run-time error (in the application output window):

Could not load image named: {0}: "outline_settings_white_48.png"
FileImageSourceHandler: Could not find image or image file was invalid: File: "outline_settings_white_48.png"

... and of course the icon is not displayed.

If I reference the icon directly, it works just fine:

<ImageButton
    x:Name="SettingsButton"
    Source="outline_settings_white_48.png"
    Clicked="GoSettings"/>

I have tried it without the .png extension, and got the same result.

What am I doing wrong?

Thanks!

P.S. Right now I only need it to work on Android.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>