C# 使用窗口作为加载屏幕
我无法直接找到任何东西来解决我的这个问题。 我正在尝试制作一个动画gif作为加载屏幕,用于我的程序中加载时间很长的几个地方 我让它在创业公司工作,但一旦我试图再次使用它,它崩溃了我的程序。我从来没有完全理解过整个线程,所以我的代码中可能有错误 [编辑]我想要的是当一个沉重的方法运行时,比如说当它从数据库或一个简单的线程获取所有数据时,启动加载屏幕。睡眠(#######),此时我想要一个动态的splashscreen运行,这个屏幕是我的第二个窗口,上面有一个动画gif。我打开了窗户,打开了窗户 我要检查的窗户是空的C# 使用窗口作为加载屏幕,c#,wpf,xaml,dynamic-splash-screen,C#,Wpf,Xaml,Dynamic Splash Screen,我无法直接找到任何东西来解决我的这个问题。 我正在尝试制作一个动画gif作为加载屏幕,用于我的程序中加载时间很长的几个地方 我让它在创业公司工作,但一旦我试图再次使用它,它崩溃了我的程序。我从来没有完全理解过整个线程,所以我的代码中可能有错误 [编辑]我想要的是当一个沉重的方法运行时,比如说当它从数据库或一个简单的线程获取所有数据时,启动加载屏幕。睡眠(#######),此时我想要一个动态的splashscreen运行,这个屏幕是我的第二个窗口,上面有一个动画gif。我打开了窗户,打开了窗户 我
- MainWindow.xaml
- 加载.xaml
- App.xaml
public partial class App : Application
{
public static ISplashScreen splashScreen;
private ManualResetEvent ResetSplashCreated;
private Thread SplashThread;
protected override void OnStartup(StartupEventArgs e)
{
// ManualResetEvent acts as a block. It waits for a signal to be set.
ResetSplashCreated = new ManualResetEvent(false);
// Create a new thread for the splash screen to run on
SplashThread = new Thread(ShowSplash);
SplashThread.SetApartmentState(ApartmentState.STA);
SplashThread.IsBackground = true;
SplashThread.Name = "Splash Screen";
SplashThread.Start();
// Wait for the blocker to be signaled before continuing. This is essentially the same as: while(ResetSplashCreated.NotSet) {}
ResetSplashCreated.WaitOne();
base.OnStartup(e);
}
private void ShowSplash()
{
// Create the window
Loading animatedSplashScreenWindow = new Loading();
splashScreen = animatedSplashScreenWindow;
// Show it
animatedSplashScreenWindow.Show();
// Now that the window is created, allow the rest of the startup to run
ResetSplashCreated.Set();
System.Windows.Threading.Dispatcher.Run();
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Action that takes time here
App.splashScreen.LoadComplete();
}
private void _btnVisUKategorier_Click(object sender, RoutedEventArgs e)
{
App.splashScreen.LoadStart();
FyldUKategorier();
App.splashScreen.LoadComplete();
}
public Loading()
{
InitializeComponent();
}
public void LoadComplete()
{
Dispatcher.InvokeShutdown();
}
}
public interface ISplashScreen
{
void LoadComplete();
}
这个页面上只有一个使用WpfAnimatedGif包的动画gif。窗户的其余部分是透明的
public partial class Loading : Window, ISplashScreen
{
public Loading()
{
InitializeComponent();
}
public void LoadStart()
{
Dispatcher.Invoke((Action)delegate()
{
//this.UpdateMessageTextBox.Text = message;
});
}
public void LoadComplete()
{
Dispatcher.InvokeShutdown();
}
}
public interface ISplashScreen
{
void LoadStart();
void LoadComplete();
}
}
App.xaml
public partial class App : Application
{
public static ISplashScreen splashScreen;
private ManualResetEvent ResetSplashCreated;
private Thread SplashThread;
protected override void OnStartup(StartupEventArgs e)
{
// ManualResetEvent acts as a block. It waits for a signal to be set.
ResetSplashCreated = new ManualResetEvent(false);
// Create a new thread for the splash screen to run on
SplashThread = new Thread(ShowSplash);
SplashThread.SetApartmentState(ApartmentState.STA);
SplashThread.IsBackground = true;
SplashThread.Name = "Splash Screen";
SplashThread.Start();
// Wait for the blocker to be signaled before continuing. This is essentially the same as: while(ResetSplashCreated.NotSet) {}
ResetSplashCreated.WaitOne();
base.OnStartup(e);
}
private void ShowSplash()
{
// Create the window
Loading animatedSplashScreenWindow = new Loading();
splashScreen = animatedSplashScreenWindow;
// Show it
animatedSplashScreenWindow.Show();
// Now that the window is created, allow the rest of the startup to run
ResetSplashCreated.Set();
System.Windows.Threading.Dispatcher.Run();
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Action that takes time here
App.splashScreen.LoadComplete();
}
private void _btnVisUKategorier_Click(object sender, RoutedEventArgs e)
{
App.splashScreen.LoadStart();
FyldUKategorier();
App.splashScreen.LoadComplete();
}
public Loading()
{
InitializeComponent();
}
public void LoadComplete()
{
Dispatcher.InvokeShutdown();
}
}
public interface ISplashScreen
{
void LoadComplete();
}
main window.xaml
public partial class App : Application
{
public static ISplashScreen splashScreen;
private ManualResetEvent ResetSplashCreated;
private Thread SplashThread;
protected override void OnStartup(StartupEventArgs e)
{
// ManualResetEvent acts as a block. It waits for a signal to be set.
ResetSplashCreated = new ManualResetEvent(false);
// Create a new thread for the splash screen to run on
SplashThread = new Thread(ShowSplash);
SplashThread.SetApartmentState(ApartmentState.STA);
SplashThread.IsBackground = true;
SplashThread.Name = "Splash Screen";
SplashThread.Start();
// Wait for the blocker to be signaled before continuing. This is essentially the same as: while(ResetSplashCreated.NotSet) {}
ResetSplashCreated.WaitOne();
base.OnStartup(e);
}
private void ShowSplash()
{
// Create the window
Loading animatedSplashScreenWindow = new Loading();
splashScreen = animatedSplashScreenWindow;
// Show it
animatedSplashScreenWindow.Show();
// Now that the window is created, allow the rest of the startup to run
ResetSplashCreated.Set();
System.Windows.Threading.Dispatcher.Run();
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Action that takes time here
App.splashScreen.LoadComplete();
}
private void _btnVisUKategorier_Click(object sender, RoutedEventArgs e)
{
App.splashScreen.LoadStart();
FyldUKategorier();
App.splashScreen.LoadComplete();
}
public Loading()
{
InitializeComponent();
}
public void LoadComplete()
{
Dispatcher.InvokeShutdown();
}
}
public interface ISplashScreen
{
void LoadComplete();
}
PS:如果你知道任何更好的解决方案,它会像我在这里尝试做的一样,请随时提供它的链接
[Edit]我更改了,所以我不再使用app.xaml,因为我不想在启动时使用它,所以我将代码移到将要使用它的主窗口中
现在它不再崩溃了,但它只是第一次显示我的窗口,任何超出此范围的内容都不会显示任何内容
[Edit-2]我进行了一些测试,并决定让窗口看起来更好,这时我遇到了确切的问题所在。
在我将上面的代码移到Main中,而不是在App.xaml中运行之后,似乎上面的代码现在可以正常工作了。我取下了钥匙
public void LoadStart()
{
Dispatcher.Invoke((Action)delegate()
{
//this.UpdateMessageTextBox.Text = message;
});
}
也是从Loading.xaml开始的,因为我没有任何东西可用于if。在我下面的示例中,它的用途是更新页面上的标签
问题似乎出在我安装的软件包上,这些软件包本应向我显示动画gif
<Image gif:ImageBehavior.RepeatBehavior="Forever" gif:ImageBehavior.AnimatedSource="/img/Animated_ARKA_Loading.gif" />
加载.xaml
public partial class App : Application
{
public static ISplashScreen splashScreen;
private ManualResetEvent ResetSplashCreated;
private Thread SplashThread;
protected override void OnStartup(StartupEventArgs e)
{
// ManualResetEvent acts as a block. It waits for a signal to be set.
ResetSplashCreated = new ManualResetEvent(false);
// Create a new thread for the splash screen to run on
SplashThread = new Thread(ShowSplash);
SplashThread.SetApartmentState(ApartmentState.STA);
SplashThread.IsBackground = true;
SplashThread.Name = "Splash Screen";
SplashThread.Start();
// Wait for the blocker to be signaled before continuing. This is essentially the same as: while(ResetSplashCreated.NotSet) {}
ResetSplashCreated.WaitOne();
base.OnStartup(e);
}
private void ShowSplash()
{
// Create the window
Loading animatedSplashScreenWindow = new Loading();
splashScreen = animatedSplashScreenWindow;
// Show it
animatedSplashScreenWindow.Show();
// Now that the window is created, allow the rest of the startup to run
ResetSplashCreated.Set();
System.Windows.Threading.Dispatcher.Run();
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Action that takes time here
App.splashScreen.LoadComplete();
}
private void _btnVisUKategorier_Click(object sender, RoutedEventArgs e)
{
App.splashScreen.LoadStart();
FyldUKategorier();
App.splashScreen.LoadComplete();
}
public Loading()
{
InitializeComponent();
}
public void LoadComplete()
{
Dispatcher.InvokeShutdown();
}
}
public interface ISplashScreen
{
void LoadComplete();
}
仅仅添加一个splashscreen并不能解决这个问题,因为我一开始就这么做了,但是程序中的一些加载要花2个月的时间,所以Being能够看到移动的加载让用户明白,它实际上并没有卡住,只是一张没有动画的图片,上面说加载似乎也有点冻结。我有splashscreen作为备份,因为它已经正常工作了:)我遵循这一点是为了让它在第一时间起作用,嗯,我想你误解了我想要的:)博客很好。这适用于启动加载,您可以在其中动态更改其上的文本。我想要的是,当程序运行时,我点击一个按钮,然后它从数据库加载一吨,同时它将显示加载屏幕。我现在正试图从app.xaml中删除它,但仍然没有找到任何修复方法。我在atm上的位置显示第一次加载和第二次加载的时间,它的清空时间点是
OnStartup()
returns您打开了两个窗口还是只有一个?换句话说,你的意图是显示一个启动屏幕;稍微关上它;然后打开main窗口
?如果我在启动时运行,它会显示良好的屏幕,并在主窗口显示yes后关闭它。在发生这种情况时,只有一个页面是打开的。但就像我说的,这不是我想要的。我不知道,我在这里的帖子中添加了一些编辑字段,并更新了我的位置。似乎我必须找到另一种方式来显示Gif,我甚至可能会使用循环和一些线程。Sleep(#######)在4张图片之间切换以模拟动画,因为Gif无论如何只有4张图片长