StaticResource not found for key TextToBoolConverter
Asked Answered
C

1

7

I have a problem with a converter I need to use on a Comment text.

I get: "StaticResource not found for key TextToBoolConverter".

Converter:

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();
        }
    }
}

View:

<?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}}">
Clayborne answered 7/2, 2018 at 16:18 Comment(0)
Y
10

Unless you have added it as an App resource, you should declare the converter as a local resource on every page you want to use it.

Just change your XAML to:

<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>
Yuri answered 7/2, 2018 at 16:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.