微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Uno Platform BottomNavBar 取消选中所选项目

如何解决Uno Platform BottomNavBar 取消选中所选项目

我有一个使用 uno-material 的底部导航栏的应用程序。酒吧运作良好。我还有一个向上的设置按钮。当用户选择按钮时 - 它会改变颜色。我想要做的是“取消选择”当前在底部导航栏上选择的项目 - 这样用户就会知道他们正在使用设置按钮。我知道 uno-material 的底部导航栏使用切换按钮 - 这就是我尝试该路径的原因。这是我迄今为止尝试过的:

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

解决方法

您原来的方法不起作用,因为代码会阻止取消选择所选项目。正确的解决方案如下所示:

private void settingsButton_Click(object sender,RoutedEventArgs e)
{
    SettingIcon.Foreground = Application.Current.Resources["NavigationViewSelectionIndicatorForeground"] as SolidColorBrush;
    BottomNavBar.SelectedItem = null;       
}

这与其他“列表”控件一致。不幸的是,目前无法在 SelectedItem 中将 null 设置为 BottomNavigationBar,因此我已为 fix this here 创建了 PR。应该很快就合并了?

,

所以我想出了一种有效的方法——但我并不是很喜欢它。如果其他人有更好的解决方案-请发布。而不是“取消选择”项目 - 我覆盖了颜色。在 onClick 事件/导航方法中 - 我手动设置了颜色。我认为这很丑陋 - 但它似乎有效并且响应迅速。这是更新后的代码:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。