2016-11-03 1 views
0

바인딩 BitmapImage 데이터는 내가 BitmapImage 나는 내 XAML 뷰에 바인딩하기 위해 노력하고있어 싱글을 가지고 내가 가지고 :WPF - 싱글

여기
The calling thread cannot access this object because it is owned by another thread 

내 코드 :

BitmapImage bitmap = new BitmapImage(); 
bitmap.BeginInit(); 
bitmap.UriSource = new Uri(fullFilePath, UriKind.Absolute); 
bitmap.EndInit(); 
MySingleton.Instance.image = bitmap; 

내 싱글 :

private BitmapImage _image; 
    public BitmapImage image 
    { 
    get { return _image; } 
    set 
    { 
     _image = value; 
     PropertyChanged(this, new PropertyChangedEventArgs(nameof(image))); 
    } 
    } 

그리고 내 XAML :

0 나는 bitmap.Freeze();을 시도했지만 오류 가지고 :

Freezable cannot be frozen

가 meanless의 여부를하지만 난 웹 소켓의 onMessage 이벤트에 비트 맵을 실체화하는 경우 나도 몰라합니다.

+0

인가'로컬 파일 경로를 fullFilePath'? – Clemens

+0

@Clemens 아니, URL을 바인딩, 및 "Application.Current.Dispatcher.Invoke (()"Lupu Silviu 주어진 코드를 테스트하고 작동하지만 바인딩이 작동하지 않는 이유는 아직 찾지 못했습니다. – Safe

+0

캐시 옵션이 'Default'이기 때문에 이미지를 정지시킬 수 없습니다 .BitmapCacheOption.OnLoad'로 비트 맵의 ​​CacheOption을'EndInit() '호출 전에 설정하십시오. – dymanoid

답변

1

코드는 Dispatcher 안에 배치해야합니다.

Application.Current.Dispatcher.Invoke(() => 
{ 
    //(your code here) 
}); 

또는

Application.Current.Dispatcher.BeginInvoke((Action)(() => 
{ 
    //(your code here) 
})); 
+0

감사합니다! 이미 본 것이지만 잘못된 가져 오기를 선택했습니다 : S 코드는 다음과 같습니다. 지금은 작동하지만 이미지는 여전히 표시되지 않습니다. 이미지가 데이터 바인딩없이 원시 코드로 표시됩니다. – Safe

+0

제 바인딩이 제대로 작동하지 않았으므로 xD 테스트에 실패했습니다. – Safe