Я пытаюсь динамически добавить пару кнопок управления в список в приложении Windows Phone.Когда я добавляю эти элементы управления в код и запускаю приложение, я получаю исключение, говорящее «Недопустимый межпоточный доступ».Ниже приведен мой код xaml и xaml.cs.
<StackPanel>
<ListBox x:Name="TripList" Height="465" HorizontalAlignment="Left" VerticalAlignment="Top" Width="456" Background="White" Foreground="Blue">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="0,0,0,4">
<StackPanel Orientation="Vertical" Width="456">
<StackPanel Orientation="Vertical">
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Width="370" FontSize="24" Text="{Binding PUDetails}"/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="24" Text="{Binding TripStatus}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<StackPanel VerticalAlignment="Top" Orientation="Horizontal" Width="300">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="Conf: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="{Binding ConfNumber}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="Est Do Tm: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="{Binding DOTime}"/>
</StackPanel>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Vertical">
<StackPanel Background="Azure" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="Svc: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="{Binding TripService}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="PU: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="{Binding PURoute}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="DO: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="{Binding DORoute}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="Pax: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="{Binding PaxDetails}"/>
</StackPanel>
</StackPanel>
</StackPanel>
<StackPanel x:Name="stknotchecked" VerticalAlignment="Top" Orientation="Vertical">
</StackPanel><StackPanel x:Name="stkaccepted"></StackPanel> </StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
Мне нужно добавить динамически созданные элементы управления на панель «stkbuttons».
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
TripList.ItemsSource = TripsListdata;
if (Tripaccept == "Accepted")
{
StackPanel stktest = FindDescendant<StackPanel>(TripList);
if (stktest.Name == "stkaccepted")
{
}
}
else
{
StackPanel stktest = FindDescendant<StackPanel>(TripList);
if(stktest.Name == "stknotchecked")
{
}
}
});
private T FindDescendant<T>(DependencyObject parentElement) where T : FrameworkElement
{
var count = VisualTreeHelper.GetChildrenCount(parentElement);
if (count == 0)
return null;
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is T)
{
return (T)child;
}
else
{
var result = FindDescendant<T>(child);
if (result != null)
return result;
}
}
return null;
}
Ниже приведена трассировка стекаисключения ..
at MS.Internal.XcpImports.CheckThread()
at System.Windows.DependencyObject..ctor(UInt32 nativeTypeIndex, IntPtr constructDO)
at System.Windows.DependencyObject..ctor(UInt32 nativeTypeIndex)
at System.Windows.Media.Brush..ctor(UInt32 nKnownTypeIndex)
at System.Windows.Media.SolidColorBrush..ctor()
at System.Windows.Media.SolidColorBrush..ctor(Color color)
at DriverAnywhere.MyTrips.ResponseCallBackTrips(IAsyncResult result)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa. <InvokeGetResponseCallback>b__8(Object state2)
at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadPool.WorkItem.doWork(Object o)
at System.Threading.Timer.ring()
Может ли кто-нибудь помочь мне решить мою проблему? .. Заранее спасибо ..