【问题标题】:StaticResource not found for key TextToBoolConverter未找到关键 TextToBoolConverter 的静态资源
【发布时间】:2018-02-07 16:18:49
【问题描述】:

我需要在评论文本上使用的转换器有问题。

我得到:“找不到关键 TextToBoolConverter 的静态资源”。

转换器:

namespace myMood.Helpers
{
    public class TextToBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, 
                                  object parameter, CultureInfo culture)
        {
            if (value != null)
                if (!(value is string)) return true;
            return string.IsNullOrWhiteSpace(value as string) ? false : true;
        }

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

查看:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="myMood.Views.Entries"
             Icon="ic_view_headline_white_24dp.png"
             xmlns:converters="clr-namespace:myMood.Helpers"
             xmlns:viewModels="clr-namespace:myMood.ViewModels">
...
<Label Text="{Binding Comment}" 
      IsVisible="{Binding Comment, Converter={StaticResource TextToBoolConverter}}">

【问题讨论】:

    标签: xaml xamarin.forms typeconverter


    【解决方案1】:

    除非您已将其添加为 App 资源,否则您应该在要使用它的每个页面上将转换器声明为本地资源。

    只需将您的 XAML 更改为:

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="myMood.Views.Entries"
                 Icon="ic_view_headline_white_24dp.png"
                 xmlns:converters="clr-namespace:myMood.Helpers"
                 xmlns:viewModels="clr-namespace:myMood.ViewModels">
        <ContentPage.Resources>
            <ResourceDictionary>
                <converters:TextToBoolConverter x:Key="TextToBoolConverter" />
            </ResourceDictionary>
        </ContentPage.Resources>
        ...
        <Label Text="{Binding Comment}" 
               IsVisible="{Binding Comment, Converter={StaticResource TextToBoolConverter}}"/>
         ...
    </ContentPage>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      相关资源
      最近更新 更多