如何在 WPF 中从一个 window 访问另一个 window 中的控制器

How to access controller from one window in another window in WPF

我有一个包含两个 xaml windows 的 WPF 应用程序,一个名为 MainWindow.xaml,另一个名为 addNewsWindow.xaml

MainWindow.xaml 中,我有一个 DocumentViewer 和一个名为 Add News 的按钮,它将我带到另一个名为 AddNewsWindow.xaml 的 window。

这是我在 MainWindow.xaml 中的 DocumentViewer 控件:

<DocumentViewer x:FieldModifier="public" x:Name="docViwer" 
   Grid.Row="2" Grid.RowSpan="4" Grid.ColumnSpan="4"
   BorderBrush="Black" BorderThickness="1" 
   Margin="1,2,40,1">

在我的 addNewsWindow.xaml 上,我有很多接受用户输入的控件和一个浏览按钮以及 select word 文件,这些文件将显示在 DocumentViewerMainWindow.xaml:

问题:

在为 addNewsWindow.xaml 中的添加按钮编写点击事件时(按下时应将 word 文件转换为 XPS 并放入 MainWindow 中的文档查看器),我可以't 引用 MainWindow DocumentViewer 并将转换后的 XPS 文件放入 DocumentViewer.

AddNewsWindow.cs

private void FilePathBtn_Click(object sender, RoutedEventArgs e)
{
    // Create OpenFileDialog 
    Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

    // Set filter for file extension and default file extension 
    dlg.DefaultExt = ".doc";
    dlg.Filter = "Word documents|*.doc;*.docx";

    // Display OpenFileDialog by calling ShowDialog method 
    Nullable<bool> result = dlg.ShowDialog();

    // Get the selected file name and display in a TextBox 
    if (result == true)
    {
        // Open document 
        string filename = dlg.FileName;
        filePathTBox.Text = filename;
    }

}
private XpsDocument ConvertWordToXps(string wordFilename, string xpsFilename)
{
    // Create a WordApplication and host word document 
    Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    try
    {
        wordApp.Documents.Open(wordFilename);

        // To Invisible the word document 
        wordApp.Application.Visible = false;

        // Minimize the opened word document 
        wordApp.WindowState = WdWindowState.wdWindowStateMinimize;

        Document doc = wordApp.ActiveDocument;

        doc.SaveAs(xpsFilename, WdSaveFormat.wdFormatXPS);

        XpsDocument xpsDocument = new XpsDocument(xpsFilename, FileAccess.Read);
        return xpsDocument;
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error occurs, The error message is  " + ex.ToString());
        return null;
    }
    finally
    {
        wordApp.Documents.Close();
        ((_Application)wordApp).Quit(WdSaveOptions.wdDoNotSaveChanges);
    }
}

private void AddNewsBtn_Click(object sender, RoutedEventArgs e)
{
    string wordDocument = filePathTBox.Text;
    if (string.IsNullOrEmpty(wordDocument) || !File.Exists(wordDocument))
    {
        MessageBox.Show("The file is invalid. Please select an existing file again.");
    }
    else
    {
        string convertedXpsDoc = string.Concat(System.IO.Path.GetTempPath(), "\", Guid.NewGuid().ToString(), ".xps");
        XpsDocument xpsDocument = ConvertWordToXps(wordDocument, convertedXpsDoc);
        if (xpsDocument == null)
        {
            return;
        }

        // MainWindow.docViewer = xpsDocument.GetFixedDocumentSequence();
       docViewer.Document = xpsDocument.GetFixedDocumentSequence();
    }
}

我收到错误:

The name docViewer does not exist in the current context

我不知道如何从 AddnewsWindow.cs

中引用 MainWindow 中的 DocumentViewer

不清楚您是如何创建和显示 AddNewsWindow 的。您可以将 MainWindow 传递给 AddNewsWindow(通过 this):

var addNewsWindow = new AddNewsWindow(this);

MainWindow可以定义更新文档的方法:

public void SetDocument(XpsDocument xpsDocument)
{
    docViewer.Document = xpsDocument.GetFixedDocumentSequence();
}

然后,从AddNewsWindow,你可以调用:

mainWindow.SetDocument(xpsDocument);

或者,您可以直接获取 MainWindow,而不是将 MainWindow 传递给 AddNewsWindow

var mainWindow = (MainWindow)Application.Current.MainWindow;

你需要转换它的原因是 Application.Current.MainWindow returns Window 的一个实例,而不是你的 MainWindow。您需要 MainWindow 才能调用其 SetDocument 方法。