如何从WPF的另一个窗口中的一个窗口访问控制器

时间:2019-01-19 20:33:26

标签: c# wpf controls

我有一个带有两个xaml窗口的WPF应用程序,一个名为MainWindow.xaml,另一个是addNewsWindow.xaml

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

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

<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">

enter image description here

在我的addNewsWindow.xaml上,我有很多控件来接受用户输入,还有一个按钮来浏览和选择单词文件,该文件将显示在DocumentViewer的{​​{1}}中:

enter image description here

问题:

在对MainWindow.xaml中的“添加”按钮的单击事件进行编码时(按下该按钮后应将单词文件转换为XPS并在addNewsWindow.xaml中放入文档查看器中),我无法引用MainWindow,然后将转换后的XPS文件放入MainWindow DocumentViewer

AddNewsWindow.cs

DocumentViewer

我得到了错误:

  

名称docViewer在当前上下文中不存在

我不知道如何从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(); } } 引用DocumentViewer中的MainWindow

1 个答案:

答案 0 :(得分:0)

不清楚如何创建和显示AddNewsWindow。您有可能(通过MainWindowAddNewsWindow传递到this

var addNewsWindow = new AddNewsWindow(this);

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

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

然后,您可以从AddNewsWindow致电:

mainWindow.SetDocument(xpsDocument);

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

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

您需要进行强制转换的原因是Application.Current.MainWindow返回Window的实例,而不是您的MainWindow。您需要MainWindow才能调用其SetDocument方法。