简体   繁体   中英

.Net MAUI Issue with VisualStateManager on Windows Platform

I have created a simple .NET MAUI project that allows items to be selected in a CollectionView. What should happen is that the VisualStateManager should change the background colour to yellow for the selected item. This works fine on the Android platform, but when I run against the Windows platform the selected item loses focus when the mouse cursor is moved away.

Is there a way to prevent the selected item from losing focus on the Windows platform?

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SelectionTest.MainPage">

    <ContentPage.Resources>
        <Style TargetType="StackLayout">
            <Setter Property="VisualStateManager.VisualStateGroups">
                <VisualStateGroupList>
                    <VisualStateGroup>
                        <VisualState Name="Normal" />
                        <VisualState Name="Selected">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor" Value="Yellow" />
                                <Setter TargetName="valueLabel" Property="Label.TextColor" Value="Red" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateGroupList>
            </Setter>
        </Style>
    </ContentPage.Resources>

    <StackLayout>
        <Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
            <Label Text="Visual State Manager Sample" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
        </Frame>
        <Entry Text="Please subscribe to my channel!">
           
        </Entry>

        <CollectionView SelectionMode="Single">
            <CollectionView.ItemsSource>
                <x:Array Type="{x:Type x:String}">
                    <x:String>111111111</x:String>
                    <x:String>222222222</x:String>
                    <x:String>333333333</x:String>
                </x:Array>
            </CollectionView.ItemsSource>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout>
                        <Label x:Name="valueLabel" Text="{Binding .}" HorizontalTextAlignment="Center" />
                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>

</ContentPage>

What you mean is that the background color should remain the selected color even when the mouse cursor moves away from the control on Windows, however it remains the selected background color on Android . In conclusion, the VisualStateManager works differently on Windows and Android so you can open a new issue if needed.

However, I tested your code and the results on both platforms worked with no difference. I think you can try to change SelectionMode="Multiple" of CollectionView that will remain the selected color.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM