MSDN说System.Windows.Application的公共静态成员是线程安全的.但是当我尝试使用多个线程运行我的应用程序时,我得到以下异常:
ArgumentException: An entry with the same key already exists.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.SortedList`2.Add(TKey key, TValue value)
at System.IO.Packaging.Package.AddIfNoPrefixCollisionDetected(ValidatedPartUri partUri,
PackagePart part)
at System.IO.Packaging.Package.GetPartHelper(Uri partUri)
at System.IO.Packaging.Package.GetPart(Uri partUri)
at System.Windows.Application.GetResourceOrContentPart(Uri uri)
at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean
bSkipJournaledProperties)
at System.Windows.Application.LoadComponent(Uri resourceLocator)
Run Code Online (Sandbox Code Playgroud)
以下调用发生异常:
genericResources = (ResourceDictionary)Application.LoadComponent(new Uri("/Themes/Generic.xaml", UriKind.Relative));
Run Code Online (Sandbox Code Playgroud)
该应用程序在单个线程上工作正常,甚至在两个或三个上.当我从5点起床后,每次都会收到错误.难道我做错了什么?我该怎么做才能解决这个问题?
我发现Pro Power Tools Extension的一些功能很烦人.有没有办法打开和关闭功能?
我试图创建一些图表图像,而无需在屏幕上显示这些图表.我已经在这方面工作了很长一段时间并尝试了很多不同的东西,但似乎没有任何工作.如果我首先在窗口中显示图表,代码可以正常工作,但如果我不在窗口中显示它,则位图只是白色,带有黑色边框(不知道为什么).
我尝试在渲染之前将图表添加到边框,并为边框指定绿色borderBrush.在位图中,我看到绿色borderBrush然后是黑色边框和白色背景但没有图表.图表不包含在黑色寄宿生中,所以我不知道它来自哪里.
我已经尝试将图表添加到窗口而不调用window.Show()并再次我得到黑色边框和白色背景.但是,如果我调用window.Show(),则位图包含图表.
我已经使用drawingVisual作为解释试图在这里,同样的结果.
这是代码(不包括将元素添加到边框或窗口):
private static BitmapSource CreateElementScreenshot(FrameworkElement element, int dpi)
{
if (!element.IsMeasureValid)
{
Size size = new Size(element.Width, element.Height);
element.Measure(size);
element.Arrange(new Rect(size));
}
element.UpdateLayout();
var scale = dpi/96.0;
var renderTargetBitmap = new RenderTargetBitmap
(
(int)(scale * element.RenderSize.Width),(int)(scale * element.RenderSize.Height),dpi,dpi,PixelFormats.Default
);
// this is waiting for dispatcher to perform measure, arrange and render passes
element.Dispatcher.Invoke(((Action)(() => renderTargetBitmap.Render(element))), DispatcherPriority.Render);
return renderTargetBitmap;
}
Run Code Online (Sandbox Code Playgroud)
注意:图表是ContentControl.
无论如何我可以让图表呈现而不先在窗口中显示它吗?
我想做一个ListBox像一个函数Grid.每次添加新项目时,它应该看起来像是添加了一个新项目GridRow(高度为星标).因此,如果有两个项目,它们将占用可用空间的一半.在某些时候,Grid行将小于将扩展的项目MinHeight,Grid并且包含ScrollViewer可以启动.
您将在一个网格内部看到此行为ScrollViewer.但是,我需要让这个工作,ListBox所以我可以设置ItemsSource,创建一个DataTemplate并继续前进.
默认问题ListBox ItemsPanel是它不会让我的第一个项目展开以填充所有可用空间.
更新:这是让它工作的代码:
<ListBox VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Width="Auto" Height="Auto">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="1"></UniformGrid>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
Run Code Online (Sandbox Code Playgroud)