我需要在评论文本上使用转换器,但遇到问题。
我得到:“未找到键 TextToBoolConverter 的 StaticResource”。
转换器:
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}}">
除非您已将其添加为应用程序资源,否则您应该在要使用它的每个页面上将转换器声明为本地资源。
只需将您的 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)