【问题标题】:Window.Show() doesn't display controls but Window.ShowDialog() doesWindow.Show() 不显示控件,但 Window.ShowDialog() 显示
【发布时间】:2014-11-25 18:40:38
【问题描述】:

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

以下代码显示了使用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:

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


    }
}

【问题讨论】:

  • 它与您的// do stuff 有什么关系吗,因为ShowDialog 会阻塞它正在运行的线程,直到窗口关闭,而对于Show,您继续并且可能会很快完成一些任务(这否则不会运行 ShowDialog) 更改您显示内容的方式。
  • 如果您在do stuff 中使用Connect to a device,您可能阻塞了UI 线程。我认为你必须看看你是如何连接的。
  • @Sjips @Vlad 我在ShowShowDialog 上设置了一个断点,那是我截屏的时候,所以它还没有执行我正在做的“东西”。实际上,我也曾一度将这些东西注释掉
  • @mdw7326 如果您使用的是Show,则屏幕不会完全呈现,直到包含prg.Show(); 的函数返回到消息泵(很可能从调用您当前所在的功能)。
  • prg.Show() 的目的是什么; ; //做一些事情 prg.Close(); ?它将关闭()

标签: c# wpf windows xaml


【解决方案1】:

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

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

尝试使用以下代码:

            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();

希望这会有所帮助:)

【讨论】:

  • 我正在尝试等到进程完成,但使用后台工作人员只会在后台执行进程,不是吗?
  • 好的,我让它工作了......一点点。我目前正在重组我的代码以更好地适应异步工作者。完成后将发布回复。
猜你喜欢
  • 1970-01-01
  • 2011-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
相关资源
最近更新 更多