一個唱片商店的小例子 可以自動旋轉唱片 點擊彈出唱片

來源:互聯網
上載者:User

效果圖

 

XAML

 1.ShopShow_Page

 

  <navigation:Page x:Class="SilverlightApplication6.ShopShow"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable="d"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           d:DesignWidth="800" d:DesignHeight="600"
           Title="ShopShow Page">
    <Grid x:Name="LayoutRoot">
        <Grid.Background>
            <ImageBrush ImageSource="../Images/win7-6.jpg" Stretch="Fill" >     
            </ImageBrush>
        </Grid.Background>
        <Image x:Name="shower" Width="180" Height="180" Stretch="Fill" Visibility="Collapsed">
            <Image.Effect>
                <DropShadowEffect Color="white" BlurRadius="10" Opacity="0.8" ShadowDepth="0">    
                </DropShadowEffect>
            </Image.Effect>
        </Image>
        <Canvas x:Name="moveCanvas"></Canvas>
        <StackPanel Orientation="Horizontal" Margin="300,500,0,0">
            <Button Width="50" Height="30" Content="Play" Margin="10" x:Name="btn_Start" Click="btn_Start_Click" ></Button>
            <Button Width="50" Height="30" Content="Stop" Margin="10" x:Name="btn_Stop" Click="btn_Stop_Click"></Button>
        </StackPanel>
    </Grid>
</navigation:Page>

 


2 ShopItem

 

<UserControl x:Class="SilverlightApplication6.ShopItem"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid x:Name="LayoutRoot">
        <Image x:Name="obj" Width="135" Height="135" Stretch="None">
        </Image>
    </Grid>
</UserControl>

 

C# 代碼

 

class ShopShow

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Windows.Threading;
using System.Windows.Media.Imaging;

namespace SilverlightApplication6
{
    public partial class ShopShow :Page
    {
       private DispatcherTimer timer;
       private List<ShopItem> objList = new List<ShopItem>();
       private double count = 14;//唱片數目
       private double degree = 0;//便宜變數
       private double width = 300; // 橢圓長半徑
       private double height = 60; //橢圓短半徑
       private double centerX = 400;//中心點 X座標
       private double centerY = 300;//中心點 Y座標
       private double itemWidth = 160; //物件寬度
       private double itemHeight = 80; //物件高度
       private double currentOpacity = 0;//透明度

       public ShopShow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ShopShow_Loaded);
        }

       void ShopShow_Loaded(object sender, RoutedEventArgs e)
       {
           timer = new DispatcherTimer();
           for (var i = 1; i <count; i++)
           {
               //執行個體化使用者控制項
               ShopItem item = new ShopItem();
               Image image = item.obj;
               //載入唱片圖片
               Uri uri = new Uri(string.Format("../Images/album{0}.png",i),UriKind.Relative);
               BitmapImage bitmap = new BitmapImage(uri);
               image.Source = bitmap;
               //繫結控制項事件
               image.MouseLeftButtonDown += new MouseButtonEventHandler(image_MouseLeftButtonDown);
               image.MouseEnter += new MouseEventHandler(image_MouseEnter);
               image.MouseLeave += new MouseEventHandler(image_MouseLeave);
               objList.Add(item);
               moveCanvas.Children.Add(item);
           }
           timer.Tick += new EventHandler(timer_Tick);
           TimeSpan sp = new TimeSpan(0,0,0,0,20);
           timer.Interval = sp;
           timer.Start();
       }
        //產生橢圓運動效果的方法
       public void StartMove()
       {
           for (var i = 0; i < objList.Count; i++)
           {
               //根據控制項總數和圓周計算一個平均值
               var tmp=(degree+(360/count)*i)%360;
               tmp = tmp * Math.PI /180;//更新x
               var posX = width * Math.Sin(tmp);//更新X
               var posY = height * Math.Cos(tmp);//更新Y
               ShopItem obj = objList[i];
               //根據寬高計算縮放比例
               double scale =(2*height - posY)/(3*height+itemHeight/2);
               Canvas.SetLeft(obj,centerX + posX - (itemWidth / 2) * scale);
               Canvas.SetTop(obj, centerY - posY - (itemHeight / 2) * scale);
               Canvas.SetZIndex(obj,int.Parse(Math.Ceiling(count*scale).ToString()));
               ScaleTransform st = new ScaleTransform(){ CenterX = scale, ScaleY = scale };
               obj.RenderTransform = st;
               obj.Opacity = scale;
           }
           degree -= 0.3;
       }
      
       void timer_Tick(object sender, EventArgs e)
       {
           StartMove();
       }

       void image_MouseLeave(object sender, MouseEventArgs e)
       {
           Image img = sender as Image;
           img.Opacity = currentOpacity;
           timer.Start();
       }

       void image_MouseEnter(object sender, MouseEventArgs e)
       {
           timer.Stop();
           Image img = sender as Image;
           currentOpacity = img.Opacity;
           img.Opacity = 1;

       }

       void image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
       {
           Image img = sender as Image;
           shower.Visibility = Visibility.Visible;
           shower.Source = img.Source;
       }

       private void btn_Start_Click(object sender, RoutedEventArgs e)
       {
           timer.Start();
       }

       private void btn_Stop_Click(object sender, RoutedEventArgs e)
       {
           timer.Stop();
       }   
    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.