DataStateBehavior - 未开火的国家?

时间:2014-08-15 21:09:44

标签: c# wpf mvvm

在视图模型中有这个逻辑:

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具有阻止操作。

想要在“测试连接”按钮上做一些动画以获得乐趣。当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>

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

1 个答案:

答案 0 :(得分:0)

不确定,因为这可能是完全关闭但是你的财产'IsBusy'没有通过查看私人等效'_IsBusy'被捕获?看起来你的模型遵循MVVM结构,即拥有私有属性,然后按照模式设置:

private string _Thing;

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

传统上,通过继承'INotifyPropertyChanged'接口并实现类似的逻辑,大多数时候在基本视图模型中实现了Raise属性:

public event PropertyChangedEventHandler PropertyChanged;

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

通常,您传入属性的字符串名称,然后系统知道将更改公开给它以进行查看。

我知道在我的情况下使用MVVM并且没有引发某些事情,通常我需要有效地实现'RaisePropertyChanged'或者在XAML进行更改时尝试调用PUBLIC PROPERTY检查而不是私有。