我正在编写使用视觉状态的UWP应用,并且在IsActive上绑定StateTrigger时遇到问题。

我们正在使用MVVM,我需要能够更改VisualState的状态。

我目前的例子:

XAML

<Page
   ...
   >

    <Page.Resources>
        <local:InvBoolConverter x:Key="InvBoolConverter" />
    </Page.Resources>

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" x:Name="MainGrid">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="textSwitcher">
                <VisualState>
                    <VisualState.StateTriggers>
                        <StateTrigger IsActive="{Binding IsRedTrigger, Converter={StaticResource InvBoolConverter}}" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="txtBlock.Foreground" Value="Blue" />
                    </VisualState.Setters>
                </VisualState>
                <VisualState>
                    <VisualState.StateTriggers>
                        <StateTrigger IsActive="{Binding IsRedTrigger}" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="txtBlock.Foreground" Value="Red" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>


        <StackPanel Orientation="Vertical">
            <Button Content="Switch" Command="SwitchClickCommand" />
        </StackPanel>

        <TextBlock x:Name="txtBlock" Margin="100" Foreground="Blue">My Text</TextBlock>
    </Grid>
</Page>


查看模型

namespace ...
{
    public class MainPageViewModel : BindableBase
    {

        public MainPageViewModel()
        {
            SwitchClickCommand = new DelegateCommand(_SwitchButtonClicked);
        }

        public readonly ICommand SwitchClickCommand;

        bool _IsRedTrigger;

        public bool IsRedTrigger
        {
            get { return _IsRedTrigger; }
            set
            {
                _IsRedTrigger = value;
                OnPropertyChanged();
            }
        }

        void SwitchButtonClicked()
        {
            IsRedTrigger = !IsRedTrigger;
        }
    }
}


除了VisualState并未实际更改之外,其他所有内容似乎都正常运行。

我是否正确使用StateTrigger

谢谢!

最佳答案

好的,您没有提到您使用Prism进行开发,现在我可以理解您的问题了。

Command="SwitchClickCommand"之所以不能使用,是因为:


  无法将文本值SwitchClickCommand分配给类型Command的属性Icommand


我可以理解这可能是拼写错误,因为您想清除帖子中的代码。我想问题是实际上您没有正确设置页面的DataContext。完成此操作后,您的代码将在我的身边完美运行:

<Page.Resources>
    <local:InvBoolConverter x:Key="InvBoolConverter" />
</Page.Resources>
<Page.DataContext>
    <local:MainPageViewModel x:Name="vm" />
</Page.DataContext>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" x:Name="MainGrid">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="textSwitcher">
            <VisualState>
                <VisualState.StateTriggers>
                    <StateTrigger IsActive="{Binding IsRedTrigger, Converter={StaticResource InvBoolConverter}}" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="txtBlock.Foreground" Value="Blue" />
                </VisualState.Setters>
            </VisualState>
            <VisualState>
                <VisualState.StateTriggers>
                    <StateTrigger IsActive="{Binding IsRedTrigger}" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="txtBlock.Foreground" Value="Red" />
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

    <StackPanel Orientation="Vertical">
        <Button Content="Switch" Command="{x:Bind vm.SwitchClickCommand}" />
    </StackPanel>

    <TextBlock x:Name="txtBlock" Margin="100" Foreground="Blue">My Text</TextBlock>
</Grid>


我将您的代码SwitchClickCommand = new DelegateCommand(_SwitchButtonClicked);更改为SwitchClickCommand = new DelegateCommand(SwitchButtonClicked);,这也是错字吗?

顺便说一句,我使用的是Prism 6.0,我的操作系统版本是10586。

09-27 16:11