Silverlight reads the contents of a file embedded in a XAP file

Source: Internet
Author: User
Tags relative xmlns silverlight

If a folder content is added to the SilverlightApplication6 project, a mxh.txt file and mxh.jpg are placed below, and the contents of the file are freely written. In the file properties of Solution Explorer, set "Build Action" to "Content"; The Copy to Output Directory property is set to does not copy.

In the XAML file, enter:

XAML Code

<UserControl x:Class="SilverlightApplication6.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="800" Height="600">
    <Grid x:Name="LayoutRoot" Background="White">
    <Canvas Width="800" Height="600">
      <TextBox x:Name="TextBoxName" Height="30" Canvas.Top="10"></TextBox>
      <Image x:Name="ImageNameIncude" Canvas.Top="60" Height="200"></Image>
      <Image x:Name="ImageNameEmbed" Canvas.Top="260" Height="100"></Image>
    </Canvas>
  </Grid>
</UserControl>

Xaml.cs Content Input:

C # code

Using System;
Using System.Windows;
Using System.Windows.Media.Imaging;
Using System.Windows.Controls;
Using System.IO;
Using System.Windows.Resources;

Namespace SilverlightApplication6
{
public partial class Mainpage:usercontrol
{
Public MainPage ()
{
InitializeComponent ();

Reading text
StreamResourceInfo r = Application.getresourcestream (new Uri ("Content/mxh.txt", urikind.relative));
StreamReader sr = new StreamReader (r.stream);
Textboxname.text = Sr. ReadToEnd ();
Sr. Dispose ();

Show build Action as Content picture
R = Application.getresourcestream (new Uri ("Content/mxh.jpg", urikind.relative));
BitmapImage bmp1 = new BitmapImage ();
Bmp1. SetSource (R.stream);
Imagenameincude.source = BMP1;

Show build Action for Resource picture
R = Application.getresourcestream (new Uri ("Silverlightapplication6;component/content/mxh2.jpg", UriKind.Relative)) ;
BitmapImage bmp2 = new BitmapImage ();
Bmp2. SetSource (R.stream);
Imagenameembed.source = BMP2;
}
}
}

Press F5 to compile the preview to see the contents of the Mxh.txt file in the TextBox and the photos showing the Mengxian.

Note: the delimiter "; component/" is required.

Also, note that the name of the assembly is used in the code SILVERLIGHTAPPLICATION6.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.