Uno Platform BottomNavBar 取消选中所选项目
Uno Platform BottomNavBar uncheck selected item
我有一个使用来自 uno-material 的底部导航栏的应用程序。酒吧工作正常。我还有一个向上的设置按钮。当用户选择按钮时 - 它会改变颜色。我想要做的是“取消选择”当前在底部导航栏上选择的项目 - 这样用户就会知道他们正在使用设置按钮。我知道 uno-material 的 bottomnavbar 使用 togglebuttons- 所以这就是我尝试这条路径的原因。到目前为止,这是我尝试过的:
XAML:
<StackPanel>
<Button x:Name="settingsButton" HorizontalAlignment="Right" Background="{StaticResource NavigationViewItemBackground}" Click="settingsButton_Click">
<SymbolIcon x:Name="settingIcon" Symbol="Setting" Foreground="{StaticResource NavigationViewItemForeground}" >
</SymbolIcon>
</Button>
</StackPanel>
<controls:BottomNavigationBar
x:Name="BottomNavBar"
Loaded="bottomNavView_Loaded">
<controls:BottomNavigationBar.Items >
<controls:BottomNavigationBarItem Label="Home" tag="HomeView" Click="OnClick" Padding.right="2" >
<controls:BottomNavigationBarItem.Icon>
<PathIcon
Data=""/>
</controls:BottomNavigationBarItem.Icon>
</controls:BottomNavigationBarItem>
<controls:BottomNavigationBarItem Label="Event" tag="event" Click="OnClick" Padding.Left="2" Padding.right="2">
<controls:BottomNavigationBarItem.Icon>
<PathIcon
Data=""/>
</controls:BottomNavigationBarItem.Icon>
</controls:BottomNavigationBarItem>
</controls:BottomNavigationBar.Items>
</controls:BottomNavigationBar>
隐藏代码:
private void settingsButton_Click(object sender, RoutedEventArgs e)
{
this.settingIcon.Foreground = Application.Current.Resources["NavigationViewSelectionIndicatorForeground"] as SolidColorBrush;
this.BottomNavBar.SelectedItem.isChecked = false;
this.BottomNavView_Navigate("settings");
}
所以我想出了一个可行的方法 - 但我并不是很喜欢它。如果其他人有更好的解决方案,请 post。而不是“取消选择”该项目 - 我覆盖了颜色。在 onClick event/Navigate 方法中-我手动设置颜色。在我看来这很丑陋-但它似乎有效并且反应灵敏。这是更新后的代码:
private void BottomNavView_Navigate(string navItemTag)
{
if (this.settingsButtonPressed)
{
this.settingIcon.Foreground = Application.Current.Resources["NavigationViewItemForeground"] as SolidColorBrush;
this.settingsButtonPressed = !this.settingsButtonPressed;
}
this.selectedNavItem.Foreground = Application.Current.Resources["NavigationViewSelectionIndicatorForeground"] as SolidColorBrush;
//do navigation here
}
private void settingsButton_Click(object sender, RoutedEventArgs e)
{
this.settingsButtonPressed = true;
this.settingIcon.Foreground = Application.Current.Resources["NavigationViewSelectionIndicatorForeground"] as SolidColorBrush;
this.selectedNavItem.Foreground = Application.Current.Resources["NavigationViewItemForegroundSelectedPressed"] as SolidColorBrush;
this.BottomNavView_Navigate("settings");
}
您原来的方法不起作用,因为代码会阻止取消选择所选项。正确的解决方案如下所示:
private void settingsButton_Click(object sender, RoutedEventArgs e)
{
SettingIcon.Foreground = Application.Current.Resources["NavigationViewSelectionIndicatorForeground"] as SolidColorBrush;
BottomNavBar.SelectedItem = null;
}
这与其他“列表”控件一致。不幸的是,目前在 BottomNavigationBar
中无法将 SelectedItem
设置为 null
,因此我创建了一个 fix this here 的 PR。它应该很快合并
我有一个使用来自 uno-material 的底部导航栏的应用程序。酒吧工作正常。我还有一个向上的设置按钮。当用户选择按钮时 - 它会改变颜色。我想要做的是“取消选择”当前在底部导航栏上选择的项目 - 这样用户就会知道他们正在使用设置按钮。我知道 uno-material 的 bottomnavbar 使用 togglebuttons- 所以这就是我尝试这条路径的原因。到目前为止,这是我尝试过的:
XAML:
<StackPanel>
<Button x:Name="settingsButton" HorizontalAlignment="Right" Background="{StaticResource NavigationViewItemBackground}" Click="settingsButton_Click">
<SymbolIcon x:Name="settingIcon" Symbol="Setting" Foreground="{StaticResource NavigationViewItemForeground}" >
</SymbolIcon>
</Button>
</StackPanel>
<controls:BottomNavigationBar
x:Name="BottomNavBar"
Loaded="bottomNavView_Loaded">
<controls:BottomNavigationBar.Items >
<controls:BottomNavigationBarItem Label="Home" tag="HomeView" Click="OnClick" Padding.right="2" >
<controls:BottomNavigationBarItem.Icon>
<PathIcon
Data=""/>
</controls:BottomNavigationBarItem.Icon>
</controls:BottomNavigationBarItem>
<controls:BottomNavigationBarItem Label="Event" tag="event" Click="OnClick" Padding.Left="2" Padding.right="2">
<controls:BottomNavigationBarItem.Icon>
<PathIcon
Data=""/>
</controls:BottomNavigationBarItem.Icon>
</controls:BottomNavigationBarItem>
</controls:BottomNavigationBar.Items>
</controls:BottomNavigationBar>
隐藏代码:
private void settingsButton_Click(object sender, RoutedEventArgs e)
{
this.settingIcon.Foreground = Application.Current.Resources["NavigationViewSelectionIndicatorForeground"] as SolidColorBrush;
this.BottomNavBar.SelectedItem.isChecked = false;
this.BottomNavView_Navigate("settings");
}
所以我想出了一个可行的方法 - 但我并不是很喜欢它。如果其他人有更好的解决方案,请 post。而不是“取消选择”该项目 - 我覆盖了颜色。在 onClick event/Navigate 方法中-我手动设置颜色。在我看来这很丑陋-但它似乎有效并且反应灵敏。这是更新后的代码:
private void BottomNavView_Navigate(string navItemTag)
{
if (this.settingsButtonPressed)
{
this.settingIcon.Foreground = Application.Current.Resources["NavigationViewItemForeground"] as SolidColorBrush;
this.settingsButtonPressed = !this.settingsButtonPressed;
}
this.selectedNavItem.Foreground = Application.Current.Resources["NavigationViewSelectionIndicatorForeground"] as SolidColorBrush;
//do navigation here
}
private void settingsButton_Click(object sender, RoutedEventArgs e)
{
this.settingsButtonPressed = true;
this.settingIcon.Foreground = Application.Current.Resources["NavigationViewSelectionIndicatorForeground"] as SolidColorBrush;
this.selectedNavItem.Foreground = Application.Current.Resources["NavigationViewItemForegroundSelectedPressed"] as SolidColorBrush;
this.BottomNavView_Navigate("settings");
}
您原来的方法不起作用,因为代码会阻止取消选择所选项。正确的解决方案如下所示:
private void settingsButton_Click(object sender, RoutedEventArgs e)
{
SettingIcon.Foreground = Application.Current.Resources["NavigationViewSelectionIndicatorForeground"] as SolidColorBrush;
BottomNavBar.SelectedItem = null;
}
这与其他“列表”控件一致。不幸的是,目前在 BottomNavigationBar
中无法将 SelectedItem
设置为 null
,因此我创建了一个 fix this here 的 PR。它应该很快合并