【发布时间】: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}"就可以了。