简体   繁体   English

错误消息“无法在不可变对象实例上为'(0)。(1)'设置动画”的按钮,带有RelayCommand

[英]Error message “Cannot animate '(0).(1)' on an immutable object instance” for Button with RelayCommand

I Use a extended button managed with two properties: IsLocked and IsRequired . 我使用由两个属性管理的扩展按钮: IsLockedIsRequired IsLocked deactivates the button by using a RelayCommand : IsLocked使用停用按钮RelayCommand

public class RelayCommand : ICommand
{
    private Action _execute;
    private Func<bool> _canExecute;

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public RelayCommand(Action execute, Func<bool> canExecute = null)
    {
        this._execute = execute;
        this._canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return this._canExecute == null || this._canExecute();
    }

    public void Execute(object parameter)
    {
        this._execute();
    }
}

IsRequired changes the background color of the button with an animation. IsRequired通过动画更改按钮的背景色。

Both properties are dependent and defined like: 这两个属性都是依赖的,并且定义如下:

public Boolean IsRequired
    {
        get { return _isRequired; }
        private set
        {
            if (_isRequired == value)
                return;

            _isRequired = value;

            if (_isRequired)
                this.IsLocked = false;

            NotifyPropertyChanged();
        }
    }

public Boolean IsLocked
    {
        get { return _isLocked; }
        private set
        {
            if (_isLocked == value)
                return;

            _isLocked = value;                

            if (_isLocked)
                this.IsRequired = false;

            NotifyPropertyChanged();
        }
    }

I control the button via properties in a viewmodel and a binding on the properties of the button in the associated view: 我通过视图模型中的属性以及在关联视图中按钮属性的绑定来控制按钮:

IsLocked="{Binding IsLocked}"
IsRequired="{Binding IsRequired}"

I have the error message "Cannot animate '(0).(1)' on an immutable object instance" when I set IsRequired to True in my viewmodel. 当我在视图模型中将IsRequired设置为True时,出现错误消息“无法在不可变对象实例上为'(0)。(1)设置动画” Normally, IsLocked is false (via Setter of the property) but I see that the property IsEnabled is still to False. 通常, IsLocked为false(通过属性的Setter),但是我看到属性IsEnabled仍为False。 So, I have tried to change IsEnabled in a PropertyChangedCallback linked to the dependency property of my extended button. 因此,我试图在链接到扩展按钮的依赖属性的PropertyChangedCallback更改IsEnabled But it's not possible, it's frozen. 但这是不可能的,它已经冻结了。

That works if I don't use the CanExecute method of the RelayCommand anymore and if I bind the property IsEnabled of my extended button directly to the property IsLocked . 这工作,如果我不使用CanExecute的方法RelayCommand了,如果我绑定属性IsEnabled直接我的扩展按钮的财产IsLocked

Any way to continue to use the relay command ? 有什么办法可以继续使用relay命令吗?

EDIT: This is the style of my extended button 编辑:这是我的扩展按钮的样式

<Button x:Class="Client.UserControls.ExtendedButton"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:local="clr-namespace:Client.UserControls"
         mc:Ignorable="d"
         d:DesignHeight="300" d:DesignWidth="300">
<Button.Style>
    <Style TargetType="{x:Type local:ExtendedButton}" BasedOn="{StaticResource {x:Type Button}}">
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsEnabled" Value="True"/>
                    <Condition Property="IsRequired" Value="True"/>
                </MultiTrigger.Conditions>
                <MultiTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard BeginTime="00:00:00"
                                RepeatBehavior="Forever"
                                Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)">
                            <ColorAnimation To="Orange" Duration="0:0:1" AutoReverse="True"/>
                        </Storyboard>
                    </BeginStoryboard>
                </MultiTrigger.EnterActions>
            </MultiTrigger>
        </Style.Triggers>
    </Style>
</Button.Style>

The style of the standard button is defined in another project named Theme with: 标准按钮的样式是在另一个名为Theme的项目中定义的,该项目具有:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:Theme">

<Style TargetType="{x:Type Button}">
    <Style.Resources>
        <ResourceDictionary Source="ColorConstants.xaml"/>
    </Style.Resources>
    <Setter Property="Background" Value="{StaticResource DefaultBackgroundSolidColor}"/>
    <Setter Property="BorderBrush" Value="White"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Height" Value="70"/>
    <Setter Property="Margin" Value="1"/>
    <Setter Property="Padding" Value="5"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}"                           
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        CornerRadius="2"
                        Margin="{TemplateBinding Margin}">
                    <TextBlock Foreground="{TemplateBinding Foreground}"
                               HorizontalAlignment="Center"
                               Margin="{TemplateBinding Padding}"
                               VerticalAlignment="Center">
                        <ContentPresenter/>
                    </TextBlock>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="True">
                        <Setter Property="Background" Value="{StaticResource IsEnabledBackgroundSolidColor}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

This is probably because you did not set a value for the initial background brush in the style with the Storyboard : 这可能是因为您没有使用Storyboard样式设置初始背景笔刷的值:

<Style TargetType="{x:Type local:ExtendedButton}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="{StaticResource DefaultBackgroundSolidColor}"/>
    <Style.Triggers>
        <MultiTrigger>
             ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 : &#39;无法在不可变对象实例上为 &#39;Fill.Color&#39; 设置动画。 - : 'Cannot animate 'Fill.Color' on an immutable object instance.' 无法在不可变对象实例上设置“ Foreground.Color”动画 - Cannot animate 'Foreground.Color' on an immutable object instance 列表框选择产生无法动画化不可变对象错误 - Listbox selection produces Cannot Animate Immutable Object error 使用带故事板抛出的DataTrigger的WPF图像无法在不可变对象实例上设置&#39;((RenderTransform)。(RotateTransform.Angle)&#39;动画 - WPF Image using DataTrigger with Storyboard throwing Cannot animate '((RenderTransform).(RotateTransform.Angle)' on an immutable object instance RelayCommand实现:对象引用未设置为对象的实例 - RelayCommand implementation : Object reference not set to an instance of an object VS dll 引用错误 - 实例对象被创建为不可变对象 - VS dll reference error - Instance object was created as immutable 对象引用未设置为对象错误消息的实例 - Object reference not set to an instance of an object error message RelayCommand WPF上的“启用”按钮 - Enable Button on RelayCommand WPF 未将对象引用设置为对象的实例,WebControl返回了错误消息 - Object reference not set to an instance of an object, error message returned from a webcontrol “无法创建抽象类或接口的实例” C#错误消息 - “Cannot create an instance of the abstract class or interface” C# error message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM