程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> WPF主動隱蔽的新聞框的實例代碼

WPF主動隱蔽的新聞框的實例代碼

編輯:C#入門知識

WPF主動隱蔽的新聞框的實例代碼。本站提示廣大學習愛好者:(WPF主動隱蔽的新聞框的實例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是WPF主動隱蔽的新聞框的實例代碼正文


(鼠標放上去將一向顯示,移開動畫持續),供給normal和error兩種邊框。

引見:傳統切實其實定,撤消,OK,CANCAL之類的對話框太繁瑣了,因為項目須要而出生的仿手機式提醒對話框。固然傳統的對話框項目中也有,這裡就不做引見了。

進場和登場動畫做得很簡略,就用Blend隨意鼓搗了一番,遷就用吧。

預覽後果以下:

思緒其實很簡略:將窗體通明化->結構和款式設計->後台傳值挪用。

預備任務:Microsoft.Expression.Interactions.dll和System.Windows.Interactivity.dll的援用。Blend中年夜多半行動須要須要這2個dll,必備啊!

1,將窗體通明化,無邊框化:(在.net 4.5中 拖動和縮放窗體不再用本身寫代碼了,集成的 <WindowChrome/>便可以完成拖動縮放窗體等諸多功效。)

症結設置以下:

AllowsTransparency="True" HorizontalAlignment="Center" Background="Transparent" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" Window

2,結構,重要包含初始結構和動畫過渡2個方面:

靜態界面結構:

<Grid  HorizontalAlignment="Center" VerticalAlignment="Top" x:Name="back">
    <Border Padding="38 0" x:Name="br" HorizontalAlignment="Center" VerticalAlignment="Center">
      <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseEnter">
          <ei:ControlStoryboardAction Storyboard="{StaticResource ShowSb}" ControlStoryboardOption="Stop"/>
        </i:EventTrigger>
        <i:EventTrigger EventName="MouseLeave">
          <ei:ControlStoryboardAction Storyboard="{StaticResource MouseLeave}"/>
        </i:EventTrigger>
      </i:Interaction.Triggers>
      <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
        <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
          <Grid.RowDefinitions>
            <RowDefinition Height="12"></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="12"></RowDefinition>
          </Grid.RowDefinitions>
          <Border Visibility="Visible" x:Name="grid1" Grid.RowSpan="3" RenderTransformOrigin="0.5,0.5" BorderBrush="#00A0E9" BorderThickness="1" CornerRadius="8">
            <Border.RenderTransform>
              <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform/>
                <TranslateTransform/>
              </TransformGroup>
            </Border.RenderTransform>
            <Border.Background>
              <LinearGradientBrush EndPoint="1,1" MappingMode="RelativeToBoundingBox" StartPoint="0,0">
                <GradientStop Color="#EFF0F2" Offset="0.75"/>
                <GradientStop Color="#EFF0F2" Offset="0.25"/>
                <GradientStop Color="#EFF0F2" Offset="1"/>
                <GradientStop Color="#EFF0F2"/>
              </LinearGradientBrush>
            </Border.Background>
          </Border>
          <Border x:Name="grid2" Visibility="Visible" Opacity="1" Grid.RowSpan="3" RenderTransformOrigin="0.5,0.5" BorderBrush="#F35150" BorderThickness="1" CornerRadius="8">
            <Border.RenderTransform>
              <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform/>
                <TranslateTransform/>
              </TransformGroup>
            </Border.RenderTransform>
            <Border.Background>
              <LinearGradientBrush EndPoint="1,1" MappingMode="RelativeToBoundingBox" StartPoint="0,0">
                <GradientStop Color="#EFF0F2" Offset="0.75"/>
                <GradientStop Color="#EFF0F2" Offset="0.25"/>
                <GradientStop Color="#EFF0F2" Offset="1"/>
                <GradientStop Color="#EFF0F2"/>
              </LinearGradientBrush>
            </Border.Background>
          </Border>
          <TextBlock  Margin="38 0 38 0" Grid.Row="1" FontSize="16" Foreground="#64676d" x:Name="tb" Text="{Binding Message,RelativeSource={RelativeSource AncestorType=Window},FallbackValue=掉敗信息}" TextWrapping="Wrap" MinWidth="200" VerticalAlignment="Center" HorizontalAlignment="Center" RenderTransformOrigin="0.5,0.5" MaxWidth="600" TextAlignment="Center" FontFamily="Microsoft YaHei">
            <TextBlock.RenderTransform>
              <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform/>
                <TranslateTransform/>
              </TransformGroup>
            </TextBlock.RenderTransform></TextBlock>
        </Grid>
      </Grid>
    </Border>
  </Grid>

進入和加入的動畫掌握:

<Storyboard x:Key="ShowSb" Completed="Storyboard_Completed">
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="grid1">
        <EasingDoubleKeyFrame KeyTime="0" Value="{Binding YOffSet}"/>
        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
        <EasingDoubleKeyFrame KeyTime="0:0:3" Value="0"/>
        <EasingDoubleKeyFrame KeyTime="0:0:3.5" Value="{Binding YOffSet}"/>
      </DoubleAnimationUsingKeyFrames>
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="grid2">
        <EasingDoubleKeyFrame KeyTime="0" Value="{Binding YOffSet}"/>
        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
        <EasingDoubleKeyFrame KeyTime="0:0:3" Value="0"/>
        <EasingDoubleKeyFrame KeyTime="0:0:3.5" Value="{Binding YOffSet}"/>
      </DoubleAnimationUsingKeyFrames>
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="tb">
        <EasingDoubleKeyFrame KeyTime="0" Value="{Binding YOffSet}"/>
        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
        <EasingDoubleKeyFrame KeyTime="0:0:3" Value="0"/>
        <EasingDoubleKeyFrame KeyTime="0:0:3.5" Value="{Binding YOffSet}"/>
      </DoubleAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="MouseLeave" Completed="Storyboard_Completed">
      <DoubleAnimationUsingKeyFrames  Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid1">
        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
      </DoubleAnimationUsingKeyFrames>
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid2">
        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
      </DoubleAnimationUsingKeyFrames>
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="tb">
        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
      </DoubleAnimationUsingKeyFrames>
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="grid1">
        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="{Binding YOffSet}"/>
      </DoubleAnimationUsingKeyFrames>
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="grid2">
        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="{Binding YOffSet}"/>
      </DoubleAnimationUsingKeyFrames>
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="tb">
        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="{Binding YOffSet}"/>
      </DoubleAnimationUsingKeyFrames>
    </Storyboard>

留意:鼠標挪動上去停滯動畫和移出後疾速消逝的代碼為:

<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<ei:ControlStoryboardAction Storyboard="{StaticResource ShowSb}" ControlStoryboardOption="Stop"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseLeave">
<ei:ControlStoryboardAction Storyboard="{StaticResource MouseLeave}"/>
</i:EventTrigger>
</i:Interaction.Triggers> 

後台代碼邏輯:

private bool iserror = false;
public void Show(string messageBoxText, bool iserror = false)
{
this.iserror = iserror;
this.Message = messageBoxText;
this.Show();
}
public OnlyShowMessageBox()
{
InitializeComponent();
this.DataContext = new model() { YOffSet = -300d };
this.Loaded += (y, k) =>
{
this.Top = 41;
this.Left = (SystemParameters.WorkArea.Width) / 2 - this.ActualWidth / 2;
if (iserror)
{
this.grid.Visibility = Visibility.Collapsed;
}
else { this.grid.Visibility = Visibility.Collapsed; }
(this.Resources["ShowSb"] as Storyboard).Begin();
};
}
private void Storyboard_Completed(object sender, EventArgs e)
{
this.Close();
} 

個中 :

public class model : ModelBase
{
private double YOffset;
public double YOffSet
{
get { return YOffset; }
set
{
YOffset = value;
RaisePropertyChangedEvent("YOffSet");
}
}
} 

個中ModelBase在 可分組的選擇框控件(MVVM下)(Toggle款式 仿制單選框RadioButton,復選框CheckBox功效) 中有引見。

最初,挪用辦法:

new OnlyShowMessageBox().Show("請留意:後方高能,制止入內!", false);

或許

new OnlyShowMessageBox().Show("不存在此對象!", true);

以上所述是小編給年夜家引見的WPF主動隱蔽的新聞框的全體論述,願望對年夜家有所贊助,假如年夜家想懂得更多內容敬請存眷!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved