wpf pack url (转帖)

12 篇文章 0 订阅
WPF URI  
转自  http://zhxh1012.blog.163.com/blog/static/140866457201191752652161/


绝对 pack WPF URI


1.资源文件 — 本地程序集

Uri uri = new Uri("pack://application:,,,/ResourceFile.xaml", UriKind.Absolute);



子文件夹中的资源文件 — 本地程序集

Uri uri = new Uri("pack://application:,,,/Subfolder/ResourceFile.xaml", UriKind.Absolute);



 2.资源文件 — 所引用的程序集


Uri uri = new Uri("pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml", UriKind.Absolute);


3.所引用的程序集的子文件夹中的资源文件


Uri uri = new Uri("pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml", UriKind.Absolute);



4.所引用的版本化程序集中的资源文件


Uri uri = new Uri("pack://application:,,,/ReferencedAssembly;v1.0.0.0;component/ResourceFile.xaml", UriKind.Absolute);




WPF URI内容文件

1.Uri uri = new Uri("pack://application:,,,/ContentFile.xaml", UriKind.Absolute);


2.子文件夹中的内容文件Uri uri = new Uri("pack://application:,,,/Subfolder/ContentFile.xaml", UriKind.Absolute);


3.源站点文件Uri uri = new Uri("pack://siteoforigin:,,,/SOOFile.xaml", UriKind.Absolute);


4.子文件夹中的源站点文件Uri uri = new Uri("pack://siteoforigin:,,,/Subfolder/SOOFile.xaml", UriKind.Absolute);


相对 pack URI


      1.WPF URI资源文件 — 本地程序集

Uri uri = new Uri("/ResourceFile.xaml", UriKind.Relative);


      2.子文件夹中的资源文件 — 本地程序集


Uri uri = new Uri("/Subfolder/ResourceFile.xaml", UriKind.Relative);


      3.资源文件 — 所引用的程序集

Uri uri = new Uri("/ReferencedAssembly;component/ResourceFile.xaml", UriKind.Relative);



     4.子文件夹中的资源文件 — 所引用的程序集

Uri uri = new Uri("/ReferencedAssembly;component/Subfolder/ResourceFile.xaml", UriKind.Relative);



    内容文件

Uri uri = new Uri("/ContentFile.xaml", UriKind.Relative);

WPF URI子文件夹中的内容文件Uri uri = new Uri("/Subfolder/ContentFile.xaml", UriKind.Relative);



WPF中的Image控件可以通过设置Source属性来显示图片,而IImageSource接口可以表示任何能够提供图像数据的对象。因此,我们可以将一个URL绑定到Image控件的Source属性上,从而动态地显示图片。 以下是一个简单的示例: 首先,我们需要定义一个实现IImageSource接口的类,用于获取指定URL的图片数据: ```csharp public class UrlImageSource : IImageSource { private readonly string _url; public UrlImageSource(string url) { _url = url; } public event EventHandler<ExceptionEventArgs> DownloadFailed; public event EventHandler DownloadCompleted; public void SetSource(Stream stream) { var bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.StreamSource = stream; bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.EndInit(); DownloadCompleted?.Invoke(this, EventArgs.Empty); OnDownloadCompleted(bitmap); } protected virtual void OnDownloadCompleted(BitmapImage bitmap) { DownloadCompleted?.Invoke(this, EventArgs.Empty); } } ``` 该类接受一个URL,并在SetSource方法中将URL中的图片数据加载到一个BitmapImage对象中。如果加载失败,则触发DownloadFailed事件。 接下来,我们可以在XAML中使用DataTemplate来绑定Image控件的Source属性: ```xml <DataTemplate DataType="{x:Type local:MyViewModel}"> <StackPanel> <TextBlock Text="{Binding Name}" /> <Image> <Image.Source> <Binding Path="ImageSource" /> </Image.Source> </Image> </StackPanel> </DataTemplate> ``` 在ViewModel中,我们可以将一个UrlImageSource对象赋值给ImageSource属性,这样就可以在UI中显示图片了: ```csharp public class MyViewModel { public string Name { get; set; } public IImageSource ImageSource { get; set; } public MyViewModel() { ImageSource = new UrlImageSource("https://example.com/image.jpg"); } } ``` 当然,你也可以通过绑定数据源来动态地设置URL
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值