做了一个图片旋转的效果运行的时候没有浏览器什么都不显示,贴出代码请各位帮忙看下~
Code==================================ShopShow.xaml============================================================
<UserControl x:Class="Sample.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"
d:DesignHeight="300" d:DesignWidth="400">
<Grid>
<Grid.Background>
<ImageBrush ImageSource="/Silverlig;component/Images/win7-9.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"/>
</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="btnStart" Click="btnStart_Click"></Button>
<Button Width="50" Height="30" Content="Stop" Margin="10" x:Name="btnStop" Click="btnStop_Click"></Button>
</StackPanel>
</Grid>
</UserControl>
==============================ShopShow.xaml.cs========================================
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.Threading;
using System.Windows.Media.Imaging;
namespace Sample
{
public partial class ShopShow : UserControl
{
private double centerX = 400;
private double centerY = 300;
private double Widthh = 300;
private double Heightt = 60;
private double degree = 0;
//项集合类
List<ShopItem> objList = new List<ShopItem>();
private double itemWidth = 160;
private double itemHeight = 80;
private double count = 14;
private double currentOpacity = 0;
private DispatcherTimer timer;
public ShopShow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(ShopShow_Loaded);
}
private void ShopShow_Loaded(object sender, RoutedEventArgs e)
{
timer = new DispatcherTimer();
for (var i = 1; i < count; i++)
{
//实例化用户控件
ShopItem item = new ShopItem();
Image images = item.obj;
//加载图片
Uri uri = new Uri(string.Format("/Silverlig;component/Images/album{0}.png", i), UriKind.RelativeOrAbsolute);
BitmapImage bitmap = new BitmapImage(uri);
images.Source = bitmap;
//绑定控件事件
images.MouseEnter += new MouseEventHandler(images_MouseEnter);
images.MouseLeave += new MouseEventHandler(images_MouseLeave);
images.MouseLeftButtonDown += new MouseButtonEventHandler(images_MouseLeftButtonDown);
//添加用户控件到集合里
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();
}
void images_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Image img = sender as Image;
shower.Visibility = Visibility.Visible;
shower.Source = img.Source;
}
void images_MouseLeave(object sender, MouseEventArgs e)
{
timer.Start();
Image img = (Image)sender;
img.Opacity = currentOpacity;
}
void images_MouseEnter(object sender, MouseEventArgs e)
{
timer.Stop();
Image img = (Image)sender;
currentOpacity = img.Opacity;
img.Opacity = 1;
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
timer.Start();
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
timer.Stop();
}
private void timer_Tick(object sender, EventArgs e)
{
StartMove();
}
private void StartMove()
{
for (var i = 0; i < objList.Count; i++)
{
//根据控件总数和圆周计算一个平均值
var tmp = (degree + (360 / count) * i) % 360;
tmp = tmp * Math.PI / 180;
var posx = (Widthh) * Math.Sin(tmp);//更新X
var posy = (Heightt) * Math.Cos(tmp);//更新Y
ShopItem obj = objList[i];
//根据宽搞计算缩放比例
double scale = (2 * Heightt - posx) / (3 * Heightt + 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();
st.ScaleX = scale;
st.ScaleY = scale;
obj.RenderTransform = st;
obj.Opacity = scale;
}
degree = degree - 0.3;
}
}
}
=================================
上面的代码,是我自己看了我买的书上面的一个列子做的,但是不知道为什么不显示效果,恳请给为帮忙解决