首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin在图像上形成URL绑定

Xamarin在图像上形成URL绑定
EN

Stack Overflow用户
提问于 2019-01-30 10:32:33
回答 2查看 851关注 0票数 0

我的xaml中有一个图片,其中的源是来自json的Iam binding.The URL将是这样的一个URL :binding.The我的公共值中的URL存储为CommonValues.URL。在绑定时,如何在json之前添加这个"CommonValues.URL“呢?所以图像的来源将是http://example.com//images/Uploads/e0111.png

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-30 10:55:47

如果你需要Uri

代码语言:javascript
复制
var myUrl= new Uri(CommonValues.URL + "images/Uploads/e0111.png");

如果string

代码语言:javascript
复制
var myUrl=CommonValues.URL + "images/Uploads/e0111.png";

或者你可以在你的ViewModelPage中这样做

代码语言:javascript
复制
public string Url => string.Format("{0}{1}", CommonValues.URL,"/images/Uploads/e0111.png");

然后在XAML中:

代码语言:javascript
复制
<Button Text="{Binding Url}"/>
票数 1
EN

Stack Overflow用户

发布于 2019-01-30 12:47:08

您可以使用转换器,这将允许您在所有视图/应用程序中重用

代码语言:javascript
复制
public class UrlConverter : IValueConverter
{
    #region IValueConverter implementation

    public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var test = value as string; 
        if (!string.IsNullOrEmpty(test))  
        {
            return CommonValues.URL + test;
        }
        return false;
    }

    public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException ();
    }

    #endregion
}

然后,在你的页面中:

代码语言:javascript
复制
<ContentPage.Resources>
    <ResourceDictionary>
        <converter:UrlConverter x:Key="UrlConverter" />
    </ResourceDictionary>
</ContentPage.Resources>

<Image Source="{Binding YourProperty, Converter={StaticResource UrlConverter}}"/>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54438467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档