天天看點

CaseStudy(showcase)資料篇-Loading的制作

做silvelight也有一段時間了,相冊、遊戲,剛剛完成的showcase這個小程式算是一個階段了。這裡就以showcase這個項目來做一下CaseStudy。

silverlight自帶了一個loading。但是由于界面的需求可能需要定制化一下。這裡我的思路是做兩個sl檔案用其中一個去加載另外一個。也就是說有兩個xap檔案一個是主要的也就是你做的silverlight程式檔案,另一個小的是用來做loading的。

public partial class Page : UserControl

    {

        public Page()

        {

            InitializeComponent();

            App.Current.Host.Content.Resized += new EventHandler(Content_Resized);

            App.Current.Host.Content.FullScreenChanged += new EventHandler(Content_FullScreenChanged);

            this.Init();

        }

        private void Init()

            Uri address = new Uri(HtmlPage.Document.DocumentUri, "ClientBin/showcase.xap");

            WebClient webClient = new WebClient();

            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);

            webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);

            webClient.OpenReadAsync(address);

        void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)

            //在這裡實作你的loading動畫

            double per = Convert.ToDouble(e.ProgressPercentage) / Convert.ToDouble(100);

            loadBar.Height = (App.Current.Host.Content.ActualHeight - 16) * per;

        void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)

            Assembly assembly = LoadAssemblyFromXap(e.Result, "showcase.dll");

            UIElement element = assembly.CreateInstance("showcase.Page") as UIElement;

            this.LayoutRoot.Children.Clear();

            this.LayoutRoot.Children.Add(element);

        Assembly LoadAssemblyFromXap(Stream packageStream, String assemblyName)

            Stream stream = Application.GetResourceStream(

                    new StreamResourceInfo(packageStream, null),

                    new Uri("AppManifest.xaml", UriKind.Relative)).Stream;

            String appManifestString = new StreamReader(stream).ReadToEnd();

            Deployment deployment = (Deployment)XamlReader.Load(appManifestString);

            Assembly assembly = null;

            foreach (AssemblyPart assemblyPart in deployment.Parts)

            {

                if (assemblyPart.Source == assemblyName)

                {

                    String source = assemblyPart.Source;

                    StreamResourceInfo streamInfo = Application.GetResourceStream(

                        new StreamResourceInfo(packageStream,

                        "application/binary"),

                        new Uri(source, UriKind.Relative));

                    assembly = assemblyPart.Load(streamInfo.Stream);

                    break;

                }

            }

            return assembly;

        void Content_FullScreenChanged(object sender, EventArgs e)

            this.ResizeFrame();

        void Content_Resized(object sender, EventArgs e)

        void ResizeFrame()

            (this as UserControl).Width = App.Current.Host.Content.ActualWidth;

            (this as UserControl).Height = App.Current.Host.Content.ActualHeight;

        void FullScreenMode()

            App.Current.Host.Content.IsFullScreen = !App.Current.Host.Content.IsFullScreen;

    }

作者:nasa

聯系:[email protected]

QQ:12446006