开发者

How checked file exist in Silverlight

开发者 https://www.devze.com 2023-03-11 12:50 出处:网络
I need converter if image not exist or null return new BitmapImage(new Uri(\"http://upload.wikimedia.org/wikipedia/commons/1/1c/No-Symbol.png\"))

I need converter if image not exist or null return new BitmapImage(new Uri("http://upload.wikimedia.org/wikipedia/commons/1/1c/No-Symbol.png")) else if file exist return file.

pathImage - example http://localhost:65051/ClientBin/images/sm_butter_cake.jpg

File.Exists - always returns false. Why?

public class ConvertNullImage : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                string pathImage = Application.Current.Host.Source.AbsoluteUri.Substring(0, 
                    Application.Current.Host.Source.AbsoluteUri.LastIndexOf("/"));
                if (File.Exists(pathImage + value.ToString()))
                {
                    var image = new BitmapImage(new Uri(value.ToString(), UriKind.Relative));
                    return image;
                }
                else
                {
                    throw new Exception("Not file");
                }

            }
            catch { return new BitmapImage(new Uri("http://upload.wikimedia.org/wikipedia/commons/1/1c/No-Symbol.png")); }
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  CultureInfo culture)
    开发者_运维技巧    {
            throw new NotImplementedException();
        }
    }


File.Exists won't work over HTTP, try using the solution here

http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/95851d08-452b-4f32-b03a-206dec5c8095/


Need use event ImageFailed.

Example:

<Image MaxHeight="50" x:Name="image" Source="{Binding photo}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="ImageFailed">
        <ei:ChangePropertyAction PropertyName="Source">
            <ei:ChangePropertyAction.Value>
            <ImageSource>
                /images/noimage.png
            </ImageSource>
            </ei:ChangePropertyAction.Value>
        </ei:ChangePropertyAction>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Image>

source, but this russian blog

0

精彩评论

暂无评论...
验证码 换一张
取 消