如何在UWP中使用BooleanToVisibilityConverter

The*_*ing 3 c# uwp

为了在UWP应用程序中制作汉堡包按钮,我尝试使用BooleanToVisibilityConverter更改汉堡包按钮的状态,就像RSSReader示例一样.

问题是,当我创建BooleanToVisibilityConverter.cs的文件夹中常见写道:

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");

    }
}
Run Code Online (Sandbox Code Playgroud)

然后将其导入MainPage.xaml:

<Page
    x:Class="UWPTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UWPTest"
    xmlns:common="using:UWPTest.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Page.Resources>
        <common:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    </Page.Resources>
    <Grid Background="Transparent">
        <ToggleButton x:Name="TogglePaneButton"
            Visibility="{x:Bind ViewModel.IsInDetailsMode, Mode=OneWay, ConverterParameter=Reverse, Converter={StaticResource BooleanToVisibilityConverter}}"
            Margin="0"
            TabIndex="1" 
            Checked="{x:Bind CheckTogglePaneButtonSizeChanged}"
            Unchecked="{x:Bind CheckTogglePaneButtonSizeChanged}"
            IsChecked="{Binding IsPaneOpen, ElementName=RootSplitView, Mode=TwoWay}"
            AutomationProperties.Name="Menu" ToolTipService.ToolTip="Menu"
            Style="{StaticResource SplitViewTogglePaneButtonStyle}"/>
    </Grid>
</Page>
Run Code Online (Sandbox Code Playgroud)

IntelliSense说名称"BooleanToVisibilityConverter"在命名空间"using:UWPTest.Common"中不存在.我无法弄清楚找不到课程的原因.

IntelliSense中文的图片:

在此输入图像描述

And*_*yak 6

添加BooleanToVisibilityConverter到资源时,您将其设置Keyboolean:

<common:BooleanToVisibilityConverter x:Key="boolean" />
Run Code Online (Sandbox Code Playgroud)

所以绑定应该如下所示:

Converter={StaticResource boolean}
Run Code Online (Sandbox Code Playgroud)

或者您可以将Key值更改BooleanToVisibilityConverter为示例中的值.