【问题标题】:Xamarin forms URL binding on ImageXamarin 在图像上形成 URL 绑定
【发布时间】:2019-01-30 10:32:33
【问题描述】:

我的 xaml 中有一个图像,其中源是我绑定的 URL。来自 json 的 URL 将如下所示:“/images/Uploads/e0111.png”。我的通用值存储类中的 URL 为 CommonValues.URL。如何在绑定时在 json 之前添加这个“CommonValues.URL”?这样 Image 的来源就是http://example.com//images/Uploads/e0111.png.?

【问题讨论】:

    标签: xaml xamarin xamarin.forms


    【解决方案1】:

    如果你需要Uri

    var myUrl= new Uri(CommonValues.URL + "images/Uploads/e0111.png");
    

    如果string

    var myUrl=CommonValues.URL + "images/Uploads/e0111.png";
    

    或者你可以在ViewModelPage 中这样做

    public string Url => string.Format("{0}{1}", CommonValues.URL,"/images/Uploads/e0111.png");
    

    然后在 XAML 中:

    <Button Text="{Binding Url}"/>
    

    【讨论】:

    • 兄弟我如何在 xaml 中做到这一点?
    • 在您的视图模型中创建一个公共属性myUrl,然后将其传递给 xaml 文件。看来你不能从 xaml 做到这一点。
    • 让我检查一下兄弟
    【解决方案2】:

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

    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
    }
    

    然后,在您的页面中:

    <ContentPage.Resources>
        <ResourceDictionary>
            <converter:UrlConverter x:Key="UrlConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>
    
    <Image Source="{Binding YourProperty, Converter={StaticResource UrlConverter}}"/>
    

    【讨论】:

      猜你喜欢
      • 2018-06-25
      • 2016-12-16
      • 2018-07-27
      • 2016-09-25
      • 2023-04-04
      • 1970-01-01
      • 2016-08-28
      • 2016-01-28
      • 2018-07-14
      相关资源
      最近更新 更多