加载内容时的透明窗口

时间:2012-09-06 13:35:58

标签: c# wpf window transparent mahapps.metro

可悲的是,在stackoverflow上没有关于这个问题的单一问题。至少,没有我在搜索时碰到的。

无论如何,当我要谈论的程序正在构建时。出现的第一个窗口是登录。当用户输入正确的登录信息时,将显示主窗口。但是,在主窗口上有大量信息是从互联网上收集的。

这使得主窗口保持透明,如下图[1]所示,持续一段合理的时间。从互联网收集的信息包括一些xml以及来自MySQL数据库的数据。

我有一个类似的Window_Loaded事件;

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        method1();
        method2(1);
        method3();
        .
        .
        .
        //method6();
    }

所以,很明显,当我取消某些方法并让这个事件少于时间时,窗口在进入正常状态之前保持透明,变得更小。

但是,我想要做的是正常加载窗口,然后可能有加载指示器来通知用户正在加载内容。

p.s我正在使用mahapps.metro控件。

提前谢谢

2 个答案:

答案 0 :(得分:1)

这是因为你在UI线程上运行阻塞代码,因此窗口没有机会重新绘制。
你需要在后台线程中完成所有这些工作。

答案 1 :(得分:0)

试试这个

MainWindow和Code。

<Window x:Class="SplashScreenWithStatus.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="600" Width="800" Loaded="Window_Loaded">
    <Grid>

    </Grid>
</Window>



 public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            // 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);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Splash.EndDisplay();
        }
    }

启动画面和代码

                                                                                                                                                                                                                                                                                                                                                                                                                                                 

公共部分类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);
    }

    }

App Class xaml和Code

<Application x:Class="SplashScreenWithStatus.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml" Startup="Application_Startup">
    <Application.Resources>

    </Application.Resources>
</Application>

 public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            Splash.BeginDisplay();
        }
    }
相关问题