If the images folder is outside the root folder, you need to tell your code as well -
You first need to set your images Build Action to Embedded resource and set value for 'MyImageSource' in your view model constructor.
In your project -
<MauiImage Include="Resources\Images\*" />
The file in your image folder will be -
Quote:
Resources/Images/infinix2.jpg
Define the image source -
[ObservableProperty]
ImageSource image;
Now use the image -
Image = "infinix2.png";
As a summary, your code will look like -
public partial class MyViewModel:ObservableObject
{
[ObservableProperty]
ImageSource myImageSource;
public MyViewModel()
{
MyImageSource = ImageSource.FromResource("YourAppName.Resources.Images.infinix2.png", typeof(MyViewModel).GetTypeInfo().Assembly);
}
}
}
You can now bind the image-
<Image Source="{Binding MyImageSource }" Aspect="AspectFill" WidthRequest="50" HeightRequest="50" />
Read up more on image path -
Default image directory on Windows[
^]
Above solution found on SO -
Setting image source in view model in .NET MAUI app[
^]