【问题标题】:Binding a URL to custom SVG container's BindableProperty将 URL 绑定到自定义 SVG 容器的 BindableProperty
【发布时间】:2019-10-10 17:12:11
【问题描述】:

我一直在使用代码布局 here 为 Xamarin Forms 项目创建 SVG 容器。它运行良好,我将其重新配置为从远程 URL 读取图像。

ResourceId 来自上述来源:

        public static readonly BindableProperty SvgUriProperty = BindableProperty.Create(
            "ResourceId",
            typeof(string),
            typeof(SvgUriContainer),
            default(string),
            propertyChanged: RedrawCanvas);


        public string ResourceId
        {
            get => (string)GetValue(SvgUriProperty);
            set => SetValue(SvgUriProperty, value);
        }

但是我似乎无法在运行时在 XAML 中绑定该 URL:

    <control:SvgUriContainer 
        Grid.Row="0"
        Grid.RowSpan="4"
        Grid.Column="0"
        ResourceId="{Binding StampUri}" 
        [...]
    />

该绑定返回一个字符串,其余绑定工作正常。任何以这种方式绑定的尝试都会导致构建错误:

No property, bindable property, or event found for 'ResourceId', or mismatching type between value and property.

无论我在容器逻辑中使用BindablePropertycreation 做什么,错误都是相同的,并且在 XAML 中。我在 XAML 中的语法错误吗?是因为Binding 在构建时是BindingExtension(不是字符串)吗?

注意:如果我将 Binding 替换为字符串/URL,则一切正常。

注意:如果我将ResourceId 的类型设置为object,我可以得到构建错误但字符串无法解析,当然。

【问题讨论】:

  • 分享 ResourceID 属性的代码会很有帮助
  • 已编辑。应该包括它。
  • 很高兴看到我的博客文章实际上在帮助某人! :) 您共享的代码对我来说看起来不错,甚至可以在我这边编译。你能分享更多的代码吗?例如您的视图模型、控件(如果您进行了任何更改)或 xaml 本身?
  • @AlexPshul 很酷 - 是的,很棒的博文!我不能真正共享代码,但是我在旁边建立了一个项目来隔离代码。今晚我会尝试复制并分享源代码。
  • @AlexPshul 我能够让它在一个更简单的项目中正常工作。对于笑容和任何感兴趣的人,我推了它here。基本上是您的 SvgFx 代码的稍微修改的克隆。我将不得不稍微重构我的代码以使绑定工作......

标签: xamarin.forms


【解决方案1】:

您能否尝试将您的 bindableProperty 修改为document 中描述的标准格式:

  public static readonly BindableProperty SvgUriProperty = BindableProperty.Create(
      "SvgUri",
      typeof(string),
      typeof(EventToCommandBehavior),
      "test",
      propertyChanged: RedrawCanvas);


    public string SvgUri
    {
        get => (string)GetValue(SvgUriProperty);
        set => SetValue(SvgUriProperty, value);
    }

Xamarin 需要属性命名约定。

【讨论】:

  • 试过了,虽然我的绑定问题在其他地方。不过,感谢命名约定指针!
  • 如果您可以分享您的项目,我可以为您测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多