2

最初のビュー (MainView) に 2 つのビューがあり、ファイルを選択してインポートします。2 番目のビュー (BView) では、このファイルの詳細をデータグリッドに表示します。

これは最初のビュー (MainView) です。

ここに画像の説明を入力

これは 2 番目のビュー (BView) です。 ここに画像の説明を入力

「インポート」をクリックすると、プログレスバーとテキストに表示され、2番目のビューがロードされます。別の 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");
            });
        }         

    }

私は何ができますか?

4

1 に答える 1