The given System.Uri cannot be converted into a Windows.Foundation.Uri

后端 未结 7 2029
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 10:47

I\'m trying to programmatically load a BitmapImage in a XAML Metro app. Here\'s my code:

var uri = new Uri(\"/Images/800x600/BackgroundTile.bmp\", UriKind.Re         


        
7条回答
  •  独厮守ぢ
    2020-12-01 11:19

    MVVM ;)

        public static ImageSource SetImageSource(string uriPath)//.com/image or some path
        {
            // this method very good for mvvm ;)
            try
            {   
                //In Model - public ImageSource AccountPhoto { get; set; } 
                //AccountPhoto = SetImageSource("some path");
                return return new BitmapImage()
                {
                    CreateOptions = BitmapCreateOptions.IgnoreImageCache,
                    UriSource = new Uri(uriPath)
                };
            }
            catch
            {
                Debug.WriteLine("Bad uri, set default image.");
                return return new BitmapImage()
                {
                    CreateOptions = BitmapCreateOptions.IgnoreImageCache,
                    UriSource = new Uri("ms-appx:///Avatar/Account Icon.png")
                };
            }
        }
    

提交回复
热议问题