silverlight 樂動魔方 實戰九

 回到Game 頁面~?

 可以開始遊戲了?···興奮,。

 

額, 恐怕讓你們失望了,···還不可以,··因爲歌曲要下載, so··要先寫一個進度頁面···顯示歌曲加載的進度。

 

好吧··

1、在文件夾UserControls裏面,新建一個進度頁面···ChildMusicLoading

 2、編輯他的前臺UI

<controls:ChildWindow x:Class="SilverlightMusicHit.ChildMusicLoading"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
           Width="400" Height="300" HasCloseButton="False">
    <Grid x:Name="LayoutRoot" Margin="2">
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="100" />
            <RowDefinition Height="100" />
        </Grid.RowDefinitions>
        <ProgressBar Grid.Row="1" Margin="0,30,0,30" Name="proMusicLoading" Value="0"/>
        <TextBlock Name="txtProcess" Grid.Row="0" Text="歌曲加載進度0%" FontSize="16" Margin="0,50,0,0"></TextBlock>
    </Grid>
</controls:ChildWindow>


3、進入他的後臺cs

    public partial class ChildMusicLoading : ChildWindow
    {
        /// <summary>
        /// 播放器
        /// </summary>
        private MediaElement _mediaElement;

        public ChildMusicLoading(int musicIndex)
        {
            InitializeComponent();

            //加載音樂
            _mediaElement = new MediaElement();
            this.LayoutRoot.Children.Add(_mediaElement);
            Common.SoundUrl(ref _mediaElement, "../" + Common._MusicValue[musicIndex].MusicPath);
            _mediaElement.DownloadProgressChanged += new RoutedEventHandler(_mediaElement_DownloadProgressChanged);
        }

        void _mediaElement_DownloadProgressChanged(object sender, RoutedEventArgs e)
        {
            _mediaElement.Pause();
            txtProcess.Text = string.Format("加載進度{0}%", Convert.ToInt32((_mediaElement.DownloadProgress * 100)).ToString());
            proMusicLoading.Value = _mediaElement.DownloadProgress * 100;
            if (_mediaElement.DownloadProgress == 1)
                this.DialogResult = true;
        }
    }


這章沒啥好說的,··哈···主要的是··musicIndex的傳遞,要知道歌曲的索引, 接着就啓動 mediaElement的加載事件, 進度條同步顯示加載進度, 如果加載完成,這個窗體就會返回值true。這時,窗體就會關閉,o(∩_∩)o 哈哈,開始遊戲了。

 

心急的按F5看看, - =沒效果的~?· 哈·,還是犯了老毛病, 還沒有引用他呢。

 

4、進入Game.cs

        public Game(int _musicIndex)
        {
            InitializeComponent();

            //讀取音樂索引
            musicIndex = _musicIndex;


            //加載音樂
            _MusicLoading = new ChildMusicLoading(musicIndex);
            _MusicLoading.Show();
        }


這次應該看到了吧,··可能本地加載太快了···會一閃而過,,試試放上服務器··讓別人瀏覽吧,。··哈··

 

name:5+x

 

參考文章與書籍;

WPF葵花寶典

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章