【问题标题】:C# Xamarin ImageURLC# Xamarin ImageURL
【发布时间】:2018-02-10 23:05:54
【问题描述】:

我有 3 个字段的模型:标题、正文、图片。

 public class Names
    {   [PrimaryKey]
        public string Title { get; set; }
        public string Body { get; set; }
        public string Picture { get; set; }}

“图片”字段是一个字符串值,它是图像 url 的一部分。 所以真正的 URL 看起来像:

Source = Settings.ServerUrl + "/api/File/" + Picture

如何在 xamarin 中将这样的 url 用于 imagecell 的图像源?

当前 XAML 示例:

当前 XAML.CS 示例

namespace Project.Mobile.Client.Portable.Views.Names
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class NamesListPage : ContentPage
    {
        public ObservableCollection<Models.Names> items { get; set; }

    public NewPage()
    {
        items = new ObservableCollection<Models.Names>();
        this.BindingContext = this;
        InitializeComponent();

        // Disabling selection
        Lst.ItemSelected += (sender, e) => {
            ((ListView)sender).SelectedItem = null;
        };

        Lst.Refreshing += (sender, e) => {
            LoadUsersData();
        };
        LoadUsersData();
    }

    public async void LoadUsersData()
    {
        Lst.IsRefreshing = true;

        var names = await App.Database.Names.GetItemsAsync();
        items.Clear();


        foreach (var item in names)
            items.Add(item);

        Lst.IsRefreshing = false;
    }

    public async void OnItemTapped(object sender, ItemTappedEventArgs e)
    {
        NamesReadPage readPage = new NamesReadPage();
        readPage.BindingContext = e.Item as Models.Names;

        await Navigation.PushAsync(readPage);
    }
}

由于代码限制,我将 xaml 代码添加为图像。很抱歉。

【问题讨论】:

  • 要么使用带有扩展完整 URL 的属性的 ViewModel,要么使用 ValueConverter

标签: c# xamarin xamarin.forms


【解决方案1】:

通过IValueConverter

public class PictureURLConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Settings.ServerUrl + "/api/File/" + (string)value;
    }

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

添加 IValueConverter 的命名空间

xmlns:local="clr-namespace:SameNameSpace;assembly=SomeAssemblyName"    

将 IValueConverter 添加到您的 ContentPage.Resources

<ContentPage.Resources>
    <ResourceDictionary>
        <local:PictureURLConverter x:Key="pictureURL"></local:PictureURLConverter>
    </ResourceDictionary>
</ContentPage.Resources>

在绑定中使用转换器

<Image Source="{Binding Picture, Converter={StaticResource pictureURL}}" />

【讨论】:

  • 非常感谢您的详细解释。如果可能的话,我可以请你在你的例子中添加简单的授权admin/admin 吗?
【解决方案2】:

创建一个 viewModel 并更改您的属性以返回您想要的 url 并将此属性绑定到您的 ImageSource

public string PictureUrl
{
    get
    {
        return Settings.ServerUrl + "/api/File/" + this.PictureUrl;
    }
    set
    {
        this.PictureUrl = value;
        OnPropertyChanged("PictureUrl");
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 2016-08-22
    • 2012-12-03
    • 2015-03-30
    相关资源
    最近更新 更多