WP7에서 크로스 스레드 작업에 어려움을 겪고 있습니다. 요소가 성공적으로 추가되면 ObservableCollection을 실행하지만 아무 것도 표시되지 않습니다. 데이터 바인딩 된 ListBox (lBox) :UI가 아닌 스레드에서 ObservableCollection을 채운 후 WP7에서 크로스 스레드 액세스가 잘못되었습니다.
내가 가진 무엇 스레드, 그래서 뭐가 문제 야?크로스 스레드 액세스가 잘못되었습니다. XAML에서 다음
public partial class MainPage : PhoneApplicationPage { private ObservableCollection<string> obrazkiFinal = new ObservableCollection<string>(); public ObservableCollection<string> ObrazkiFinal { get { return obrazkiFinal; } set { obrazkiFinal = value; } } // Constructor public MainPage() { InitializeComponent(); } private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { lBox.ItemsSource = ObrazkiFinal; HttpWebRequest httpRequest = WebRequest.CreateHttp(@"http://website"); IAsyncResult res = httpRequest.BeginGetResponse(new AsyncCallback(RespResult),httpRequest); } private void RespResult(IAsyncResult respResylt) { var res = (HttpWebRequest)respResylt.AsyncState; var resp = res.EndGetResponse(respResylt); /* some parsing code */ foreach (/* found pic urls */) { //new httpwebrequest HttpWebRequest picHttpRequest = WebRequest.CreateHttp(picUrl); IAsyncResult picRes = picHttpRequest.BeginGetResponse(DownloadImageResult, picHttpRequest); } private void DownloadImageResult(IAsyncResult result) { var res = state.HttpWebRequest; var resp = res.EndGetResponse(result); /*some saving code*/ Dispatcher.BeginInvoke(() => { ObrazkiFinal.Add(fileName); }); } } }
과 :
<ListBox Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="Auto" Name="lBox"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <Image Source="{Binding}"></Image> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
및하여 PhoneApplicationPage 코스의
:DataContext="{Binding RelativeSource={RelativeSource Self}}"
ObservableCollection에이 성공적으로 UI 내에서 채워집니다 여기
@edit 스택 추적 첨가 :
System.Windows.FrameworkElement.GetValueInternal에서 System.Windows.DependencyObject.GetValueInternal (DependencyProperty에 DP)에 MS.Internal.XcpImports.CheckThread()에서(DependencyProperty에 DP)에 System.Net.Browser.ClientHttpWebRequest myPhoneApp.MainPage.DownloadImageResult에서 System.Windows.Controls.ItemsControl.get_ItemsSource에서 System.Windows.DependencyObject.GetValue (DependencyProperty에 DP) () (IAsyncResult를 결과)에 . 시스템에서 System.Threading.ThreadPool.WorkItem.WaitCallback_Context (객체 상태)에서 System.Threading.ExecutionContext.Run (ExecutionContext에 ExecutionContext에, ContextCallback 콜백, 객체 상태)에서 <> C_ DisplayClassa.b _8 (객체 상태 2) .Threading.ThreadPool.WorkItem.doWork System.Threading.Timer.ring에서 (객체 O) 는()
'fileName' 변수의 출처가 즉시 명확하지 않으므로 간략하게 일부 코드를 제거했습니다. 이 코드 중 어떤 것이 컬렉션을 수정합니까? –
아니요, 유일한 부분을 수정하는 컬렉션은 DownloadImageResult (IAsyncResult 결과)에 있습니다. – szysz3kster
스택 추적을 사용할 수 있습니까? –