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?
stringorImageSource?stringwith just filename and it doesn't work.