我創建了一個WPF應用程序,然後通過刪除app.xaml並將構建設置爲類庫將其轉換爲DLL。我正在使用C#控制檯應用程序來測試DLL。對於我的第一次測試,我能夠讓應用程序顯示得很好,如果我把mainwindow.show()放在mainWindow = new MainWindow()下的try塊中。現在我需要能夠預加載wpf應用程序,並且只在需要時顯示它,而不必每次都加載它。我遇到的問題是顯示wpf應用程序的調用位於不同的線程上,並且ShowWPFAppDLL()主窗口爲空。任何方式我可以得到這個工作?從控制檯應用程序顯示DLL的問題
控制檯應用程序:
namespace ConsoleApp
{
class Program
{
static WPFAppDLL.LoadWpfAppDll loader = new WPFAppDLL.LoadWpfAppDll();
static void Main(string[] args)
{
Thread worker = new Thread(new ThreadStart(LoadWpfApp));
worker.SetApartmentState(ApartmentState.STA);
worker.Name = "WpfThread";
worker.IsBackground = true;
worker.Start();
Thread.Sleep(15000);
ShowWpfApp();
worker.Join();
}
private static void ShowWpfApp()
{
loader.ShowWPFAppDLL();
}
private static void LoadWpfApp()
{
loader.Load();
}
}
}
WPF應用程序(DLL):
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace WPFAppDLL
{
public class LoadWpfAppDll
{
MainWindow mainWindow = null;
public void Load(string[] args)
{
Application application = new Application();
application.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/WPFAppDLL;component/Resources/DataTemplates/DataTemplate.xaml", UriKind.RelativeOrAbsolute) });
application.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/WPFAppDLL;component/Resources/Styles/GlobalStyles.xaml", UriKind.RelativeOrAbsolute) });
application.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/WPFAppDLL;component/Resources/Styles/ImageResources.xaml", UriKind.RelativeOrAbsolute) });
application.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/WPFAppDLL;component/Resources/Styles/BrushesStyles.xaml", UriKind.RelativeOrAbsolute) });
application.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/WPFAppDLL;component/Resources/Styles/TabControlStyles.xaml", UriKind.RelativeOrAbsolute) });
application.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/WPFAppDLL;component/Resources/Styles/ButtonStyles.xaml", UriKind.RelativeOrAbsolute) });
application.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/WPFAppDLL;component/Resources/Styles/LabelStyles.xaml", UriKind.RelativeOrAbsolute) });
application.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/WPFAppDLL;component/Resources/Styles/TextboxStyles.xaml", UriKind.RelativeOrAbsolute) });
application.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/WPFAppDLL;component/Resources/Styles/ComboBoxStyles.xaml", UriKind.RelativeOrAbsolute) });
application.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/WPFAppDLL;component/Resources/Styles/DatagridStyles.xaml", UriKind.RelativeOrAbsolute) });
application.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/WPFAppDLL;component/Resources/Styles/GroupBoxStyles.xaml", UriKind.RelativeOrAbsolute) });
application.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/WPFAppDLL;component/Resources/Styles/CheckBoxStyles.xaml", UriKind.RelativeOrAbsolute) });
application.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/WPFAppDLL;component/Resources/Styles/RadioButtonStyles.xaml", UriKind.RelativeOrAbsolute) });
application.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/WPFAppDLL;component/Resources/Styles/Converters.xaml", UriKind.RelativeOrAbsolute) });
application.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/WPFAppDLL;component/Resources/Styles/ListBoxStyles.xaml", UriKind.RelativeOrAbsolute) });
application.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/WPFAppDLL;component/Resources/Styles/MessageBoxStyles.xaml", UriKind.RelativeOrAbsolute) });
SplashScreenWindow splashWindow = new SplashScreenWindow();
splashWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
splashWindow.Show();
EventManager.RegisterClassHandler(typeof(TextBox), TextBox.PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(SelectivelyIgnoreMouseButton));
EventManager.RegisterClassHandler(typeof(TextBox), TextBox.GotKeyboardFocusEvent, new RoutedEventHandler(SelectAllText));
EventManager.RegisterClassHandler(typeof(TextBox), TextBox.MouseDoubleClickEvent, new RoutedEventHandler(SelectAllText));
try
{
mainWindow = new MainWindow();
mainWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
splashWindow.Close();
application.Run();
}
catch (Exception ex)
{
splashWindow.Close();
MessageBox.Show("Error starting application:" + Environment.NewLine + ex.ToString(), "Error Message", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
public void ShowWPFAppDLL()
{
if (mainWindow != null)
{
mainWindow.Show();
}
}
private void SelectivelyIgnoreMouseButton(object sender, MouseButtonEventArgs e)
{
DependencyObject parent = e.OriginalSource as UIElement;
while (parent != null && !(parent is TextBox))
{
parent = VisualTreeHelper.GetParent(parent);
}
if (parent != null)
{
TextBox textBox = (TextBox)parent;
if (!textBox.IsKeyboardFocusWithin)
{
textBox.Focus();
e.Handled = true;
}
}
}
private void SelectAllText(object sender, RoutedEventArgs e)
{
TextBox textBox = e.OriginalSource as TextBox;
if (textBox != null)
{
textBox.SelectAll();
}
}
}
}
那麼上面的代碼到底在哪裏失敗? – Fayilt
顯然,* mainWindow *僅在* Load *方法中初始化。在使用* mainWindow *之前,您需要調用它一次。當* mainWindow *爲null時,什麼會阻止你從方法ShowWPFAppDLL中調用* Load *?這真的是你的問題嗎? – elgonzo
@Fayilt當我調用ShowWPFAppDLL()時,mainWindow爲null,所以它不會像我想要的那樣顯示。 – Danno