【问题标题】:How to use BooleanToVisibilityConverter in UWP如何在 UWP 中使用 BooleanToVisibilityConverter
【发布时间】:2021-09-30 14:52:17
【问题描述】:

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

问题是,当我在文件夹 Common 中创建 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");

    }
}

然后将其导入到 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>

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

IntelliSense中文文字图片:

【问题讨论】:

  • IntelliSense 并不总是准确的。你能无误地构建项目吗?
  • 只是为了确定。代码在运行时不会编译或崩溃?有时智能感知会显示此类错误,但代码正常且运行良好。
  • 另外,仅供参考,如果您的目标是 SDK 版本 14393 或更高版本,x:Bind 将自动将布尔值转换为可见性值,而无需您编写 IValueConverter。
  • 我也无法编译。还有“名称'BooleanToVisibilityConverter'在名称空间'using:UWPTest.Common'中不存在。”在错误列表中。
  • 请注意,UWP 现在具有从布尔到可见性的内置转换。所以只需Visibility="{x:Bind ViewModel.IsInDetailsMode, Mode=OneWay}" 就可以了。

标签: c# uwp


【解决方案1】:

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

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

所以绑定应该是这样的:

Converter={StaticResource boolean}

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

【讨论】:

  • 对不起,那是我愚蠢的错。示例中的原始代码为Converter={StaticResource BooleanToVisibilityConverter}。该错误似乎与ToggleButton 部分无关。
  • 我的错误修复后问题仍然存在。
  • @Thesharing 在这种情况下我只能建议标准解决方案:1. 重建解决方案; 2. 从 xaml 重建解决方案中删除转换器并尝试再次添加它; 3. 使用一些字符串比较器检查名称和命名空间中的拼写(有些不同的字符看起来相同); 4. 重新启动 Visual Studio。您也可以按照 Decade Moon 的建议考虑删除转换器,但您需要反向值,抱歉没有帮助,我会考虑其他解决方案。
【解决方案2】:

实际上,您不需要实现自己的转换器。 只需使用现有的one provided by Micorsoft

UWP 转换器的完整列表是here

XAML 示例

<Page
    x:Class="YourPageClass"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:core="using:Microsoft.Xaml.Interactions.Core"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:converters="using:Microsoft.Toolkit.Uwp.UI.Converters"
    xmlns:extensions="using:EtherClient.Extensions"      
    NavigationCacheMode="Enabled" VerticalAlignment="Stretch"
    mc:Ignorable="d"  Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Page.Resources>
        <converters:BoolToVisibilityConverter x:Key="MyBooleanToVisibilityConverter"/>
        
 <Border BorderBrush="LightGreen" BorderThickness="1" Visibility="{x:Bind YourBoolPropertyGoesHere, Mode=OneWay, Converter={StaticResource MyBooleanToVisibilityConverter}}" >
                        <FontIcon FontSize="12" Margin="2,0,2,0" FontFamily="Segoe MDL2 Assets"  Foreground="LightGreen" Glyph="&#xf003;"/>
                        
</Border>
...

【讨论】:

    猜你喜欢
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 2016-09-12
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多