Window.Show() 不显示控件但 Window.ShowDialog() 会 [英] Window.Show() doesn't display controls but Window.ShowDialog() does

查看:20
本文介绍了Window.Show() 不显示控件但 Window.ShowDialog() 会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在单击按钮时创建一个新的 Window,但是当我使用 Window.Show() 显示它时,只显示了 Window 框架本身(不是内容).当我做同样的事情,但使用 Window.ShowDialog() 显示它时,显示控件.

I'm trying to create a new Window on a button click, but when I display it using Window.Show() only the Window frame itself is shown (not the content). When I do the same thing, but display it using Window.ShowDialog(), the controls are shown.

以下代码显示了使用 Window.Show() 创建和显示对话框的过程:

The following code shows the creating and displaying of the dialog using Window.Show():

//in MainWindow.xaml.cs
ProgressBox prg = new ProgressBox("wErg", "Connecting to device...");
prg.Show();
; //do stuff 
prg.Close();

这会产生:

虽然下面的代码...

//in MainWindow.xaml.cs
ProgressBox prg = new ProgressBox("wErg", "Connecting to device...");
prg.ShowDialog();
;//do stuff
prg.Close();

生产:

窗口代码:

ProgressBox.xaml:

ProgressBox.xaml:

<Window x:Class="wErg.ProgressBox"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="" Height="106" Width="300" ResizeMode="NoResize">
    <Grid Margin="10,10,10,10">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock x:Name="Status" TextWrapping="Wrap" Text="TextBlock" Grid.Row="0"/>
        <Grid Grid.Row="1">
            <ProgressBar x:Name="ProgressBar" Height="20"/>
            <TextBlock x:Name="Percentage" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Grid>

    </Grid>
</Window>

ProgressBox.xaml.cs:

ProgressBox.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Threading;

namespace SomeNameSpace
{
    /// <summary>
    /// Interaction logic for ProgressBox.xaml
    /// </summary>
    public partial class ProgressBox : Window
    {

        /*
         * <summary>
         * Constructs a progress bar with a title, text, lower bound (min value), 
         * upper bound (max value), and starting position.
         * </summary>
         */
        public ProgressBox(
            String Title,
            String Status,
            int min,
            int max,
            int pos)
        {
            InitializeComponent();
            this.Title = Title;
            this.Status.Text = Status;
            this.ProgressBar.Minimum = min;
            this.ProgressBar.Maximum = max;
            this.ProgressBar.Value = pos;
            this.DataContext = this;
        }

        public ProgressBox(
            String Title,
            String Status)
        {
            InitializeComponent();
            this.Title = Title;
            this.Status.Text = Status;
            this.ProgressBar.IsIndeterminate = true;
        }

        /*
         * <summary>
         * Sets the text of the progress dialog
         * </summary>
         */
        public void SetStatus(String Status)
        {
            this.Status.Text = Status;            
        }

        /*
         * <summary>
         * Sets the position of the progress on the bar.
         * and updates the percent string.
         * </summary>
         */
        public bool SetPosition(int pos)
        {
            bool outcome = false;
            if(ProgressBar.Minimum <= pos && pos <= ProgressBar.Maximum)
            {
                this.ProgressBar.Value = pos;
                int percentage = Convert.ToInt32((pos - ProgressBar.Minimum) * 100 / (ProgressBar.Maximum - ProgressBar.Minimum));
                Percentage.Text = percentage.ToString() + "%";
                outcome = true;
            }
            return outcome;
        }


    }
}

推荐答案

如果您使用 .show() 打开,您的主窗口线程仍然有效.但是,如果您使用 ShowDialog() 打开进度条窗口,则主线程停止工作并开始处理进度条线程.

Your Main window Thread is still working if you open using .show() . But if you open progress bar window using ShowDialog() then main thread stop working and start working on progress bar thread.

使用后台工作者来执行//do 事情.

Use background worker to perform //do stuff.

尝试使用以下代码:

            BackgroundWorker bw = new BackgroundWorker();
            ProgressBox prg = new ProgressBox("wErg", "Connecting to device...");
            bw.DoWork += (o, ea) =>
            {
                //do stuff
            };
            bw.RunWorkerCompleted += (o, ea) =>
            {
                prg.Close();
            };
            prg.Show();
            bw.RunWorkerAsync();

希望这有帮助:)

这篇关于Window.Show() 不显示控件但 Window.ShowDialog() 会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆