1

我在视图中使用了进度条,如下所示:

<ProgressBar  IsIndeterminate="{Binding IsBusy}" Visibility="{Binding IsBusy,Converter={StaticResource BooleanToVisibilityConverter}}" HorizontalAlignment="Left" Height="33" Margin="46,222,0,0" VerticalAlignment="Top" Width="358"/>
  • BooleanToVisibilityConverter 在 ViewModel 中定义
  • 在视图的 xaml 代码上出现错误 <phone:PhoneApplicationPage.Resources> <BooleanToVisibilityConverter x:Key="BooleanConverter"/> </phone:PhoneApplicationPage.Resources>

*错误是:

名称空间“ http://schemas.microsoft.com/client/2007 ”中不存在名称“BooleanToVisibilityConverter” 。

请告诉我如何解决此错误

4

3 回答 3

2

您可以像这样定义转换器:

namespace YourNamespace
{
    public class BooleanToVisibilityConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (bool)value ? Visibility.Visible : Visibility.Collapsed;
        }

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

构建您的解决方案!在 XAML 中:

<navigation:Page x:Class="MyCustomNamespace"
    xmlns:converters="clr-namespace:YourNamespace">
    <navigation:Page.Resources>
        <converters:BooleanToVisibilityConverter x:Key="BooleanToVisibility" />
    </navigation:Page.Resources>
    <TextBlock Visibility="{Binding BooleanFromViewModel, Convert={StaticResource BooleanToVisibility}" />
</navigation:Page>
于 2013-03-04T09:32:23.743 回答
1
<UserControl xmlns:converter="your namespace">
<UserControl.Resources>
<converter:BooleanToVisibilityConverter x:Key="bool2VisibilityConverter" />
</UserControl.Resources>
<TextBlock Visibility={Binding Path=Field, Converter={StaticResource bool2VisibilityConverter}} />
</UserControl>
于 2013-03-04T09:35:46.270 回答
0

您需要在 xaml 中定义您的命名空间。在 Window/UserControl 的顶层,您需要定义一个 xml 命名空间:

xmlns:yn="clr-namespace:YourNamespace"

然后你需要参考你BooleanToVisibilityConverter使用的命名空间:

<yn:BooleanToVisibilityConverter ... />
于 2013-03-04T09:32:05.743 回答