微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight 文件资源读取

使用程序代码加载图片



读取资源的方法:Application.GetResourceStream(Uri uri): StreamResourceInfo



Page.xaml文件


<UserControl

          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

          x:Class="SilverlightApplication7.Page"

          Width="640" Height="480">



          <Grid x:Name="LayoutRoot" Background="White">

          </Grid>

</UserControl>




Page.xaml.cs文件


using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Windows.Resources;

using System.Windows.Media.Imaging;



namespace SilverlightApplication7

{

          public partial class Page : UserControl

          {

                   public Page()

                   {

                            // 需要初始化变量

                            InitializeComponent();

            this.MouseLeftButtonDown += new MouseButtonEventHandler(Page_MouseLeftButtonDown);

        }



        void Page_MouseLeftButtonDown(object sender,MouseButtonEventArgs e)

        {

            Image img = LoadImage("/SilverlightApplication7;component/1150810574.jpg");

            LayoutRoot.Children.Add(img);   

        }

        Image LoadImage(string relativeUrlString)

        {

            Uri uri = new Uri(relativeUrlString,UriKind.Relative);

            StreamResourceInfo sri = Application.GetResourceStream(uri);

            BitmapImage bimg = new BitmapImage();

            bimg.SetSource(sri.Stream);

            Image img = new Image();

            img.source = bimg;

            return img;

        }

          }

}




使用代码加载文本

Uri uri = new Uri("/SilverlightApplication7;component/remark.txt",UriKind.Relative);            StreamResourceInfo sri = Application.GetResourceStream(uri); System.IO.StreamReader reader = new System.IO.StreamReader(sri.Stream); MessageBox.Show(reader.ReadToEnd());

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐