【发布时间】:2013-04-03 08:55:49
【问题描述】:
我在第一个视图(MainView)中有 2 个视图,我选择一个文件并导入它,在第二个视图(BView)中,在数据网格中显示该文件的详细信息。
这是第一个视图(MainView):
这是第二个视图(BView):
我希望当我点击“导入”时出现在进度条和文本上,同时加载第二个视图。我想在另一个 TASK 中打开另一个视图,但收到以下错误消息:
“调用线程无法访问此对象,因为另一个线程拥有它。”
这是MainViewModel的代码是:
[Export(typeof(IShell))]
public class MainViewModel : Screen
{
public string Path{ get; set; }
public bool IsBusy { get; set; }
public string Text { get; set; }
[Import]
IWindowManager WindowManager { get; set; }
public MainViewModel()
{
IsBusy = false;
Text = "";
}
public void Open()
{
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "Text|*.txt|All|*.*";
fd.FilterIndex = 1;
fd.ShowDialog();
Path= fd.FileName;
NotifyOfPropertyChange("Path");
}
public void Import()
{
if (Percorso != null)
{
IsBusy = true;
Text = "Generate..";
NotifyOfPropertyChange("IsBusy");
NotifyOfPropertyChange("Text");
Task.Factory.StartNew(() => GoNew());
}
else
{
MessageBox.Show("Select file!", "Error",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
public void GoNew()
{
WindowManager.ShowWindow(new BViewModel(Path), null, null);
Execute.OnUIThread(() =>
{
IsBusy = false;
NotifyOfPropertyChange("IsBusy");
Text = "";
NotifyOfPropertyChange("Text");
});
}
}
我能做什么?
【问题讨论】:
-
这可能与
WindowManager.ShowWindow不在 UI 线程上有关吗?看起来其他所有内容都被编组到 UI 线程 - 哪一行会引发错误? -
这一行:WindowManager.ShowWindow(new BViewModel(Path), null, null);
标签: c# wpf view caliburn.micro