问题
I am working with WPF through blend and Visual Studio 2012. Every time I try to use certain code I keep getting errors like this. For the life of me I can't figure it out. Any help is appreciated, and any explanations would be great.
Here is a screen shot:
Edit: To see the image more clear, right click it and choose "Open in new tab" if available.
Thanks, I updated my code. Now It's giving me a new error.
Screenshot:
Error: Error 1 'Titan.MainWindow.WindowMaximizeButton' is a 'field' but is used like a 'method' C:\Users\Jake\Documents\Expression\Blend 4\Projects\Titan\Titan\MainWindow.xaml 202 91 Titan
XAML Code:
<Button x:Name="WindowMaximizeButton" Content="Button" Width="14.333" HorizontalAlignment="Right" VerticalAlignment="Top" Style="{DynamicResource WindowMaximizeButton}" Click="WindowMaximizeButton" Margin="0,0,48.998,0">
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
x:Class="Titan.MainWindow"
x:Name="Titan"
Title="Titan"
Width="880" Height="474" WindowStyle="None" WindowState="Maximized" WindowStartupLocation="CenterScreen" Background="#FF0F0F0F">
<Window.Resources>
<Style x:Key="ButtonFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#F3F3F3" Offset="0"/>
<GradientStop Color="#EBEBEB" Offset="0.5"/>
<GradientStop Color="#DDDDDD" Offset="0.5"/>
<GradientStop Color="#CDCDCD" Offset="1"/>
</LinearGradientBrush>
<SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/>
<Style x:Key="WindowExitButton" TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
<Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
<Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Resources>
<Storyboard x:Key="MouseOver">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="rectangle">
<EasingColorKeyFrame KeyTime="0:0:0.2" Value="Maroon"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<Grid x:Name="grid" Height="20" Margin="-14,0,-3,0" Width="Auto">
<Rectangle x:Name="rectangle" Stroke="Black">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF210000" Offset="0.978"/>
<GradientStop Color="#FF4D0000"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Image x:Name="image" Source="appbar.close.png" Margin="-5,-3.5,-4.5,-3.5"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true"/>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="MouseOver_BeginStoryboard"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard x:Name="MouseOver_BeginStoryboard" Storyboard="{StaticResource MouseOver}"/>
</Trigger.EnterActions>
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="true"/>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="WindowMaximizeButton" TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
<Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
<Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Resources>
<Storyboard x:Key="MouseOver">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="rectangle">
<EasingColorKeyFrame KeyTime="0:0:0.2" Value="Maroon"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<Grid x:Name="grid" Height="20" Margin="-14,0,-3,0" Width="Auto">
<Rectangle x:Name="rectangle" Stroke="Black">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF210000" Offset="0.978"/>
<GradientStop Color="#FF4D0000"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Image x:Name="image" Source="appbar.tablet.windows.png" Margin="-5,-3.5,-4.5,-3.5"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true"/>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="MouseOver_BeginStoryboard"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard x:Name="MouseOver_BeginStoryboard" Storyboard="{StaticResource MouseOver}"/>
</Trigger.EnterActions>
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="true"/>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="WindowMinimizeButton" TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
<Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
<Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Resources>
<Storyboard x:Key="MouseOver">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="rectangle">
<EasingColorKeyFrame KeyTime="0:0:0.2" Value="Maroon"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<Grid x:Name="grid" Height="20" Margin="-14,0,-3,0" Width="Auto">
<Rectangle x:Name="rectangle" Stroke="Black">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF210000" Offset="0.978"/>
<GradientStop Color="#FF4D0000"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Image x:Name="image" Source="appbar.window.minimize.png" Margin="-5,-3.5,-4.5,-3.5" Opacity="0.995"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true"/>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="MouseOver_BeginStoryboard"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard x:Name="MouseOver_BeginStoryboard" Storyboard="{StaticResource MouseOver}"/>
</Trigger.EnterActions>
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="true"/>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter" SourceName="WindowExitButton"/>
</Window.Triggers>
<Grid x:Name="LayoutRoot">
<Rectangle Height="84" VerticalAlignment="Top">
<Rectangle.Effect>
<DropShadowEffect BlurRadius="25" Opacity="0.495"/>
</Rectangle.Effect>
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="1"/>
<GradientStop Color="#FF101010"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Menu Height="35" VerticalAlignment="Top" Background="{x:Null}" Margin="0,49,0,0"/>
<Button x:Name="WindowExitButton" Content="Button" Width="31" HorizontalAlignment="Right" VerticalAlignment="Top" Style="{DynamicResource WindowExitButton}" Click="WindowExitButton_Click" Margin="0,0,2,0">
<Button.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF3F3F3" Offset="0"/>
<GradientStop Color="#FFEBEBEB" Offset="0.5"/>
<GradientStop Color="#FFDDDDDD" Offset="0.5"/>
<GradientStop Color="#FFCDCDCD" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
</Button>
<Button x:Name="WindowMaximizeButton" Content="Button" Width="14.333" HorizontalAlignment="Right" VerticalAlignment="Top" Style="{DynamicResource WindowMaximizeButton}" Click="WindowExitButton_Click" Margin="0,0,48.998,0">
<Button.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF3F3F3" Offset="0"/>
<GradientStop Color="#FFEBEBEB" Offset="0.5"/>
<GradientStop Color="#FFDDDDDD" Offset="0.5"/>
<GradientStop Color="#FFCDCDCD" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
</Button>
<Button x:Name="WindowMinimizeButton" Content="Button" Width="14.333" HorizontalAlignment="Right" VerticalAlignment="Top" Style="{DynamicResource WindowMinimizeButton}" Click="WindowExitButton_Click" Margin="0,0,79.332,0">
<Button.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF3F3F3" Offset="0"/>
<GradientStop Color="#FFEBEBEB" Offset="0.5"/>
<GradientStop Color="#FFDDDDDD" Offset="0.5"/>
<GradientStop Color="#FFCDCDCD" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
</Button>
</Grid>
</Window>
回答1:
this is WPF. Not windows forms. So use like
this.WindowState = System.Windows.WindowState.Normal;
FormWindowState is an enumerator for Windows Forms Applications
You might be migrating from Windows Forms development to WPF, thats why you're confused
The next error is in this line
<Button x:Name="WindowMaximizeButton" Content="Button" Width="14.333" HorizontalAlignment="Right" VerticalAlignment="Top" Style="{DynamicResource WindowMaximizeButton}" Click="WindowMaximizeButton" Margin="0,0,48.998,0">
at here
Click="WindowMaximizeButton"
Here you have to assign a method name inside from the class that will be triggered when you click the button.
But you have given the name of the button itself
You need to set it like
Click="WindowMaximizeButton_Click"
来源:https://stackoverflow.com/questions/29133586/formwindowstate-does-not-exist-in-the-current-context