“展示的替代方案是什么?”;请稍等;调用为非线程时wpf中的窗口
最近我需要在wpf应用程序中实现请等待对话框。我找到了下面的代码。这真的很好,但它总是在saprate线程中打开一个窗口并保持该位置。以下代码是否有其他更改。而我对代码的请求是非线程的“展示的替代方案是什么?”;请稍等;调用为非线程时wpf中的窗口,wpf,Wpf,最近我需要在wpf应用程序中实现请等待对话框。我找到了下面的代码。这真的很好,但它总是在saprate线程中打开一个窗口并保持该位置。以下代码是否有其他更改。而我对代码的请求是非线程的 private void NewWindowThread<T,P>(Func<P, T> constructor, P param) where T : Window { Thread thread = new Thread(() => { T w = constructor(par
private void NewWindowThread<T,P>(Func<P, T> constructor, P param) where T : Window
{
Thread thread = new Thread(() =>
{
T w = constructor(param);
w.Show();
w.Closed += (sender, e) => w.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
private void NewWindowThread(Func构造函数,P参数),其中T:Window
{
线程线程=新线程(()=>
{
T w=构造函数(参数);
w、 Show();
w、 Closed+=(sender,e)=>w.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run();
});
SetApartmentState(ApartmentState.STA);
thread.Start();
}
要调用上面的方法,请使用下面的行。加载窗口是您要在对话框中显示的窗口(请稍候。windows)
string t=“请稍候…”;
NewWindowThread(c=>newloadingWindow(c),t);
阻止ui线程从来都不是一个好主意,但它越来越成为一个坏主意
Windows将告诉用户您的应用程序已停止响应。这可能会促使他们强迫你申请。如果渲染进度条,它们将丢失动画效果,并且可能渲染不正确。在WPF中,gui动画将停止
使用后台线程进行繁重的处理,如果需要将数据写回到主线程使用的对象中,请将它们重新打包到gui线程中
BackgroundWorker
在那里很有用 这可能会帮到你
public partial class Splash : Window
{
private static Splash splash = new Splash();
// To refresh the UI immediately
private delegate void RefreshDelegate();
private static void Refresh(DependencyObject obj)
{
obj.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render,
(RefreshDelegate)delegate { });
}
public Splash()
{
InitializeComponent();
}
public static void BeginDisplay()
{
splash.Show();
}
public static void EndDisplay()
{
splash.Close();
}
public static void Loading(string test)
{
splash.statuslbl.Content = test;
Refresh(splash.statuslbl);
}
}
使用上述代码
Splash.BeginDisplay();
// Setting the status to show the application is still loading data
Splash.Loading("Connecting...");
// Set to sleep to simulate long running process
Thread.Sleep(1500);
Splash.Loading("Retrieving....");
Thread.Sleep(1500);
Splash.Loading("Success....");
Thread.Sleep(1500);
Splash.EndDisplay();
Splash.BeginDisplay();
// Setting the status to show the application is still loading data
Splash.Loading("Connecting...");
// Set to sleep to simulate long running process
Thread.Sleep(1500);
Splash.Loading("Retrieving....");
Thread.Sleep(1500);
Splash.Loading("Success....");
Thread.Sleep(1500);
Splash.EndDisplay();