找不到关键的TextToBoolConverter的StaticResource [英] StaticResource not found for key TextToBoolConverter

查看:74
本文介绍了找不到关键的TextToBoolConverter的StaticResource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在注释文本上需要使用的转换器有问题.

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

我得到:找不到关键的TextToBoolConverter的StaticResource".

I get: "StaticResource not found for key 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}}">

推荐答案

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

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.

只需将您的XAML更改为:

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>

这篇关于找不到关键的TextToBoolConverter的StaticResource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆