DataStateBehavior -状态未触发

本文关键字:状态 DataStateBehavior | 更新日期: 2023-09-27 18:18:08

在视图模型中加入以下逻辑:

private void ExecuteTestConnectionCommand()
{
    _IsBusy = true;
    _connectionTester.CanConnect(_currentConnectionString);
    _IsBusy = false;
}
public const string IsBusyPropertyName = "IsBusy";    
private bool _IsBusy = false;    
public bool IsBusy
{
    get
    {
        return _IsBusy;
    }
    set
    {
        if (_IsBusy == value)
        {
            return;
        }
        RaisePropertyChanging(IsBusyPropertyName);
        _IsBusy = value;
        RaisePropertyChanged(IsBusyPropertyName);
    }
}

CanConnect有阻塞操作

想要在Test Connection按钮上做一些动画。当IsBusy = true时,希望按钮淡入淡出(或至少某种动画)。

到目前为止,我有:

<Button x:Name="buttonTest" Command="{Binding TestConnectionCommand}" Content="Test">
    <i:Interaction.Behaviors>
        <ei:DataStateBehavior Binding="{Binding IsBusy}" 
            Value="True" TrueState="BusyState" FalseState="NotBusyState"/>
    </i:Interaction.Behaviors>
</Button>

:

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="VisualStateGroup">
        <VisualStateGroup.Transitions>
            <VisualTransition GeneratedDuration="0:0:3" To="BusyState">
                <VisualTransition.GeneratedEasingFunction>
                    <CircleEase EasingMode="EaseOut"/>
                </VisualTransition.GeneratedEasingFunction>
            </VisualTransition>
            <VisualTransition GeneratedDuration="0:0:4" To="NotBusyState">
                <VisualTransition.GeneratedEasingFunction>
                    <BounceEase EasingMode="EaseInOut"/>
                </VisualTransition.GeneratedEasingFunction>
            </VisualTransition>
            <VisualTransition GeneratedDuration="0:0:2">
                <VisualTransition.GeneratedEasingFunction>
                    <BounceEase EasingMode="EaseIn"/>
                </VisualTransition.GeneratedEasingFunction>
            </VisualTransition>
        </VisualStateGroup.Transitions>
        <VisualState x:Name="BusyState">
            <Storyboard AutoReverse="True" RepeatBehavior="Forever"/>
        </VisualState>
        <VisualState x:Name="NotBusyState"/>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

当我点击test按钮时,据我所知没有动画发生。为什么?

DataStateBehavior -状态未触发

不确定,因为这可能完全关闭,但您的属性'IsBusy'没有通过查看私有等效'_IsBusy'来捕获吗?看起来您的模型遵循MVVM结构,具有私有属性,然后按照以下模式设置:

private string _Thing;
Public string Thing
{
    get { return _Thing; }
    set { _Thing = value; }
   RaisePropertyChanged("Thing")
}

传统上Raise属性是在基视图模型中通过继承'INotifyPropertyChanged'接口并实现类似的逻辑来实现的:

public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

通常你传入你的属性的字符串名称,然后系统知道将更改暴露给它的视图。

我知道在我的情况下做的事情与MVVM和一些没有得到提高,通常是我需要实现"RaisePropertyChanged"有效或尝试调用公共属性检查,而不是私人当XAML正在进行更改。