惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cloudbric
Cloudbric
Schneier on Security
Schneier on Security
V2EX - 技术
V2EX - 技术
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
O
OpenAI News
S
Security @ Cisco Blogs
Scott Helme
Scott Helme
Security Archives - TechRepublic
Security Archives - TechRepublic
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
WordPress大学
WordPress大学
云风的 BLOG
云风的 BLOG
T
Threatpost
Hacker News: Ask HN
Hacker News: Ask HN
Microsoft Azure Blog
Microsoft Azure Blog
Know Your Adversary
Know Your Adversary
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
Forbes - Security
Forbes - Security
NISL@THU
NISL@THU
Security Latest
Security Latest
G
Google Developers Blog
D
Docker
T
Threat Research - Cisco Blogs
N
Netflix TechBlog - Medium
C
CERT Recently Published Vulnerability Notes
H
Help Net Security
B
Blog
Martin Fowler
Martin Fowler
N
News and Events Feed by Topic
Simon Willison's Weblog
Simon Willison's Weblog
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
Lohrmann on Cybersecurity
Y
Y Combinator Blog
PCI Perspectives
PCI Perspectives
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
Project Zero
Project Zero
爱范儿
爱范儿
Cisco Talos Blog
Cisco Talos Blog
博客园 - 聂微东
Hugging Face - Blog
Hugging Face - Blog
人人都是产品经理
人人都是产品经理
V
Vulnerabilities – Threatpost
P
Proofpoint News Feed
Cyberwarzone
Cyberwarzone
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
N
News | PayPal Newsroom
Recorded Future
Recorded Future

博客园 - MainTao

WebStorm 自定义字体+颜色+语法高亮+导入导出用户设置 WebStorm安装Vim以及快捷键设置 scroll clock–jQuery widget implement 基于iScroll 的开发(不断补充) C#操作Win7/Win8的库、收藏夹 P/Invoke Interop 实例 编写jQueryUI插件(widget) 调试Javascript Windows Phone 7监测网络环境变化 Blend制作动画 用OpacityMask快速制作theme friendly UI 用gradient brush和OpacityMask实现fade edge效果 blend 画图 ControlTemplate & DataTemplate PhoneApplicationFrame以及设置Obscured/Unobscured的event handler Windows Phone 7 短信表情 WP7应用程序生命周期,以及各个event handler分别该做什么 第一个WP7程序 开发总结 XAML中的空格、换行、Tab
WP7图片从独立存储绑定
MainTao · 2012-05-23 · via 博客园 - MainTao

需求:一个ListBox上需要绑定图片,根据情况设置source,可能是isolatedStorage中的文件,也可能是xap中的文件。
代码如下:
首先是一个Converter的代码,里面有如何从isolatedStorage和xap中读取文件流。
 
public class StringToImageSource : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            BitmapImage ret = new BitmapImage();
            string filePath = (string)value;

            using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (iso.FileExists(filePath))
                { // 使用隔离存储中的文件
                    using (var source = iso.OpenFile(filePath, FileMode.Open, FileAccess.Read))
                    {
                        ret.SetSource(source);
                    }
                }
                else
                { // 使用资源文件(注意将BuildAction设置为Content)
                    Uri uri = new Uri(filePath, UriKind.RelativeOrAbsolute);
                    StreamResourceInfo sri = Application.GetResourceStream(uri);
                    ret.SetSource(sri.Stream);
                }

                return ret;
            }
        }
        catch (Exception)
        {
            return null; // 如果发生异常,让图片位置为空
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return DependencyProperty.UnsetValue;
    }

    #endregion
}

xaml绑定这样写:

Source="{Binding Img, Converter={StaticResource StringToImageSource}”