How to refer to Embedded Resources from XAML?

前端 未结 4 706
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 16:52

I have several images that i want to be Embedded into the exe.

When i set the Build Action to Embedded Resource I get through out t

相关标签:
4条回答
  • 2020-12-03 17:32

    Set the build action to Resource, not Embedded Resource

    0 讨论(0)
  • 2020-12-03 17:33

    Just for those using xamarin forms and bump into this question, this can be done by creating a custom xaml markup extension explained here:

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows

    in the "Embedded Images"->"Using XAML" section

    Citation of custom extension

    [ContentProperty (nameof(Source))]
    public class ImageResourceExtension : IMarkupExtension
    {
     public string Source { get; set; }
    
     public object ProvideValue (IServiceProvider serviceProvider)
     {
       if (Source == null)
       {
         return null;
       }
    
       // Do your translation lookup here, using whatever method you require
       var imageSource = ImageSource.FromResource(Source, typeof(ImageResourceExtension).GetTypeInfo().Assembly);
    
       return imageSource;
     }
    }
    

    citation of how to use

    <?xml version="1.0" encoding="UTF-8" ?>
    <ContentPage
       xmlns="http://xamarin.com/schemas/2014/forms"
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
       xmlns:local="clr-namespace:WorkingWithImages;assembly=WorkingWithImages"
       x:Class="WorkingWithImages.EmbeddedImagesXaml">
     <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
       <!-- use a custom Markup Extension -->
       <Image Source="{local:ImageResource WorkingWithImages.beach.jpg}" />
     </StackLayout>
    </ContentPage>
    
    0 讨论(0)
  • 2020-12-03 17:34

    When you set the BuildAction to Resource it goes as embedded resource in an assembly. Or you can set BuildAction to Content then it will bundled into the resulting .xap file. You can use any one of these BuildActions. By setting BuildAction to Content you can access Image like: "/Resources/Images/darkaurora.png" (must begin with slash). And when you use the BuildAction Resource then you can access image as "/YearBook;component/Resources/Images/darkaurora.png" (assemblyname;component/relativepath). Hope this will help.

    0 讨论(0)
  • 2020-12-03 17:36

    ImageSource cannot be instantiated.

    public abstract class ImageSource : Animatable, 
    IFormattable
    

    There's that little abstract in there which will screw your day up. Your xaml is actually trying to instantiate an instance of ImageSource, then assign the value within the element (your Uri, in this case) to a property marked with the ContentPropertyAttribute (??) using whatever converter that could be located to convert the string to an object (again, ??).

    I think you want a BitmapSource.

    <BitmapImage 
        x:Key="Image_Background" 
        UriSource="/Images/darkaurora.png" />
    
    0 讨论(0)
提交回复
热议问题