Window.Show()不显示控件,但Window.ShowDialog()可以

时间:2014-11-25 18:40:38

标签: c# wpf windows xaml

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

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

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

这产生:
Window.Show dialog created

以下代码......

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

产地:
Window.ShowDialog dialog created

窗口代码:

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:

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


    }
}

1 个答案:

答案 0 :(得分:2)

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

使用后台工作程序执行//执行操作。

尝试使用以下代码:

&#13;
&#13;
            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();
&#13;
&#13;
&#13;

希望这有助于:)