First Look at the demo:
I'll step through the process of implementing this demo, which is to read the pictures and videos in the zip file.
Demo Overall Architecture:
First we prepare a few pictures and videos, then compress them into the Resource.zip file, after we have done, we set up a resource.xml file to record the resources within the compressed package
<files>
<type=name="a.wmv"/>
<type=name="1.jpg"/>
<type=name="2.jpg"/>
<type=name="3.jpg"/>
<type=name="4.jpg"/>
</files>
This XML file records the type and name of the file and we compress it into resource.zip after completion ( Note : This step will affect the read stream later)
Now we're designing the UI.
<x:name=/>
<x:name=/>
<stackpanel Horizontalalignment= "left" verticalalignment= "Bottom" opacity= "0.5" orientation= "horizontal" margin=name =>
<button cont Ent= "Prev" x:name=< Span style= "color: #0000ff;" > "Prevbutton" height= "30" margin= Width=opacity=" 1 "cursor=" Hand " Isenabled=/>
<button x:< Span style= "color: #ff0000;" >name= "Nextbutton" height= margin= "0,0,5,0" width= "opacity=< Span style= "color: #0000ff;" > "1" content= "Hand" isenabled= "False" />
</StackPanel>
An image and MediaElement control is placed on the UI to display the read resource
Now let's start building the ResourceInfo.cs file.
Enum ResourceType
{
Video,
Image
}
Class Resourceinfo
{
Public ResourceType Type
{
get; set;
}
String Name
{
Set
}
}
The type of the file is represented as an enumeration, and now we are doing the reading of the data in MainPage.xaml.cs with WebClient
Declare 3 class member variables first:
Private StreamResourceInfo zip;
Private list<resourceinfo> Resourceinfo;
int index = 0;
Now we're going to get the stream, its full code:
void Load (object Sender,routedeventargs e)
{
New Uri (HtmlPage.Document.DocumentUri, "resource.zip");
New WebClient ();
{
Null
{
Return
}
These steps encapsulate the read-out flow information in reader, which makes it easy to use the LINQ to XML operations later
NULL);
New Uri ("resource.xml", urikind.relative));
New StreamReader (MainInfo. Stream);
XDocument doc = xdocument.load (reader);
In Doc. Descendants ("file")
New Resourceinfo
{
Type = (resourcetype) enum.parse (typeof (ResourceType), C.attribute ("true),
Name = C.attribute ("name"). Value
};
New List<resourceinfo> ();
Resourceinfo.addrange (file);
True
True
Display (Resourceinfo[0]);
};
Webclient.openreadasync (URI);
}
void Display (Resourceinfo Resource)
{
Get the corresponding stream data
StreamResourceInfo media = application.getresourcestream (Zip,new Uri (Resource). name,urikind.relative));
Switch (resource. Type)
{
Case Resourcetype.image:
image.visibility = visibility.visible;
video.visibility = visibility.collapsed;
New BitmapImage ();
Image. SetSource (media. Stream);
Image.source = image;
Break
Case Resourcetype.video:
image.visibility = visibility.collapsed;
video.visibility = visibility.visible;
Video.setsource (media. Stream);
Video.play ();
Break
}
}
As a matter of fact, after loading this code, we can already play the first resource a.wmv in the XML file successfully on the page.
We continue the loop on the button implementation of the interface to display the previous one, the next resource function
void Stopvideo ()
{
if (Resourceinfo[index]. Type = = Resourcetype.video)
{
Video.stop ();
}
}
void Prevbutton_click (object sender, RoutedEventArgs e)
{
Stopvideo ();
if (--index < 0)
{
index = resourceinfo.count-1;
}
Display (Resourceinfo[index]);
}
void nextButton_Click (object sender, RoutedEventArgs e)
{
Stopvideo ();
if (++index >=resourceinfo.count)
{
index = 0;
}
Display (Resourceinfo[index]);
}
So we're done with this demo.