来自磁盘Xamarin Forms的图像源

时间:2017-03-29 13:44:06

标签: image file xamarin uwp

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

image.Source = ImageSource.FromFile(filePath);

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

1 个答案:

答案 0 :(得分:0)

执行所需操作的好方法是在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;
    }
}