.NET MAUI集合选择颜色没有改变

huangapple go评论59阅读模式
英文:

.NET MAUI Collection selection color isn't changing

问题

以下是您要翻译的代码部分,我会跳过不需要翻译的部分。

<!-- I tried this -->
<ContentPage.Resources>
    <Style x:Key="BorderSelectionStyle" TargetType="Border">
        <Setter Property="VisualStateManager.VisualStateGroups">
            <VisualStateGroupList>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Normal" />
                    <VisualState x:Name="Selected">
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="Transparent" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateGroupList>
        </Setter>
    </Style>
</ContentPage.Resources>

<StackLayout>

    <ActivityIndicator HeightRequest="40"
                   WidthRequest="40"                                       
                   IsRunning="{Binding IsBusy}"
                   IsVisible="{Binding IsBusy}"/>

    <Border StrokeThickness="2" Padding="10" Margin="10" VerticalOptions="FillAndExpand">

        <Border.StrokeShape>
            <RoundRectangle CornerRadius="10"></RoundRectangle>
        </Border.StrokeShape>

        <StackLayout>                

            <CollectionView ItemsSource ="{Binding ArticleList}"                        
                    RemainingItemsThreshold="1"
                    RemainingItemsThresholdReachedCommand="{Binding LoadMoreArticleDataCommand}"
                    VerticalOptions="FillAndExpand"                           
                    SelectionMode="Single"                         
                    SelectedItem="{Binding SelectedArticle}"                        
                    x:Name="ArticlesCollectionView">

                <CollectionView.Header>
                    <StackLayout Margin="10">
                        <Label Text="Articles" FontSize="Large" FontAttributes="Bold" HorizontalTextAlignment="Start"/>
                        <Label Text="Select articles which you like and start exploring" HorizontalTextAlignment="Start" FontSize="Caption"/>                            
                    </StackLayout>
                </CollectionView.Header>

                <CollectionView.ItemTemplate>

                    <DataTemplate x:DataType="models:ArticleResponce">

                        <Border StrokeThickness="3.5">

                            <Border.StrokeShape>
                                <RoundRectangle CornerRadius="10"></RoundRectangle>
                            </Border.StrokeShape>

                            

                            <Grid BackgroundColor="White">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>

                                <Image Source="dotnet_bot.svg" Aspect="AspectFill" HeightRequest="100" Grid.RowSpan="2" Grid.Column="0" Margin="5"/>

                                <!-- Labels -->
                                <StackLayout Grid.Column="1" Spacing="2" Padding="20" HorizontalOptions="FillAndExpand" Margin="20,0,10,0">

                                    <StackLayout Orientation="Horizontal">
                                        <Image Source="dotnet_bot.svg" WidthRequest="20" HeightRequest="20" />
                                        <Label Text="{Binding Title}" FontSize="15" Margin="5" LineBreakMode="WordWrap" MaxLines="3" HorizontalOptions="StartAndExpand" />
                                    </StackLayout>

                                    <StackLayout Orientation="Horizontal">
                                        <Image Source="dotnet_bot.svg" WidthRequest="20" HeightRequest="20"/>
                                        <Label Text="{Binding Author}" FontSize="15" Margin="5" LineBreakMode="WordWrap" MaxLines="1" HorizontalOptions="StartAndExpand"/>
                                    </StackLayout>

                                    <StackLayout Orientation="Horizontal">
                                        <Image Source="dotnet_bot.svg" WidthRequest="20" HeightRequest="20" />
                                        <Label Text="{Binding PublishDate}" FontSize="15" Margin="5" LineBreakMode="WordWrap"/>
                                    </StackLayout>

                                </StackLayout>
                                
                                    <!-- Second variant which I found -->                                
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup Name="CommonStates">
                                        <VisualState Name="Normal" />
                                        <VisualState Name="Selected">
                                            <VisualState.Setters>
                                                <Setter Property="BackgroundColor" Value="Yellow" />
                                            </VisualState.Setters>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>

                            </Grid>

                        </Border>

                    </DataTemplate>

                </CollectionView.ItemTemplate>

            </CollectionView>

        </StackLayout>

    </Border>


    <ActivityIndicator HeightRequest="40"
                   WidthRequest="40"
                   IsRunning="{Binding IsLoading}"
                   IsVisible="{Binding IsLoading}"/>

</StackLayout>

</ContentPage>

ViewModel 部分不需要翻译。

英文:

I wondering how to change color of selection. I read official docs, topics, forum questions but it doesn't work for me.I tried 2 VisualState variant which I mark with comment.I also want to know is this any way to change size of selection and any other properties. Sometimes when I set visual states like Microsoft docs said, color of selection become transparent and I cant even see this, but all events working fine. Maybe you can advise me way in which I can just tapped on collectionview item and go to other page.

Here is my view XAML code:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
xmlns:local=&quot;clr-namespace:Bibliomatic_MAUI_App&quot;             
xmlns:viewModels=&quot;clr-namespace:Bibliomatic_MAUI_App.ViewModels&quot;
xmlns:models=&quot;clr-namespace:Bibliomatic_MAUI_App.Models&quot;
x:DataType=&quot;viewModels:ArticlesViewModel&quot;             
x:Class=&quot;Bibliomatic_MAUI_App.Views.ArticleView&quot;
Title=&quot;&quot;&gt;
&lt;!-- I tried this --&gt;
&lt;ContentPage.Resources&gt;
&lt;Style x:Key=&quot;BorderSelectionStyle&quot; TargetType=&quot;Border&quot;&gt;
&lt;Setter  Property=&quot;VisualStateManager.VisualStateGroups&quot;&gt;
&lt;VisualStateGroupList&gt;
&lt;VisualStateGroup x:Name=&quot;CommonStates&quot;&gt;
&lt;VisualState x:Name=&quot;Normal&quot; /&gt;
&lt;VisualState x:Name=&quot;Selected&quot;&gt;
&lt;VisualState.Setters&gt;
&lt;Setter Property=&quot;BackgroundColor&quot;
Value=&quot;Transparent&quot; /&gt;
&lt;/VisualState.Setters&gt;
&lt;/VisualState&gt;
&lt;/VisualStateGroup&gt;
&lt;/VisualStateGroupList&gt;
&lt;/Setter&gt;
&lt;/Style&gt;
&lt;/ContentPage.Resources&gt;
&lt;StackLayout&gt;
&lt;ActivityIndicator HeightRequest=&quot;40&quot;
WidthRequest=&quot;40&quot;                                       
IsRunning=&quot;{Binding IsBusy}&quot;
IsVisible=&quot;{Binding IsBusy}&quot;/&gt;
&lt;Border StrokeThickness=&quot;2&quot; Padding=&quot;10&quot; Margin=&quot;10&quot; VerticalOptions=&quot;FillAndExpand&quot;&gt;
&lt;Border.StrokeShape&gt;
&lt;RoundRectangle CornerRadius=&quot;10&quot;&gt;&lt;/RoundRectangle&gt;
&lt;/Border.StrokeShape&gt;
&lt;StackLayout&gt;                
&lt;CollectionView ItemsSource =&quot;{Binding ArticleList}&quot;                        
RemainingItemsThreshold=&quot;1&quot;
RemainingItemsThresholdReachedCommand=&quot;{Binding LoadMoreArticleDataCommand}&quot;
VerticalOptions=&quot;FillAndExpand&quot;                           
SelectionMode=&quot;Single&quot;                         
SelectedItem=&quot;{Binding SelectedArticle}&quot;                        
x:Name=&quot;ArticlesCollectionView&quot;&gt;
&lt;CollectionView.Header&gt;
&lt;StackLayout Margin=&quot;10&quot;&gt;
&lt;Label Text=&quot;Articles&quot; FontSize=&quot;Large&quot; FontAttributes=&quot;Bold&quot; HorizontalTextAlignment=&quot;Start&quot;/&gt;
&lt;Label Text=&quot;Select articles which you like and start exploring&quot; HorizontalTextAlignment=&quot;Start&quot; FontSize=&quot;Caption&quot;/&gt;                            
&lt;/StackLayout&gt;
&lt;/CollectionView.Header&gt;
&lt;CollectionView.ItemTemplate&gt;
&lt;DataTemplate x:DataType=&quot;models:ArticleResponce&quot;&gt;
&lt;Border StrokeThickness=&quot;3.5&quot;&gt;
&lt;Border.StrokeShape&gt;
&lt;RoundRectangle CornerRadius=&quot;10&quot;&gt;&lt;/RoundRectangle&gt;
&lt;/Border.StrokeShape&gt;
&lt;Grid BackgroundColor=&quot;White&quot;&gt;
&lt;Grid.ColumnDefinitions&gt;
&lt;ColumnDefinition Width=&quot;Auto&quot; /&gt;
&lt;ColumnDefinition Width=&quot;*&quot; /&gt;
&lt;/Grid.ColumnDefinitions&gt;
&lt;Image Source=&quot;dotnet_bot.svg&quot; Aspect=&quot;AspectFill&quot; HeightRequest=&quot;100&quot; Grid.RowSpan=&quot;2&quot; Grid.Column=&quot;0&quot; Margin=&quot;5&quot;/&gt;
&lt;!-- Labels --&gt;
&lt;StackLayout Grid.Column=&quot;1&quot; Spacing=&quot;2&quot; Padding=&quot;20&quot; HorizontalOptions=&quot;FillAndExpand&quot; Margin=&quot;20,0,10,0&quot;&gt;
&lt;StackLayout Orientation=&quot;Horizontal&quot;&gt;
&lt;Image Source=&quot;dotnet_bot.svg&quot; WidthRequest=&quot;20&quot; HeightRequest=&quot;20&quot; /&gt;
&lt;Label Text=&quot;{Binding Title}&quot; FontSize=&quot;15&quot; Margin=&quot;5&quot; LineBreakMode=&quot;WordWrap&quot; MaxLines=&quot;3&quot; HorizontalOptions=&quot;StartAndExpand&quot; /&gt;
&lt;/StackLayout&gt;
&lt;StackLayout Orientation=&quot;Horizontal&quot;&gt;
&lt;Image Source=&quot;dotnet_bot.svg&quot; WidthRequest=&quot;20&quot; HeightRequest=&quot;20&quot;/&gt;
&lt;Label Text=&quot;{Binding Author}&quot; FontSize=&quot;15&quot; Margin=&quot;5&quot; LineBreakMode=&quot;WordWrap&quot; MaxLines=&quot;1&quot; HorizontalOptions=&quot;StartAndExpand&quot;/&gt;
&lt;/StackLayout&gt;
&lt;StackLayout Orientation=&quot;Horizontal&quot;&gt;
&lt;Image Source=&quot;dotnet_bot.svg&quot; WidthRequest=&quot;20&quot; HeightRequest=&quot;20&quot; /&gt;
&lt;Label Text=&quot;{Binding PublishDate}&quot; FontSize=&quot;15&quot; Margin=&quot;5&quot; LineBreakMode=&quot;WordWrap&quot;/&gt;
&lt;/StackLayout&gt;
&lt;/StackLayout&gt;
&lt;!-- Second variant which I found --&gt;                                &lt;VisualStateManager.VisualStateGroups&gt;
&lt;VisualStateGroup Name=&quot;CommonStates&quot;&gt;
&lt;VisualState Name=&quot;Normal&quot; /&gt;
&lt;VisualState Name=&quot;Selected&quot;&gt;
&lt;VisualState.Setters&gt;
&lt;Setter Property=&quot;BackgroundColor&quot; Value=&quot;Yellow&quot; /&gt;
&lt;/VisualState.Setters&gt;
&lt;/VisualState&gt;
&lt;/VisualStateGroup&gt;
&lt;/VisualStateManager.VisualStateGroups&gt;
&lt;/Grid&gt;
&lt;/Border&gt;
&lt;/DataTemplate&gt;
&lt;/CollectionView.ItemTemplate&gt;
&lt;/CollectionView&gt;
&lt;/StackLayout&gt;
&lt;/Border&gt;
&lt;ActivityIndicator HeightRequest=&quot;40&quot;
WidthRequest=&quot;40&quot;
IsRunning=&quot;{Binding IsLoading}&quot;
IsVisible=&quot;{Binding IsLoading}&quot;/&gt;
&lt;/StackLayout&gt;
&lt;/ContentPage&gt;

And also ViewModel code:

using Bibliomatic_MAUI_App.Models;
using Bibliomatic_MAUI_App.Services;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Collections.ObjectModel;
using System.Windows.Input;
namespace Bibliomatic_MAUI_App.ViewModels
{
public partial class ArticlesViewModel : ObservableObject
{
public ObservableCollection&lt;ArticleResponce&gt; ArticleList { get; set; }
private List&lt;ArticleResponce&gt; allArticlesList;
private readonly ArticleService articleService;
private int pageSize = 20;
[ObservableProperty]
public bool isBusy;
[ObservableProperty]
public bool isLoading;
private ArticleResponce selectedArticle;
public ArticleResponce SelectedArticle
{
get =&gt; selectedArticle;
set
{
if (SetProperty(ref selectedArticle, value))
{                    
OnItemSelected(value);
}
}
}
public ArticlesViewModel(ArticleService articleService)
{
this.articleService = articleService;
ArticleList = new ObservableCollection&lt;ArticleResponce&gt;();
LoadArticleDataCommand.Execute(null);
}
private async void OnItemSelected(ArticleResponce selectedArticle)
{
if (selectedArticle != null)
{
await Shell.Current.GoToAsync($&quot;main?title={selectedArticle.Title}&quot;);
}
}      
[RelayCommand]
public async Task LoadArticleData()
{
IsBusy = true;
allArticlesList = await articleService.GetAllArticles();
var articleData = allArticlesList.Take(pageSize).ToList();
foreach (var data in articleData)
ArticleList.Add(data);
IsBusy = false;
}
[RelayCommand]
public async Task LoadMoreArticleData()
{
if (IsLoading) return;
allArticlesList = await articleService.GetAllArticles();
if (allArticlesList?.Count &gt; 0)
{
IsLoading = true;
var articleData = allArticlesList.Skip(ArticleList.Count).Take(pageSize).ToList();
foreach (var data in articleData)
ArticleList.Add(data);
IsLoading = false;
}
}
}
}

答案1

得分: 0

第一点,我注意到你有两个边框。

一个在CollectionView外部,另一个在CollectionView内部。你想应用样式在哪一个?如果我理解正确,你想应用到内部的那个。没关系,我们可以通过以下方式将样式应用到它(如果你在ContentPage.Resources中定义了VisualStateManager):

<Border StrokeThickness="3.5" Style="{x:StaticResource BorderSelectionStyle}"

通过这一行,我们将样式应用到内部的边框。

如果你在样式中删除x:Key="BorderSelectionStyle",它将默认应用于所有边框。

第二点

你在ContentPage.Resources中定义的VisualStateManager是可以的。它应该可以工作。但第二个变体没有效果。因为你只是将VisualStateManager放在Grid内部。所以它默认会应用于Grid。

CollectionView DataTemplate中的根元素是Border。当我们在CollectionView中点击项目时,根元素Border会更改VisualStates为"Selected"。Border内的其他控件不会更改状态。

所以你可以将第二个变体的位置改变为:

<DataTemplate>
<Border>
<Grid>
</Grid>
<!--在这里放置你的VisualStateManager-->
</Border>

如果你有任何问题,随时提问。

英文:

First, I noticed that you have two Borders.

One is outside the CollectionView, another is inside the CollectionView. Which one you want to apply the style? If I understand correctly, you want to apply to the inner one. Never mind, we could just apply the Style to it using (if you define the VisualStateManager in ContentPage.Resources):

&lt;Border StrokeThickness=&quot;3.5&quot; Style=&quot;{x:StaticResource BorderSelectionStyle}&quot;

Through this line we apply the style to the inner Border.

If you delete x:Key=&quot;BorderSelectionStyle&quot; in Style, it will apply to all Border by default.

Second

The VisualStateManager you define in ContentPage.Resources is ok. It should work. But the second variant has no effect. Because you just put the VisualStateManager inside the Grid. So it will by default apply to the Grid.

The root element in CollectionView DataTemplate is Border. When we click the item in CollectionView, the root element Border will change the VisualStates to "Selected". Other controls in Border will not change the states.

So you could just change the position of the second variant:

&lt;DataTemplate&gt;
&lt;Border&gt;
&lt;Grid&gt;
&lt;/Grid&gt;
&lt;!--put your visualstatemanager here--&gt;
&lt;/Border&gt;

If you have any question, feel free to ask.

huangapple
  • 本文由 发表于 2023年4月7日 03:06:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定