问题描述
你好,
我想使用一个简单的例程:如果用户单击一个按钮,则该按钮消失,并且显示进度条.处理完成后,该按钮变为可见.
Hello,
I want to use a simple routine: if user clicks a button, then the button disappears and progress bar shows up. After processing is done, the button becomes visible.
viewModel.StartRendering();
string script = viewModel.LilyScript;
ThreadStart x = delegate
{
Thread.Sleep(1000); // simulate long processing
lily.ProcessScript(script);
Dispatcher.Invoke(new Action(delegate
{
viewModel.LilyRenderOutput = lily.ImageSource;
viewModel.EndRendering();
}), DispatcherPriority.Normal);
};
Thread thr = new Thread(x);
thr.Name = "Renderowanie";
thr.Start();
viewModel具有dep属性,这些属性绑定到控件的属性.
The viewModel has dep properties which are bound to controls'' properties.
class MainWindowVM
{
...
// (obvious code omitted)
public Visibility RenderingProgressBarVisibility...
public static readonly DependencyProperty ...
public Visibility PaintNotesButtonVisibility...
public static readonly DependencyProperty ...
public ImageSource LilyRenderOutput
internal void StartRendering()
{
RenderingProgressBarVisibility = Visibility.Visible;
PaintNotesButtonVisibility = Visibility.Hidden;
}
internal void EndRendering()
{
RenderingProgressBarVisibility = Visibility.Hidden;
PaintNotesButtonVisibility = Visibility.Visible;
}
}
我有一个异常调用线程无法访问该对象,因为其他线程拥有它.在线
I got an exception The calling thread cannot access this object because a different thread owns it. on line
viewModel.LilyRenderOutput = lily.ImageSource;
在xaml中:
In xaml:
<Image Source="{Binding LilyRenderOutput}" Stretch="None" />
有趣的事实:代码
Interesting fact: the code
Dispatcher.Invoke(new Action(delegate
{
this.Title="abc";
}), DispatcherPriority.Normal);
确实有效.我使用错误的调度程序还是什么?我尝试过viewModel.Dispather没有成功.
感谢任何帮助.
does work. Do I use a wrong dispatcher or what? I tried viewModel.Dispather with no success.
Any help appreciated.
推荐答案
这篇关于如何从另一个线程访问和修改绑定到控件属性的依赖项属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!