4
、设置应用程序控制逻辑
1
)MainPage.xaml.cs
文件代码
:


Code
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.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using SLMitsuControls;
namespace SilverlightClient
{
public partial class MainPage : UserControl, IDataProvider
{
//定义全局变量
private List<object> PageObjectList;//页面对象列表
public enum PageType { right, left };//页面类型
public string fileMedia = "";//文件媒体
public string headerPage = "";//首页
public int maxiPageNum = 0;//最大页数
public enum Location { local, web };//枚举应用程序所在
public Location modeLocation;
private int pageDownload = 0;//下载的页面数
private string uriResources = "";//Uri地址
//构造函数
public MainPage()
{
InitializeComponent();
PageObjectList = new List<object>();
}
//UserControl事件触发处理
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
if (modeLocation == Location.local)
{
this.canvChanging.Visibility = Visibility.Collapsed;
this.canvasBook.Visibility = Visibility.Visible;
//填充页面列表
FillPagesList();
}
if (modeLocation == Location.web)
{
this.canvChanging.Visibility = Visibility.Visible;
this.canvasBook.Visibility = Visibility.Collapsed;
//开始下载页面
DownloadPages();
}
}
//开始将页面填充至List中
private void FillPagesList()
{
//用页面填充列表
for (int xx = 1; xx <= maxiPageNum; xx++)
{
if (xx % 2 != 0)
{
//前一页即奇数页
AddPagetoList(PageType.right, fileMedia + "/" + headerPage +
xx.ToString("00") + ".jpg", xx.ToString(),
maxiPageNum.ToString(), true);
}
else
{
//后一页即偶数页
AddPagetoList(PageType.left, true);
}
}
//移除最后一页的按钮
TypePage.RightPage page = PageObjectList[maxiPageNum - 1] as TypePage.RightPage;
page.setterdisplayBtnNext(false);
//为翻页按钮指派事件触发处理
for (int xx = 1; xx < maxiPageNum; xx++)
{
if (xx % 2 != 0)
{
//前一页即奇数页
TypePage.RightPage pp = PageObjectList[xx - 1] as TypePage.RightPage;
Button btnNext = pp.getbtnNext();
btnNext.Click += new RoutedEventHandler(btnNext_Click);
}
else
{
//后一页即偶数页
TypePage.LeftPage pp = PageObjectList[xx - 1] as TypePage.LeftPage;
Button btnPrevIoUs = pp.getbtnPrevIoUs();
btnPrevIoUs.Click += new RoutedEventHandler(btnPrevIoUs_Click);
}
}
//为Book设置数据内容
book.SetData(this);
}
//向页面列表中添加具体页面
private void AddPagetoList(PageType pageType, string pathImage,
string numPage, string numMaxiPage,
bool showBtnYesNo)
{
switch (pageType)
{
case PageType.right:
TypePage.RightPage pcd = new SilverlightClient.TypePage.RightPage();
pcd.setterimgPhoto(pathImage);
pcd.setterPageNumber(numPage, numMaxiPage);
pcd.setterdisplayBtnNext(showBtnYesNo);
PageObjectList.Add(pcd);
break;
case PageType.left:
TypePage.LeftPage pcg = new SilverlightClient.TypePage.LeftPage();
pcg.setterimgPhoto(pathImage);
pcg.setterPageNumber(numPage, numMaxiPage);
pcg.setterdisplayBtnPrevIoUs(showBtnYesNo);
PageObjectList.Add(pcg);
break;
}
}
//“下一页”按钮事件触发处理
private void btnNext_Click(object sender, RoutedEventArgs e)
{
book.AnimatetoNextPage(500);
}
//“上一页”按钮事件触发处理
private void btnPrevIoUs_Click(object sender, RoutedEventArgs e)
{
book.AnimatetoPrevIoUsPage(500);
}
//从网络上下载页面
private void DownloadPages()
{
this.canvChanging.Visibility = Visibility.Visible;
this.uriResources = Application.Current.Host.source.AbsoluteUri;
int index = uriResources.IndexOf("SilverlightClient.xap");
uriResources = uriResources.Substring(0, index);
this.changingProgressBar.Minimum = 0;
this.changingProgressBar.Maximum = maxiPageNum - 1;
string theResources = uriResources + fileMedia + "/" + headerPage +
(pageDownload + 1).ToString("00") + ".jpg";
string theResourcesNum = headerPage + (pageDownload + 1).ToString("00") + ".jpg";
AsynchronouslyDownloadPage(theResources, theResourcesNum);
}
//异步下载页面
private void AsynchronouslyDownloadPage(string path, string num)
{
WebClient unWeb = new WebClient();
unWeb.DownloadStringCompleted +=
new DownloadStringCompletedEventHandler(unWeb_DownloadStringCompleted);
unWeb.DownloadStringAsync(new Uri(path));
this.changingText.Text = "正在下载 : " + num;
this.changingProgressBar.Value = this.pageDownload;
}
//异步下载页面完成事件触发处理
private void unWeb_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
this.pageDownload++;
if (this.pageDownload < this.maxiPageNum)//持续不断下载页面直到所有页面都下完
{
string theResources = uriResources + fileMedia + "/" + headerPage +
(pageDownload + 1).ToString("00") + ".jpg";
string theResourcesNum = headerPage + (pageDownload + 1).ToString("00") + ".jpg";
AsynchronouslyDownloadPage(theResources, theResourcesNum);
}
else
{
FillPagesList();
this.canvChanging.Visibility = Visibility.Collapsed;
this.canvasBook.Visibility = Visibility.Visible;
}
}
//强制声明接口
#region IDataProvider Members
public object GetItem(int index)
{
return PageObjectList[index];
}
public int GetCount()
{
return PageObjectList.Count;
}
#endregion
}
}


Code
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;
namespace SilverlightClient
{
public partial class App : Application
{
public string gFileMedia = "";
public string gHeaderPage = "";
public int gPageNumber = 0;
public string gModeLocation = "";
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
int paramOk = 0;
//从HTML中取出初始化数据
if (e.InitParams.ContainsKey("gFile"))
{
gFileMedia = e.InitParams["gFile"];
paramOk++;
}
if (e.InitParams.ContainsKey("gHeaderPage"))
{
gHeaderPage = e.InitParams["gHeaderPage"];
paramOk++;
}
if (e.InitParams.ContainsKey("gNum"))
{
string recup = e.InitParams["gNum"];
gPageNumber = int.Parse(recup);
paramOk++;
}
if (e.InitParams.ContainsKey("gLocation"))
{
gModeLocation = e.InitParams["gLocation"];
paramOk++;
}
if (paramOk == 4)
{
//初始化MainPage
MainPage maPage = new MainPage();
maPage.fileMedia = gFileMedia;
maPage.headerPage = gHeaderPage;
maPage.maxiPageNum = gPageNumber;
if (gModeLocation.Compareto("local") == 0)
{
maPage.modeLocation = MainPage.Location.local;
}
if (gModeLocation.Compareto("web") == 0)
{
maPage.modeLocation = MainPage.Location.web;
}
this.RootVisual = maPage;
}
}
private void Application_Exit(object sender, EventArgs e)
{
}
private void Application_UnhandledException(object sender,
ApplicationUnhandledExceptionEventArgs e)
{
if (!System.Diagnostics.Debugger.IsAttached)
{
e.Handled = true;
Deployment.Current.dispatcher.BeginInvoke(delegate { ReportErrorTodoM(e); });
}
}
private void ReportErrorTodoM(ApplicationUnhandledExceptionEventArgs e)
{
try
{
string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
System.Windows.browser.HtmlPage.Window.Eval(
"throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
}
catch (Exception)
{
}
}
}
}
3
)
在
SilverlightClientTestPage.aspx
文件(位于工程
FlipPage
文件夹下)中,添加用绿色粗体标明的代码:
<
body
>
<form id="form1" runat="server" style="height:100%">
<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
width="100%" height="100%">
<param name="source" value="ClientBin/SilverlightClient.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="3.0.40624.0" />
<param name="autoUpgrade" value="true" />
<!--
设置Book初始化参数-->
<param name="initParams" value="gFile=mediaPictures,gHeaderPage=albumPictures,gNum=9,gLocation=web" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0"
style="text-decoration:none">
@H_52_2502@ <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object><iframe id="_sl_historyFrame"
style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
</form>
</
body
>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。