【发布时间】: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 我在
Show和ShowDialog上设置了一个断点,那是我截屏的时候,所以它还没有执行我正在做的“东西”。实际上,我也曾一度将这些东西注释掉 -
@mdw7326 如果您使用的是
Show,则屏幕不会完全呈现,直到包含prg.Show();的函数返回到消息泵(很可能从调用您当前所在的功能)。 -
prg.Show() 的目的是什么; ; //做一些事情 prg.Close(); ?它将关闭()