【问题标题】:C# Xamarin load image from urlC# Xamarin 从 url 加载图像
【发布时间】:2017-04-07 22:43:39
【问题描述】:

我正在尝试从 url 字符串加载图像。下面的代码是一个项目数组,其中Photo 是加载图像的内容。通常我会包含我硬盘中的图像并通过Photo = "image_name.jpg"加载该图像

这是第一个代码:

public MainPageViewModel()
{        
    items.Add(new CardStackView.Item() { Name = "Title 1", Photo = " xxx ", Description = "Desc 1" });
    items.Add(new CardStackView.Item() { Name = "Title 2", Photo = " xxx ", Description = "Desc 2" });
    items.Add(new CardStackView.Item() { Name = "Title 3", Photo = " xxx ", Description = "Desc 3" });
    items.Add(new CardStackView.Item() { Name = "Title 4", Photo = " xxx ", Description = "Desc 4" });
    items.Add(new CardStackView.Item() { Name = "Title 5", Photo = " xxx ", Description = "Desc 5" });
    items.Add(new CardStackView.Item() { Name = "Title 6", Photo = " xxx ", Description = "Desc 6" });
}

我希望xxx 成为我的网址示例https://i.vimeocdn.com/portrait/58832_300x300

这段代码是我显示图像的地方:

Photo = new Image()
{
    InputTransparent = true,
    Aspect = Aspect.Fill,
    Scale = 0.95
};

view.Children.Add(Photo,
                  Constraint.RelativeToParent((parent) => { double w = parent.Width * 1; return ((parent.Width - w) / 2); }),
                  Constraint.Constant(10),
                  Constraint.RelativeToParent((parent) => { return parent.Width; }),
                  Constraint.RelativeToParent((parent) => { return (parent.Height* 0.80); }));  

请注意,这两段代码不是在同一个文件中,而是在两个不同的文件中。

我希望这不是重复的,因为我找不到任何解决问题的方法。

【问题讨论】:

  • 问题或问题是什么?
  • @EvZ "我正在尝试从 url 字符串加载图像。"
  • @TheGijr 那么它有什么问题呢?它会使应用程序崩溃吗?它只是不显示图像吗?更具体。
  • @EvZ 没有我试图找到一种显示图像的方法,只是粘贴一个 url 似乎不起作用?

标签: c# image xamarin xamarin.forms


【解决方案1】:

您需要设置Source 元素的Source

Photo = new Image()
{
    InputTransparent = true,
    Aspect = Aspect.Fill,
    Scale = 0.95,
    Source = "https://your_image_url.com/the_image.png"
};

【讨论】:

    【解决方案2】:

    我推荐你使用Binding,基本上你只需要为Source设置值。尝试以下方法:

      <Image Source="{Binding ImageUrl}" WidthRequest="80" HorizontalOptions="Start" VerticalOptions="Start" />
    

    【讨论】:

      猜你喜欢
      • 2018-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      • 2013-07-23
      • 2020-05-12
      相关资源
      最近更新 更多