我想創建一個動畫來模擬一個卡通運動員運行。我有兩種不同的人物角色運行狀態圖像,我希望能夠在兩幅圖像之間循環,圖像變化之間的延遲時間大約爲1秒。在兩個圖像之間旋轉模擬動畫在C#WPF
我試圖通過一個改變畫布背景的故事板來做到這一點,也試圖把兩個圖像放在一起,並在短時間內改變0% - > 100%的不透明度,但是要麼我從我的代碼中錯過某些東西來使其運行起來,要麼我只是在錯誤的方向前進解決方案。
任何幫助或鏈接到可能的解決方案將不勝感激,因爲我似乎無法找到任何幫助我。
希望這解釋了我在找什麼,但如果需要,我可以提供進一步的細節。
編輯:好的,所以我採用了Noobacode提供的示例,並且已將其更改爲我認爲應該執行的操作。我有以下幾點:
用戶控件的代碼背後
public partial class UserControl1 : UserControl
{
private int currentIndex = 1;
private DispatcherTimer updateImageTimer;
public UserControl1()
{
InitializeComponent();
this.updateImageTimer = new DispatcherTimer(DispatcherPriority.Render);
this.updateImageTimer.Interval = TimeSpan.FromMilliseconds(30.0);
this.updateImageTimer.Tick += new EventHandler(this.updateImageTimer_Tick);
}
private void updateImageTimer_Tick(object sender, EventArgs e)
{
if(currentIndex == 1)
{
backgroundImage.Source = new BitmapImage(new Uri("images/blurredBackground1.png", UriKind.Relative));
currentIndex++;
}
else if(currentIndex == 2)
{
backgroundImage.Source = new BitmapImage(new Uri("images/blurredBackground2.png", UriKind.Relative));
currentIndex--;
}
}
public void Play()
{
updateImageTimer.Start();
}
public void Stop()
{
updateImageTimer.Stop();
}
}
用戶控制XAML
<UserControl x:Class="Going_For_Gold.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="492" d:DesignWidth="800">
<Grid>
<Image x:Name="backgroundImage" Stretch="Fill"/>
</Grid>
現在我已經添加了用戶控制我的主要窗口,但圖像控制我已經放入主窗口(我知道這是正確的工作如果我把一個初始圖像源放到UserControl中,我可以通過主窗口上的控件看到圖像),但是這個代碼似乎沒有在tick上更新圖像源。
你可以給我們代碼到目前爲止? –
我假設你開始了tick事件? 'this.updateImageTimer.Start();'我看到你有Play()方法,但你曾經使用過它嗎?添加一些調試代碼來查看tick事件是否觸發。 –
是的,我在用戶按下按鈕時從主窗口調用.Start()方法。 tick事件正在運行。 – user3280835