Chỉ cần để cung cấp một giải pháp thay thế. Có thể giữ một ứng dụng đang chạy mà không có bất kỳ cửa sổ nào đang mở. Đối với tôi điều này cảm thấy ít 'hackish'. :) http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx
public class AppCode : Application
{
// Entry point method
[STAThread]
public static void Main()
{
AppCode app = new AppCode();
app.ShutdownMode = ShutdownMode.OnExplicitShutdown;
app.Run();
...
app.Shutdown();
}
}
EDIT: Ok này có một chút rườm rà. Application.Run sẽ chặn, vì vậy nó cần phải chạy trong thread riêng của nó. Khi nó chạy theo chủ đề riêng của nó, bất kỳ tương tác nào giữa chuỗi chính của bạn và luồng ui của bạn đã được thực hiện tốt nhất bởi Application.Current.Dispatcher.Invoke. Đây là một số mã làm việc, giả định rằng bạn có một lớp kế thừa từ Ứng dụng. Tôi đang sử dụng một App.xaml/App.xaml.cs đã sửa đổi mà một mẫu dự án WPF tạo ra cho bạn, để xử lý miễn phí ResourceDictionaries miễn phí.
public class Program
{
// Entry point method
[STAThread]
public static void Main()
{
var thread = new System.Threading.Thread(CreateApp);
thread.SetApartmentState(System.Threading.ApartmentState.STA);
thread.Start();
// This is kinda shoddy, but the thread needs some time
// before we can invoke anything on the dispatcher
System.Threading.Thread.Sleep(100);
// In order to get input from the user, display a
// dialog and return the result on the dispatcher
var result = (int)Application.Current.Dispatcher.Invoke(new Func<int>(() =>
{
var win = new MainWindow();
win.ShowDialog();
return 10;
}), null);
// Show something to the user without waiting for a result
Application.Current.Dispatcher.Invoke(new Action(() =>
{
var win = new MainWindow();
win.ShowDialog();
}), null);
System.Console.WriteLine("result" + result);
System.Console.ReadLine();
// This doesn't really seem necessary
Application.Current.Dispatcher.InvokeShutdown();
}
private static void CreateApp()
{
App app = new App();
app.ShutdownMode = ShutdownMode.OnExplicitShutdown;
app.Run();
}
}
Nguồn
2011-09-13 07:07:50
Nếu bạn không 'Chạy()' một ứng dụng, bạn không có 'Ứng dụng'. Bạn đang làm gì đấy? – SLaks
Aaah, niềm vui khi sử dụng các điều khiển WPF bên ngoài ứng dụng WPF. – Will
Tôi đang cố gắng hiển thị các cửa sổ WPF từ một ứng dụng giao diện điều khiển đa luồng. Các trường hợp của cùng một cửa sổ được hiển thị nhiều lần có thể và sẽ xảy ra, vì vậy tình hình không quá dễ dàng! :/ – em70