Image source in runtime

Dani_S 4,501 Reputation points
2024-08-11T12:05:47.1866667+00:00

Hi,

How i fill in view model this source.

Please supply the code, net 8 maui.

<Image Grid.Row="0" Grid.ColumnSpan="3" Source="{Binding LogoImage}" WidthRequest="250" HeightRequest="110" HorizontalOptions="Start" VerticalOptions="Center"/>

Developer technologies .NET .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2024-08-12T05:46:32.17+00:00

    Hello,

    The Source property of the Image control in Maui corresponds to the ImageSource class. In ViewModel, you can refer to Load a local image to load Source in the following way.

    private ImageSource logoImage;
    public ImageSource LogoImage
    {
        get
        {
            return logoImage;
        }
        set
        {
            if (logoImage != value)
            {
                logoImage = value;
                OnPropertyChanged(); // reports this property
            }
        }
    }
    //You can assign values ​​directly using the file path
    LogoImage = "test.png";
    LogoImage = ImageSource.FromFile("test.png");
    
    // You can assign a value via a Uri, which is usually suitable for remote files.
    LogoImage = ImageSource.FromUri(new Uri("https://aka.ms/campus.jpg"));
    
    // If your image is a Stream read from a file, you can convert it to an ImageSource in this way;
    LogoImage = Source = ImageSource.FromStream(() => your_file_stream);
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Dani_S 4,501 Reputation points
    2024-08-13T12:30:56.6966667+00:00

    You can close this ticket it Solved-:)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.