0

这个属性让我发疯,我的 wpf 表单中有一个按钮,我想在这里实现的是

当我单击按钮时,禁用主窗口并弹出消息框。

这是后面的代码

    private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
    {

        if (sender is Button)
        {
            Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    Application.Current.MainWindow.IsEnabled = false;
                    MessageBox.Show(Application.Current.MainWindow, "test");

                }));
           }
    }

初始化我的主窗口

protected override void InitializeShell()
    {                    
        base.InitializeShell();

        Application.Current.MainWindow = (Shell)this.Shell;
        Application.Current.MainWindow.Show();
    }

它在“Application.Current.MainWindow.IsEnabled = false;”处抛出异常“'[Unknown]' 属性未指向路径'(0).(1)[1].(2)' 中的 DependencyObject。” .

这是几行的堆栈跟踪

       at System.Windows.Media.Animation.Storyboard.VerifyPathIsAnimatable(PropertyPath path)
   at System.Windows.Media.Animation.Storyboard.ClockTreeWalkRecursive(Clock currentClock, DependencyObject containingObject, INameScope nameScope, DependencyObject parentObject, String parentObjectName, PropertyPath parentPropertyPath, HandoffBehavior handoffBehavior, HybridDictionary clockMappings, Int64 layer)

如果我绕过它,它会显示消息框,但我想知道为什么它会在 isEnable=false 抛出异常?

非常感谢任何帮助。

提前致谢

我的 shell.xaml

<Window x:Class="Webster.Client.Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:Webster.Client"
    xmlns:cal="http://www.codeplex.com/CompositeWPF"         
    xmlns:Controls="clr-namespace:Webster.Client.Controls"
    xmlns:inf="clr-namespace:Webster.Client.Infrastructure;assembly=Webster.Client.Infrastructure"
    Title="Webster - News Segmentation and Encoding" WindowStartupLocation="CenterScreen" WindowState="Normal"
    Icon="/Webster.Client;component/Resources/film_clip64.png" SizeToContent="Manual" HorizontalAlignment="Center"
    MinWidth="390" Loaded="Window_Loaded">

<!-- window -->
<Window.Background>
    <ImageBrush ImageSource="Resources/background.png"  Stretch="Fill"/>
</Window.Background>

<Window.Resources>
    <DataTemplate DataType="{x:Type inf:ExtendedHeader}">
        <StackPanel Orientation="Horizontal" ToolTip="{Binding Path=ToolTip}">
            <Image Source="{Binding Path=IconUri}" Width="16" Height="16" />
            <TextBlock Text="{Binding Path=Title}" />
        </StackPanel>
    </DataTemplate>
    <Storyboard x:Key="InTransition">
        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Logo" Storyboard.TargetProperty="(UIElement.Opacity)">
            <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
            <SplineDoubleKeyFrame KeyTime="00:00:02" Value="0"/>
            <SplineDoubleKeyFrame KeyTime="00:00:02.5000000" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ContentGrid" Storyboard.TargetProperty="(UIElement.Opacity)">
            <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
            <SplineDoubleKeyFrame KeyTime="00:00:01" Value="0"/>
            <SplineDoubleKeyFrame KeyTime="00:00:01.5000000" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ContentGrid" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
            <SplineDoubleKeyFrame KeyTime="00:00:00" Value="-72"/>
            <SplineDoubleKeyFrame KeyTime="00:00:01" Value="-157"/>
            <SplineDoubleKeyFrame KeySpline="0.5,0,0.5,1" KeyTime="00:00:01.5000000" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Logo" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
            <SplineDoubleKeyFrame KeyTime="00:00:00" Value="-50"/>
            <SplineDoubleKeyFrame KeyTime="00:00:02" Value="-101"/>
            <SplineDoubleKeyFrame KeySpline="0,0,0,1" KeyTime="00:00:02.5000000" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="MainToolbar" Storyboard.TargetProperty="(UIElement.Opacity)">
            <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
            <SplineDoubleKeyFrame KeyTime="00:00:01.5000000" Value="0"/>
            <SplineDoubleKeyFrame KeyTime="00:00:02" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="MainToolbar" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
            <SplineDoubleKeyFrame KeyTime="00:00:00" Value="100"/>
            <SplineDoubleKeyFrame KeyTime="00:00:01.5000000" Value="100"/>
            <SplineDoubleKeyFrame KeyTime="00:00:02" Value="0" KeySpline="0,0.495999991893768,0.504000008106232,1"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>
<Window.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
        <BeginStoryboard Storyboard="{StaticResource InTransition}"/>
    </EventTrigger>
</Window.Triggers>

<!-- content grid -->
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Top" >
    <Grid RenderTransformOrigin="0.5,0.5" Margin="0,0,0,0" Grid.RowSpan="2">
        <Grid.RowDefinitions>
            <RowDefinition Height="20"/>
            <RowDefinition Height="*" />
            <RowDefinition Height="25"/>
            <RowDefinition Height="20"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="30*" />
            <ColumnDefinition Width="627*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <!-- logo -->
        <Canvas x:Name="Logo" Grid.ColumnSpan="3" RenderTransformOrigin="0.5,0.5">
            <Canvas.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </Canvas.RenderTransform>
            <TextBlock 
                Text="" 
                TextAlignment="Center" 
                TextWrapping="NoWrap" 
                Foreground="#FFC4CFD6" 
                FontSize="50" 
                Opacity="0.85" 
                Canvas.Left="9" 
                Canvas.Top="6" 
                VerticalAlignment="Stretch" 
                HorizontalAlignment="Stretch" 
                Height="60" 
                Width="{Binding ElementName=Logo,Path=ActualWidth}"
                FontWeight="Bold" />
        </Canvas>

        <!-- main bar -->
        <ItemsControl x:Name="MainToolbar" cal:RegionManager.RegionName="{x:Static inf:RegionNames.MainToolBarRegion}" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,0,0" Grid.Row="1" RenderTransformOrigin="0.5,0.5" Grid.Column="1">
            <ItemsControl.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </ItemsControl.RenderTransform>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>                        
                    <StackPanel Orientation="Horizontal" Margin="10, 0, 10, 10">
                    </StackPanel>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>

        <!-- content -->            
        <Grid x:Name="ContentGrid" Grid.Row="1" RenderTransformOrigin="0.5,0.5" Grid.ColumnSpan="2">
            <Grid.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </Grid.RenderTransform>

            <Controls:AnimatedTabControlWebster 
                x:Name="StoryTab"
                SelectedIndex="0"  
                VerticalAlignment="Stretch"
                HorizontalAlignment="Stretch"
                ItemContainerStyle="{StaticResource ShellTabItemStyle}" 
                Background="{StaticResource headerBarBG}" 
                cal:RegionManager.RegionName="{x:Static inf:RegionNames.MainRegion}"
                AutomationProperties.AutomationId="PositionSummaryTab" IsEnabled="True">
            </Controls:AnimatedTabControlWebster>
        </Grid>

        <!-- footer -->
        <StackPanel
                Grid.Row="2"
                Grid.Column="0" 
                Grid.ColumnSpan="3"                     
                VerticalAlignment="Stretch"
                HorizontalAlignment="Stretch"
                Orientation="Horizontal"
                FlowDirection="RightToLeft"
                Margin="0 0 10 0"
                >
            <Button Name="btnExit" Click="btnExit_Click">Exit</Button>
        </StackPanel>
        <StackPanel
                Grid.Row="3"
                Grid.Column="0" 
                Grid.ColumnSpan="3"     
                VerticalAlignment="Stretch"
                HorizontalAlignment="Center"
                Orientation="Horizontal"
                >
            <ItemsControl 
                    cal:RegionManager.RegionName="{x:Static inf:RegionNames.MainFooterRegion}" 
                    />
        </StackPanel>
    </Grid>
</Grid>

4

0 回答 0