如何在UWP中使用BooleanToVisibilityConverter

时间:2016-10-03 12:55:29

标签: c# uwp

为了在UWP应用程序中制作汉堡包按钮,我尝试使用_CreateDeviceInfo()来更改汉堡包按钮的状态,就像RSSReader Example一样。

问题是,当我在 Common 文件夹中创建 BooleanToVisibilityConverter.cs 并写道:

BooleanToVisibilityConverter

然后将其导入 MainPage.xaml

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;

namespace UWPTest.Common {
    public class BooleanToVisibilityConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, string language) =>
            (bool)value ^ (parameter as string ?? string.Empty).Equals("Reverse") ?
                Visibility.Visible : Visibility.Collapsed;

        public object ConvertBack(object value, Type targetType, object parameter, string language) =>
            (Visibility)value == Visibility.Visible ^ (parameter as string ?? string.Empty).Equals("Reverse");

    }
}

IntelliSense说名称" BooleanToVisibilityConverter"命名空间中不存在"使用:UWPTest.Common"。我无法弄清楚找不到该类的原因。

智慧感知的中文图片:

enter image description here

1 个答案:

答案 0 :(得分:6)

当您向资源添加BooleanToVisibilityConverter时,您将Key设置为boolean

<common:BooleanToVisibilityConverter x:Key="boolean" />

所以绑定应该如下:

Converter={StaticResource boolean}

或者您可以将Key值更改为BooleanToVisibilityConverter,就像在示例中所做的那样。