这是工作代码(但在 UI 线程上):
<ContentControl
Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"
Focusable="True"
HorizontalAlignment="Right"
Content="{Binding Work}" >
</ContentControl>
Run Code Online (Sandbox Code Playgroud)
这是我试图制作的代码,但它不起作用:(代码隐藏)
InitializeComponent();
var thread = new Thread(
() =>
{
var a = new ContentControl { Focusable = true, HorizontalAlignment = HorizontalAlignment.Right };
var binding = new Binding { Path = new PropertyPath("Work"), };
a.SetBinding(ContentProperty, binding);
MainGrid.Dispatcher.Invoke(new Action(() => { MainGrid.Children.Add(a); }));
}) { IsBackground = true };
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
Run Code Online (Sandbox Code Playgroud)
我的目标是创建 aConentControl并将其放置在网格中,该网格位于listview具有多个 的a 内ContentControls。)所示的解决方案正在崩溃,因为不同的线程拥有MainGrid. 有人对此有好的想法吗?(很想在另一个线程上执行此操作,因为它ContentControl …
似乎无法通过GC收集动作.为什么?
WeakReference reference = null;
WeakReference reference2 = null;
new Action(() =>
{
Action<string> deliveryMessage = (ans) => { };
object a = new object();
reference = new WeakReference(deliveryMessage);
reference2 = new WeakReference(a);
}) ();
GC.Collect();
GC.WaitForPendingFinalizers();
Assert.Null(reference2.Target); //True
Assert.Null(reference.Target); //False
Run Code Online (Sandbox Code Playgroud)
我将在WeakReference列表中使用Action,但我需要先通过这个简单的测试才能完成...