AdaptiveTrigger的视觉状态不起作用

本文关键字:不起作用 状态 视觉 AdaptiveTrigger | 更新日期: 2023-09-27 18:29:43

我在PivotItem中有ListView。我的数据模板是UserControl,它包含两个具有自适应触发器的视觉状态。问题是我的视觉状态不起作用。怎么了?

ListView标记的一部分:

<ListView.ItemTemplate>
          <DataTemplate x:DataType="models:RecommendedAnime">
               <controls:RecomendationControl/>
          </DataTemplate>
</ListView.ItemTemplate>

我的UserControl xaml:

<UserControl
x:Class="MyAnimelistRT.Controls.RecomendationControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:system="using:System"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="WindowStates">
        <VisualState x:Name="NarrowState">
            <VisualState.StateTriggers>
                <AdaptiveTrigger MinWindowWidth="0" />
            </VisualState.StateTriggers>
            <VisualState.Setters>
                <Setter Target="ImageCover.(RelativePanel.Below)" Value="TextBlockCover" />
                <Setter Target="ListViewRecomendations.(RelativePanel.Below)" Value="ImageCover" />
            </VisualState.Setters>
        </VisualState>
        <VisualState x:Name="WideState">
            <VisualState.StateTriggers>
                <AdaptiveTrigger MinWindowWidth="500" />
            </VisualState.StateTriggers>
            <VisualState.Setters>
                <Setter Target="TextBlockCover.(RelativePanel.RightOf)" Value="ImageCover" />
                <Setter Target="ListViewRecomendations.(RelativePanel.RightOf)" Value="ImageCover" />
                <Setter Target="ListViewRecomendations.(RelativePanel.Below)" Value="TextBlockCover" />
            </VisualState.Setters>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<RelativePanel>
    <TextBlock x:Name="TextBlockCover" Style="{StaticResource SubheaderTextBlockStyle}" Text="{x:Bind Item.Title,Mode=OneWay}" Margin="4,0,0,0"/>
    <Image x:Name="ImageCover" HorizontalAlignment="Left" Source="{x:Bind Item.ImageUrl,Mode=OneWay}" Stretch="None"/>
    <ListView x:Name="ListViewRecomendations" ItemsSource="{x:Bind Item.Recomendations,Mode=OneWay}" SelectionMode="None" IsItemClickEnabled="False">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="system:String">
                <TextBlock Style="{StaticResource BodyTextBlockStyle}" Text="{x:Bind}"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</RelativePanel>

我的UserControl代码背后:

public sealed partial class RecomendationControl
{
    public RecommendedAnime Item => (RecommendedAnime) DataContext;
    public RecomendationControl()
    {
        InitializeComponent();
        DataContextChanged += (s, e) => Bindings.Update();
    }
}

AdaptiveTrigger的视觉状态不起作用

这只是一种预感,但你能尝试将UserControl中的所有内容都封装在网格中吗?我记得我也遇到过这样的问题,把它全部(包括VisualStateManager)放在一个网格中会有所帮助。

同时尝试在NarrowState中将MinWindowWidth设置为"1"。根据我的经验,"0"并不总是有效的。