【问题标题】:Xamarin Forms ConverterParameter XAML error Invalid Property Path SyntaxXamarin Forms ConverterParameter XAML 错误无效的属性路径语法
【发布时间】:2021-06-26 23:00:04
【问题描述】:

我的 XAML 中有如下文本绑定:

Text="{Binding Converter={StaticResource ColumnToCaption}, ConverterParameter=-1}"

它只是获取列号并根据我存储在集合中的数据查找该列的标题应该是什么。这一直运行良好,但在最后一组升级到 Visual Studio / Xamarin 表单后,XAML 智能感知开始生成错误“无效的属性值”。我没想太多,因为该应用程序似乎可以工作。然后我的应用程序中开始出现一些奇怪的事情,我注意到在我的错误列表中,我列出了 43 个“无效的属性值”错误(基本上每个使用 ConverterParameter 的地方都有一个错误)。

我仔细检查了 MS 文档以确保没有任何变化(此链接 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters),它仍然显示将值作为数字传递是可以的。以下是 MS 文档中的示例:

 <Label Text="{Binding Red, Converter={StaticResource doubleToInt}, ConverterParameter=255,
     StringFormat='Red = {0:X2}'}" />

我只是想将其排除为我的问题的根源,但我似乎无法找出如何让错误消失。任何帮助将不胜感激。

哦,这里是转换器代码,您可以看到它:

public class ColumnToCaption : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string ls_ReturnValue = ""; 
        try
        {
            if (System.Convert.ToInt32(parameter.ToString()) < 0)
            {
                if (App.gvm_AppSettings.AutoExpire)
                {
                    ls_ReturnValue = AppResources.Time;
                }
                else
                {
                    ls_ReturnValue = AppResources.CheckedIn;
                }
            }
            else
            {
                ls_ReturnValue = App.gvm_AppSettings.FormFieldCaptionText(Int32.Parse(parameter.ToString()));
            }
        }
        catch (Exception ex)
        {
            App.ProcessException(ex);
        }

        return ls_ReturnValue;
    }

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

【问题讨论】:

  • 您还能运行该应用程序吗?当您使用相同的 xaml 创建新应用时,是否会收到相同的错误?
  • 是的,应用程序仍在运行,但奇怪的事情正在发生,我正在尝试解决这些错误以确保它们不是问题的根源。我没有尝试过一个新项目,因为我正试图将这个项目推出市场
  • 如果应用程序仍然运行意味着代码是正确的,我不确定是什么导致了这些错误。正常的做法是删除bin和obj来清理重建。
  • 好的,我会试试的。谢谢
  • 我删除了 bin 和 obj 文件,没有任何区别。

标签: xamarin.forms data-binding ivalueconverter


【解决方案1】:

不能加个字符串资源吗?

...
<ContentPage.Resources>
    <ResourceDictionary>
        <converters:DoubleToInt x:Key="doubleToInt" />
        <x:String x:Key="twoFiveFive">255</x:String>
    </ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
...
    <Label Text="{Binding Red, Converter={StaticResource doubleToInt}, ConverterParameter={StaticResource twoFiveFive}}" .../>
...
</ContentPage.Content>

【讨论】:

  • 这需要我为我想使用的每个参数创建一个字符串资源。 (在我的情况下有很多)。对于应该简单修复的东西,这似乎需要做很多额外的工作。但是,如果这确实成为我的阻塞问题,我会记住这个解决方案,因为它应该可以工作。谢谢@Sorcamshadow。
猜你喜欢
  • 2021-06-05
  • 2016-10-05
  • 2015-07-01
  • 2013-05-06
  • 1970-01-01
  • 2010-10-12
  • 2017-04-30
  • 1970-01-01
  • 2020-06-18
相关资源
最近更新 更多