WPF에서 상대 파일 경로를 BitmapImage
으로 변환하는 IValueConverter
이 있습니다.링크로 프로젝트에 추가 된 이미지 파일을 소비하려고 할 때 IOException이 발생하는 이유는 무엇입니까?
강령 :
public class RelativeImagePathToImage : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var relativePath = (string)value;
if (string.IsNullOrEmpty(relativePath)) return Binding.DoNothing;
var path = "pack://application:,,,/" + value;
var uri = new Uri(path);
return new BitmapImage(uri);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Binding.DoNothing;
}
}
문제점 : 나는 (링크로 프로젝트에 추가 된 파일을 사용하려고 할 때까지이 컨버터는 잘 작동했다
솔루션 탐색기 -> 기존 항목 추가 -> 링크로 추가). 이미지 파일의 BuildAction
은 Content
으로 설정되고 파일은 Copy Always
으로 표시됩니다. 파일이 확실히 "bin"폴더에 제대로 복사되고 있지만 어떤 이유로 인해 변환기가 return new BitmapImage(uri)
이되면 찌를 수 있습니다.
예외 :
System.IO.IOException was unhandled
Message="Cannot locate resource 'images/splash.png'."
Source="PresentationFramework"
질문 :이
누군가가 이것을 설명 할 수 있습니까? 이것은 .NET Framework의 버그입니까? 예상되는 동작입니까? 해결 방법이 있습니까, 아니면 이미지 콘텐츠 파일 옵션이 아닌 "링크로 추가"입니까?
편집 :
좋아, 해결 방법을 발견했다. 여기 내 수정 된 컨버터 클래스는 다음과 같습니다
public class RelativeImagePathToImage : IValueConverter
{
private static string _rootPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var relativePath = (string)value;
if (string.IsNullOrEmpty(relativePath)) return Binding.DoNothing;
var path = _rootPath + "/" + relativePath;
var uri = new Uri(path);
return new BitmapImage(uri);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Binding.DoNothing;
}
}
은 분명히, 링크 된 파일이있는 packuri
를 사용에 문제의 어떤 종류가있다. 하지만 왜?
그러나 코드는 폴더에있는 이미지 파일에 대해 잘 작동하고'Content' -'Copy Always'로 설정됩니다. 폴더에 파일을 저장하는 대신 링크를 사용했을 때만 작동이 멈췄습니다. – devuxer
+1, 내 사건을 해결하기위한 힌트로 충분했다. 한 프로젝트 폴더의 깊은 파일 = 잘 작동했다. 2 개의 프로젝트 폴더 deep = 예외. 어리석은. –