8

In my .NET MAUI app, I want to be able to set my image source by binding it to a property in my view model -- see below:

public partial class MyViewModel : BaseViewModel
{
   [ObservableProperty]
   ImageSource myImageSource;

   void Init()
   {
        if(somevalue == "something")
           MyImageSource = ImageSource.FromFile("image_a.png");
        else
           MyImageSource = ImageSource.FromFile("image_b.png");
   }
}

And in my XAML page< I do this:

<Image
   Source{Binding MyImageSource}
   HeightRequest="30"
   HorizontalOptions="Center"
   VerticalOptions="Center" />

This doesn't display the image but no errors. In my view model, I also tried MyImageSource = ImageSource.FromResource("MyProject.Resources.Images.image_a.png"); which also doesn't display the image.

The images I want to display are in Resources > Images folder.

What am I doing wrong here? How do I set the image source to a file in a folder under Resources through my view model?

8
  • Just use string. "a.png" is perfectly fine. Commented Mar 10, 2023 at 6:18
  • Tried that but didn't work Commented Mar 10, 2023 at 6:18
  • How can It not work, when I am using it all the time :) Commented Mar 10, 2023 at 6:19
  • What is your property type in the view model? string or ImageSource? Commented Mar 10, 2023 at 6:20
  • Also, do you just use the filename or include the path as well? So far, I tried it as string with just filename and it doesn't work. Commented Mar 10, 2023 at 6:23

2 Answers 2

11

In project:

<MauiImage Include="Resources\Images\*" />

File in folder:

Resources/Images/dotnet_bot.svg

Define:

[ObservableProperty]
ImageSource image;

Use:

Image = "dotnet_bot.png";
Sign up to request clarification or add additional context in comments.

2 Comments

What if the image file is in the cache directory of the device? For example, what if the image file is created like this: var filePath = Path.Combine(FileSystem.CacheDirectory, Path.GetRandomFileName()); using var fileStream = File.Create(filePath);
@dpant You can get image source from file, uri or stream. learn.microsoft.com/en-us/dotnet/api/…
2

A method is to set your images's Build Action to Embedded resource and set value for MyImageSource in your view Model constructor,just as follows:

public partial  class MyViewModel:ObservableObject 
    {
        [ObservableProperty]
        ImageSource myImageSource;
    
        public MyViewModel()
        {
            MyImageSource = ImageSource.FromResource("YourAppName.Resources.Images.test.png", typeof(MyViewModel).GetTypeInfo().Assembly); 
        }
       
        }
    }

And bind like this:

<Image   Source="{Binding MyImageSource }" Aspect="AspectFill" WidthRequest="50"   HeightRequest="50" />

Note:

1.YourAppName is the name of the app,you can change it to yours.

2.Please change your images' Build Action to Embedded resource

2 Comments

Where do we find the App Name in the project?
In csproj file. In your solution, double click the name of your project. It will open the csproj for that project. Then you can see ApplicationTitle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.