【问题标题】:Image source from disk Xamarin Forms来自磁盘 Xamarin Forms 的图像源
【发布时间】:2017-08-23 01:08:29
【问题描述】:

我有一个 Xamarin Forms 应用程序,我想做的是将图像视图的源设置为 UWP 磁盘中的特定图像。 也就是说,我的磁盘中有我的图像的绝对路径,我想以编程方式设置图像视图的来源。 我尝试过使用

image.Source = ImageSource.FromFile(filePath);

但它在 UWP 中不起作用,尽管它在 Android 中起作用。

【问题讨论】:

标签: image file xamarin uwp


【解决方案1】:

做你想做的事的好方法是在你的ViewModel 中使用一种方法来放置基于操作系统的路径

查看

<Image Source="{Binding ImagePath}" />

ViewModel 示例

public string ImagePath
{
    get
    {
        return _path;
    }
    set
    {
        if (_path != value)
        {
            _path = value;
            OnPropertyChanged("ImagePath");
        }
    }
}    
private string _path;

当您为图像设置路径时,您可以使用这样的函数:

public static class OSHelpers {
    /// <summary>
    /// Sets the os image path.
    /// </summary>
    /// <param name="ImageName">Name of the image.</param>
    /// <returns>System.String.</returns>
    public static string SetOSImagePath(string ImageName) {
        string rtn = ImageName;

        if (Device.OS == TargetPlatform.Windows || Device.OS == TargetPlatform.WinPhone) {
            rtn = "Images/" + ImageName;
        }

        return rtn;
    }
}

【讨论】:

    猜你喜欢
    • 2019-08-07
    • 2021-08-25
    • 2019-03-18
    • 1970-01-01
    • 2013-04-11
    • 2018-01-28
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多