【问题标题】:Images from web not showing after update网络图片更新后不显示
【发布时间】:2019-08-19 02:16:51
【问题描述】:

今天我已将 Xamarin Forms 更新到 4.1 版本,并且不再加载来自 Web 的图像。我尝试重新启动 VS(对于 mac),清理解决方案并重建它,从我的手机中完全删除应用程序,甚至重新启动设备,但似乎没有任何效果。

这是 XAML 标记

  <ListView
     x:Name="campaignsListView"
     HasUnevenRows="True"
     RelativeLayout.WidthConstraint="{ConstraintExpression
         Type=RelativeToParent,
         Property=Width,
         Factor=1}">
     <ListView.ItemTemplate>
         <DataTemplate>
             <ViewCell>
                 <Frame CornerRadius="10"
                     RelativeLayout.WidthConstraint="{ConstraintExpression
                         Type=RelativeToParent,
                         Property=Width,
                         Factor=1}">
                        <StackLayout Orientation="Vertical">
                            <Image
                                BackgroundColor="Aqua"
                                HeightRequest="150"
                                HorizontalOptions="FillAndExpand"
                                Source="{Binding CampaignImage}"></Image>
                            <Label

                             HorizontalOptions="CenterAndExpand"
                             Text="{Binding CampaignImage}"></Label>
                        </StackLayout>
                     </Frame>
             </ViewCell>
         </DataTemplate>
     </ListView.ItemTemplate>
 </ListView>

我不相信任何绑定错误正在发生,因为我最终出于调试目的,将图像 URL 绑定到标签并且它工作正常

我正在尝试加载的图片 https://www.editorajuspodivm.com.br/cdn/imagens/produtos/original/produto-teste-marcador-de-paginas-1154410cb043c754d9e2ada9fed04650.png

编辑: 我注意到我的 VS 更新来自预览频道,我更改为稳定频道,进行了所有更新,现在一切都很好了。作为预览的构建,我相信可能还有一些问题

【问题讨论】:

  • 你在哪里以及如何设置ItemsSource

标签: xaml xamarin xamarin.forms


【解决方案1】:

CampaignImage 必须设置为 uri CampaignImageUri

CampaignImageUri = 新 Uri(CampaignImage):

【讨论】:

  • 我注意到我的 VS 更新来自预览频道,我切换到稳定频道,完成了所有更新,现在一切正常
【解决方案2】:

我创建了一个演示来使用 Xamarin.forms 4.1 测试您的代码,它运行良好。 这是代码。

ImageViewModel.cs

class ImageViewModel : INotifyPropertyChanged
{
    public string _image;
    public string image
    {
        get
        {
            return _image;
        }
        set
        {
            if (_image != value)
            {
                _image = value;
                NotifyPropertyChanged();
            }
        }
    }
    public string _imageName;
    public string imageName
    {
        get
        {
            return _imageName;
        }
        set
        {
            if (_imageName != value)
            {
                _imageName = value;
                NotifyPropertyChanged();
            }
        }
    }


    protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

page.xaml.cs

ObservableCollection<ImageViewModel> imageViewModels = new 
ObservableCollection<ImageViewModel>();

    public Page1()
    {
        InitializeComponent();
        imageViewModels.Add(new ImageViewModel
        {
            image = "https://www.editorajuspodivm.com.br/cdn/imagens/produtos/original/produto-teste-marcador-de-paginas-1154410cb043c754d9e2ada9fed04650.png",
            imageName = "Image1"
        });

        campaignsListView.ItemsSource = imageViewModels;
    }

xaml 与您的相同。

<StackLayout Orientation="Vertical">
     <Image BackgroundColor="Aqua" HeightRequest="150"      
            HorizontalOptions="FillAndExpand" Source="{Binding image}"></Image>
     <Label HorizontalOptions="CenterAndExpand" Text="{Binding imageName}"></Label>
</StackLayout> 

注意:将图像 url 定义为字符串类型。

【讨论】:

  • 我相信我在更新过程中出了点问题,因为没有使用像这样的东西&lt;Image HeightRequest="150" HorizontalOptions="FillAndExpand" Source="https://www.editorajuspodivm.com.br/cdn/imagens/produtos/original/produto-teste-marcador-de-paginas-1154410cb043c754d9e2ada9fed04650.png"&gt;&lt;/Image&gt;我只得到一个空白区域
  • 我注意到我的 VS 更新来自预览频道,我切换到稳定频道,完成了所有更新,现在一切正常
猜你喜欢
  • 2018-02-19
  • 2020-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-28
  • 2018-12-18
  • 1970-01-01
  • 2020-08-13
相关资源
最近更新 更多