Tuesday, July 10, 2012

Creating and Using EventLog in WindowService

First Drag and Drop EventLog component into Component design from ToolBox 


Event log (named : eventLog1) is added into you component class.


Create New Event Log in your window service Component class


 public EmployeeStatusService()
 {
     InitializeComponent();
     if (!System.Diagnostics.EventLog.SourceExists("MyLogInfo"))
     {
          System.Diagnostics.EventLog.CreateEventSource(
                    "MyLogInfo", "HirenLog");
     }
     eventLog1.Source = "SmartNetDeviceInfo";
     eventLog1.Log = "SmartNetLog";
 } 

Write Log entry on created log


 protected override void OnStart(string[] args)
 {
     eventLog1.WriteEntry("Service Started");
 } 

Page Transition within Frame in WPF

Create Resource for each RenderTransform

 <Window.Resources>
         <!-- Slide and Fade -->
        <Storyboard x:Key="SlideAndFadeIn">
            <ThicknessAnimation Duration="0:0:.75"
                                Storyboard.TargetProperty="Margin"
                                From="500,0,-500,0"
                                To="0"
                                DecelerationRatio=".9" />
            <DoubleAnimation Duration="0:0:.25"
                             Storyboard.TargetProperty="Opacity"
                             From="0"
                             To="1" />
        </Storyboard>
        <Storyboard x:Key="SlideAndFadeOut">
            <ThicknessAnimation Duration="0:0:.5"
                                Storyboard.TargetProperty="Margin"
                                To="-500,0,500,0"
                                AccelerationRatio=".9" />
            <DoubleAnimation Duration="0:0:.5"
                             Storyboard.TargetProperty="Opacity"
                             To="0" />
        </Storyboard>
        <!-- Fade -->
        <Storyboard x:Key="FadeIn">
            <DoubleAnimation Duration="0:0:.25"
                             Storyboard.TargetProperty="Opacity"
                             From="0"
                             To="1" />
        </Storyboard>
        <Storyboard x:Key="FadeOut">
            <DoubleAnimation Duration="0:0:.5"
                             Storyboard.TargetProperty="Opacity"
                             To="0" />
        </Storyboard>
        <!-- Slide -->
        <Storyboard x:Key="SlideIn">
            <ThicknessAnimation Duration="0:0:.75"
                                Storyboard.TargetProperty="Margin"
                                From="500,0,-500,0"
                                To="0"
                                DecelerationRatio=".9" />
        </Storyboard>
        <Storyboard x:Key="SlideOut">
            <ThicknessAnimation Duration="0:0:.5"
                                Storyboard.TargetProperty="Margin"
                                To="-500,0,500,0"
                                AccelerationRatio=".9" />
        </Storyboard>
        <!-- Grow -->
        <Storyboard x:Key="GrowIn">
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
                             From="0"
                             To="1"
                             Duration="0:0:.75"
                             DecelerationRatio=".9" />
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
                             From="0"
                             To="1"
                             Duration="0:0:.75"
                             DecelerationRatio=".9" />
        </Storyboard>
        <Storyboard x:Key="GrowOut">
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
                             To="0"
                             Duration="0:0:.75"
                             AccelerationRatio=".9" />
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
                             To="0"
                             Duration="0:0:.75"
                             AccelerationRatio=".9" />
        </Storyboard>
        <!-- Grow and Fade -->
        <Storyboard x:Key="GrowAndFadeIn">
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
                             From="0"
                             To="1"
                             Duration="0:0:.75"
                             DecelerationRatio=".9" />
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
                             From="0"
                             To="1"
                             Duration="0:0:.75"
                             DecelerationRatio=".9" />
            <DoubleAnimation Duration="0:0:.25"
                             Storyboard.TargetProperty="Opacity"
                             From="0"
                             To="1" />
        </Storyboard>
        <Storyboard x:Key="GrowAndFadeOut">
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
                             To="0"
                             Duration="0:0:.75"
                             AccelerationRatio=".9" />
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
                             To="0"
                             Duration="0:0:.75"
                             AccelerationRatio=".9" />
            <DoubleAnimation Duration="0:0:.75"
                             Storyboard.TargetProperty="Opacity"
                             To="0" />
        </Storyboard>
        <!-- Flip -->
        <Storyboard x:Key="FlipIn">
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleX)"
                             From="-100"
                             To="0"
                             Duration="0:0:.75"
                             DecelerationRatio=".9" />
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleY)"
                             From="-100"
                             To="0"
                             Duration="0:0:.75"
                             DecelerationRatio=".9" />
        </Storyboard>
        <Storyboard x:Key="FlipOut">
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleX)"
                             To="100"
                             Duration="0:0:.75"
                             AccelerationRatio=".9" />
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleY)"
                             To="100"
                             Duration="0:0:.75"
                             AccelerationRatio=".9" />
        </Storyboard>
        <!-- Flip and Fade -->
        <Storyboard x:Key="FlipAndFadeIn">
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleX)"
                             From="-100"
                             To="0"
                             Duration="0:0:.75"
                             DecelerationRatio=".9" />
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleY)"
                             From="-100"
                             To="0"
                             Duration="0:0:.75"
                             DecelerationRatio=".9" />
            <DoubleAnimation Duration="0:0:.25"
                             Storyboard.TargetProperty="Opacity"
                             From="0"
                             To="1" />
        </Storyboard>
        <Storyboard x:Key="FlipAndFadeOut">
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleX)"
                             To="100"
                             Duration="0:0:.75"
                             AccelerationRatio=".9" />
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleY)"
                             To="100"
                             Duration="0:0:.75"
                             AccelerationRatio=".9" />
            <DoubleAnimation Duration="0:0:.75"
                             Storyboard.TargetProperty="Opacity"
                             To="0" />
        </Storyboard>
        <!-- Spin -->
        <Storyboard x:Key="SpinIn">
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"
                             From="-360"
                             To="0"
                             Duration="0:0:.75"
                             DecelerationRatio=".9" />
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
                             From="0"
                             To="1"
                             Duration="0:0:.75"
                             DecelerationRatio=".9" />
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
                             From="0"
                             To="1"
                             Duration="0:0:.75"
                             DecelerationRatio=".9" />
        </Storyboard>
        <Storyboard x:Key="SpinOut">
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"
                             To="360"
                             Duration="0:0:.75"
                             AccelerationRatio=".9" />
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
                             To="0"
                             Duration="0:0:.75"
                             AccelerationRatio=".9" />
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
                             To="0"
                             Duration="0:0:.75"
                             AccelerationRatio=".9" />
        </Storyboard>
        <!-- Spin and Fade -->
        <Storyboard x:Key="SpinAndFadeIn">
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"
                             From="-360"
                             To="0"
                             Duration="0:0:.75"
                             DecelerationRatio=".9" />
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
                             From="0"
                             To="1"
                             Duration="0:0:.75"
                             DecelerationRatio=".9" />
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
                             From="0"
                             To="1"
                             Duration="0:0:.75"
                             DecelerationRatio=".9" />
            <DoubleAnimation Duration="0:0:.25"
                             Storyboard.TargetProperty="Opacity"
                             From="0"
                             To="1" />
        </Storyboard>
        <Storyboard x:Key="SpinAndFadeOut">
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"
                             To="360"
                             Duration="0:0:.75"
                             AccelerationRatio=".9" />
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
                             To="0"
                             Duration="0:0:.75"
                             AccelerationRatio=".9" />
            <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
                             To="0"
                             Duration="0:0:.75"
                             AccelerationRatio=".9" />
            <DoubleAnimation Duration="0:0:.75"
                             Storyboard.TargetProperty="Opacity"
                             To="0" />
        </Storyboard>
    </Window.Resources> 

Add Frame Control into your Page and Set Transforms in TransformGroups


<Frame x:Name="FrameWithinGrid"
               ContentRendered="FrameWithinGrid_ContentRendered"
               Source="Home.xaml">
            <Frame.RenderTransform>
               <TransformGroup>
                    <ScaleTransform ScaleX="1"
                                    ScaleY="1"
                                    CenterX="{Binding ElementName=FrameWithinGrid, Path=ActualWidth, Converter={StaticResource CenterConverter}}"
                                    CenterY="{Binding ElementName=FrameWithinGrid, Path=ActualHeight, Converter={StaticResource CenterConverter}}" />
                    <SkewTransform AngleX="0"
                                   AngleY="0"
                                   CenterX="{Binding ElementName=FrameWithinGrid, Path=ActualWidth, Converter={StaticResource CenterConverter}}"
                                   CenterY="{Binding ElementName=FrameWithinGrid, Path=ActualHeight, Converter={StaticResource CenterConverter}}" />
                    <RotateTransform Angle="0"
                                     CenterX="{Binding ElementName=FrameWithinGrid, Path=ActualWidth, Converter={StaticResource CenterConverter}}"
                                     CenterY="{Binding ElementName=FrameWithinGrid, Path=ActualHeight, Converter={StaticResource CenterConverter}}" />
                    <TranslateTransform X="0"
                                        Y="0" />
                </TransformGroup>
            </Frame.RenderTransform>
        </Frame> 

Use Your Resource as a Storyboard in FrameWithinGrid_ContentRendered Event


  private void FrameWithinGrid_ContentRendered(object sender, EventArgs e)
  {  
     Storyboard sb = this.Resources["GrowOut"] as Storyboard;
     sb.Completed += new EventHandler(sb_Completed);
     sb.Begin(FrameWithinGrid);
  }
  void sb_Completed(object sender, EventArgs e)
  {
     Storyboard sb = this.Resources["GrowIn"] as Storyboard;          
     sb.Begin(FrameWithinGrid);
  } 

WPF Frame Navigation Animation : TranslateTransform and RotateTransform

RotateTransform

MainWindow.xaml

<Frame Name="FrameWithinGrid"
               ContentRendered="FrameWithinGrid_ContentRendered"             
               Source="Home.xaml">
            <Frame.RenderTransform>
              <RotateTransform Angle="0"
                               CenterX="425"
                               CenterY="300" />
            </Frame.RenderTransform>
</Frame>

MainWindow.xaml.cs

 
 private void FrameWithinGrid_ContentRendered(object sender, EventArgs e)
 {
    Storyboard storyboard = new Storyboard();
    DoubleAnimation rotateAnimation = new DoubleAnimation();
    rotateAnimation.Duration = TimeSpan.FromMilliseconds(1000);
    rotateAnimation.From = 0;
    rotateAnimation.To = 180;        
    storyboard.Children.Add(rotateAnimation);
    Storyboard.SetTargetProperty(rotateAnimation,
          new PropertyPath("RenderTransform.Angle"));
    Storyboard.SetTarget(rotateAnimation, FrameWithinGrid);
    storyboard.Begin(); 
 }
 
 
TranslateTransform

MainWindow.xaml

<Frame Name="FrameWithinGrid"
               ContentRendered="FrameWithinGrid_ContentRendered"             
               Source="Home.xaml">
            <Frame.RenderTransform>
                <TranslateTransform X="0"
                                    Y="0" />
            </Frame.RenderTransform>
</Frame>

MainWindow.xaml.cs

 
 private void FrameWithinGrid_ContentRendered(object sender, EventArgs e)
 {
    Storyboard storyboard = new Storyboard();
    DoubleAnimation transAnimation = new DoubleAnimation();
    transAnimation.Duration = TimeSpan.FromMilliseconds(500);
    transAnimation.From = 0;
    transAnimation.To = 800;
    storyboard.Children.Add(transAnimation);
    Storyboard.SetTargetProperty(transAnimation, new PropertyPath("RenderTransform.X"));
    //Storyboard.SetTargetProperty(transAnimation, new PropertyPath("RenderTransform.Y"));
    Storyboard.SetTarget(transAnimation, FrameWithinGrid);
    storyboard.Completed += new EventHandler(storyboard_Completed);
    storyboard.Begin();
 }
 void storyboard_Completed(object sender, EventArgs e)
 {
    Storyboard storyboard = new Storyboard();
    DoubleAnimation growAnimation = new DoubleAnimation();
    growAnimation.Duration = TimeSpan.FromMilliseconds(500);
    growAnimation.From = -800;
    growAnimation.To = 0;
    storyboard.Children.Add(growAnimation);

    Storyboard.SetTargetProperty(growAnimation, new PropertyPath("RenderTransform.X"));
    //Storyboard.SetTargetProperty(growAnimation, new PropertyPath("RenderTransform.Y"));
    Storyboard.SetTarget(growAnimation, FrameWithinGrid);        
    storyboard.Begin();
 } 

Wednesday, July 4, 2012

WPF : Handling Event on View From ViewModel

1. First Create Event Handler in ViewModel 

LoginViewModel

   public event EventHandler LoginCompleted = delegate { };
   public void OnLoginCompleted()
   {
        LoginCompleted(this, new EventArgs());
   }

2. Handle ViewModel Event in View


LoginView.Xaml.cs

 
public LoginView()
{
   this.DataContext = new LoginViewModel(); 
   (this.DataContext as LoginViewModel).LoginCompleted += 
              new EventHandler(LoginViewModel_LoginCompleted); 
}
void LoginViewModel_LoginCompleted(object sender, EventArgs e)
{
   //your Coode goes here         
}