运行时的 Image Source

Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,441 信誉分 Microsoft 外部员工
2024-08-16T06:04:32.0133333+00:00

你好

 

我如何填写视图模型这个源。

 

请提供代码,net 8 maui。

 


 

谢谢

 

此问题整理于:[https://learn.microsoft.com/en-us/answers/questions/1860486/image-source-in-runtime](https://learn.microsoft.com/en-us/answers/questions/1860486/image-source-in-runtime"https://learn.microsoft.com/en-us/answers/questions/1860486/image-source-in-runtime")
开发人员技术 | .NET | .NET MAUI
0 个注释 无注释
{count} 票

1 个答案

排序依据: 非常有帮助
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,131 信誉分 Microsoft 外部员工
    2024-08-16T06:34:47.6966667+00:00

    你好,

    Maui 中 Image 控件的 Source 属性对应于 ImageSource 类。在 ViewModel 中,您可以参考 Load a local image 来按照以下方式加载 Source。

    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);
    

    如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。 注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。

    0 个注释 无注释

你的答案

问题作者可以将答案标记为“接受的答案”,这有助于用户了解已解决作者问题的答案。