简体   繁体   English

在 MVVM 中获取 BooleanToVisibilityConverter 的 xaml 绑定代码错误

[英]Getting error on xaml binding code for BooleanToVisibilityConverter in MVVM

I used progress bar in View as below:我在视图中使用了进度条,如下所示:

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

*Error is: *错误是:

The name "BooleanToVisibilityConverter" does not exist in the namespace " http://schemas.microsoft.com/client/2007 ".名称空间“ http://schemas.microsoft.com/client/2007 ”中不存在名称“BooleanToVisibilityConverter”。

Please Tell me how to resolve this error请告诉我如何解决此错误

You can define a converter like this :您可以像这样定义转换器:

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

Build your solution !构建您的解决方案! And in XAML :在 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>
<UserControl xmlns:converter="your namespace">
<UserControl.Resources>
<converter:BooleanToVisibilityConverter x:Key="bool2VisibilityConverter" />
</UserControl.Resources>
<TextBlock Visibility={Binding Path=Field, Converter={StaticResource bool2VisibilityConverter}} />
</UserControl>

You'll need define your namespace in xaml.您需要在 xaml 中定义命名空间。 At the top level of your Window/UserControl, you need to define an xml namespace:在 Window/UserControl 的顶层,您需要定义一个 xml 命名空间:

xmlns:yn="clr-namespace:YourNamespace"

and then you need to refer to your BooleanToVisibilityConverter using that namespace:然后您需要使用该命名空间引用您的BooleanToVisibilityConverter

<yn:BooleanToVisibilityConverter ... />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM