StatusBar Text/Background 更新工作不可靠
StatusBar Text/Background updates don't work reliably
WPF 新手,试图弄清楚为什么我对 StatusBar 的样式更改无法可靠地工作。代码看起来很简单:
private void OnTryP4Login(object sender, RoutedEventArgs e)
{
statusText.Text = string.Format("Connecting to {0} as '{1}'.",
textBoxUri.Text, textBoxUser.Text); // <-- Doesn't work
statusBar.Background = Brushes.Yellow; // <-- Doesn't work
if (m_Manager.P4Login(textBoxUri.Text, textBoxUser.Text,
textBoxClientSpec.Text, passwordBox.Password))
{
statusText.Text = "SUCCESS!"; // <-- Doesn't work
statusBar.Background = Brushes.Green; // <-- Doesn't work
Thread.Sleep(2000);
this.Close();
}
else
{
statusBar.Background = Brushes.Red; // <-- THIS works
statusText.Text = "Error connecting to Perforce server!"; // <-- THIS works
buttonConnect.Visibility = Visibility.Collapsed;
buttonOK.Visibility = Visibility.Visible;
}
}
谁能告诉我错误的地方?
相关XAML:
<Window x:Class="WpfClient.P4LoginDialog"
[...]
Icon="feat.ico">
<DockPanel>
<Grid DockPanel.Dock="Top" Height="158">
[...]
<Button x:Name="buttonConnect" Content="Connect" Click="OnTryP4Login" HorizontalAlignment="Left" Margin="274,130,0,-18" VerticalAlignment="Top" Width="75"/>
</Grid>
<StatusBar x:Name="statusBar" Height="25" Background="#888" DockPanel.Dock="Bottom" HorizontalAlignment="Stretch" >
<TextBlock x:Name="statusText"
Width="Auto"
Height="Auto"
Foreground="#fff"
Text="Connect to P4 for live integration"
HorizontalAlignment="Right"
TextAlignment="Right"
/>
</StatusBar>
</DockPanel>
更新:感谢这些建议,我已经能够通过执行以下操作使黄色文本正常工作。绿色成功消息仍然不走运。我阅读了大量有关异步的资料,以充分理解执行流程和建议的全部含义(上次我做类似的事情时,BackgroundWorkers 风靡一时)。我感谢任何其他 hints/explanations.
private async void OnTryP4Login(object sender, RoutedEventArgs e)
{
statusText.Text = string.Format("Connecting to {0} as '{1}'.",
textBoxUri.Text, textBoxUser.Text); // <-- Works now
statusBar.Background = Brushes.DarkGoldenrod; // <-- Works now
Task<bool> getConnection = m_Manager.P4Login(textBoxUri.Text, textBoxUser.Text, textBoxClientSpec.Text, passwordBox.Password);
bool connection = await getConnection;
if (connection)
{
statusText.Text = "SUCCESS!"; // <-- Still doesn't work (stays DarkGoldenrod)
statusBar.Background = Brushes.Green; // <-- Still doesn't work (stays DarkGoldenrod)
Thread.Sleep(2000);
this.Close();
}
else
{
statusBar.Background = Brushes.Red; // <-- Still works
statusText.Text = "Error connecting to Perforce server!"; // <-- Still works
buttonConnect.Visibility = Visibility.Collapsed;
buttonOK.Visibility = Visibility.Visible;
}
}
public async Task<bool> P4Login(string uri, string user, string clientSpec, string password)
{
await Task.Delay(100);
return m_PI.Connect(uri, user, clientSpec, password);
}
我想整个代码 运行 都在 GUI 线程中并阻止 WPF 更新 GUI。
试试这个:
private async void OnTryP4Login(object sender, RoutedEventArgs e)
{
statusText.Text = string.Format("Connecting to {0} as '{1}'.",
textBoxUri.Text, textBoxUser.Text); // <-- Doesn't work
statusBar.Background = Brushes.Yellow; // <-- Doesn't work
await Task.Delay(100);
if (m_Manager.P4Login(textBoxUri.Text, textBoxUser.Text,
textBoxClientSpec.Text, passwordBox.Password))
{
statusText.Text = "SUCCESS!"; // <-- Doesn't work
statusBar.Background = Brushes.Green; // <-- Doesn't work
Thread.Sleep(2000);
this.Close();
}
else
{
statusBar.Background = Brushes.Red; // <-- THIS works
statusText.Text = "Error connecting to Perforce server!"; // <-- THIS works
buttonConnect.Visibility = Visibility.Collapsed;
buttonOK.Visibility = Visibility.Visible;
}
}
如果可行,您可能应该 运行 像这样的实际登录异步:
var loginSucceeded = await Task.Run(() => m_Manager.P4Login(textBoxUri.Text, textBoxUser.Text,
textBoxClientSpec.Text, passwordBox.Password));
而不是使用 Task.Delay(100)
。
在单线程应用程序中,在事件处理程序完成并且可以更新屏幕之前,对控件的更新不会产生影响。你的 Thread.Sleep(2000);
挡住了整个 UI.
创建方法 async
并将睡眠替换为 await Task.Delay( 2000 )
。这将允许正常的事件循环在这两秒内接管并且 UI 将得到更新。
编辑: 您的 "Connecting" 和黄色背景仍然无法正常工作,因为(我假设)m_Manager.P4Login()
是一个同步函数。如果您不能使其异步,请在调用它之前执行此操作:
await Dispatcher.Yield( DispatcherPriority.ApplicationIdle );
这让事件循环在您的代码继续之前短暂接管并处理所有未完成的事件(包括您的文本和背景颜色更新)。
WPF 新手,试图弄清楚为什么我对 StatusBar 的样式更改无法可靠地工作。代码看起来很简单:
private void OnTryP4Login(object sender, RoutedEventArgs e)
{
statusText.Text = string.Format("Connecting to {0} as '{1}'.",
textBoxUri.Text, textBoxUser.Text); // <-- Doesn't work
statusBar.Background = Brushes.Yellow; // <-- Doesn't work
if (m_Manager.P4Login(textBoxUri.Text, textBoxUser.Text,
textBoxClientSpec.Text, passwordBox.Password))
{
statusText.Text = "SUCCESS!"; // <-- Doesn't work
statusBar.Background = Brushes.Green; // <-- Doesn't work
Thread.Sleep(2000);
this.Close();
}
else
{
statusBar.Background = Brushes.Red; // <-- THIS works
statusText.Text = "Error connecting to Perforce server!"; // <-- THIS works
buttonConnect.Visibility = Visibility.Collapsed;
buttonOK.Visibility = Visibility.Visible;
}
}
谁能告诉我错误的地方?
相关XAML:
<Window x:Class="WpfClient.P4LoginDialog"
[...]
Icon="feat.ico">
<DockPanel>
<Grid DockPanel.Dock="Top" Height="158">
[...]
<Button x:Name="buttonConnect" Content="Connect" Click="OnTryP4Login" HorizontalAlignment="Left" Margin="274,130,0,-18" VerticalAlignment="Top" Width="75"/>
</Grid>
<StatusBar x:Name="statusBar" Height="25" Background="#888" DockPanel.Dock="Bottom" HorizontalAlignment="Stretch" >
<TextBlock x:Name="statusText"
Width="Auto"
Height="Auto"
Foreground="#fff"
Text="Connect to P4 for live integration"
HorizontalAlignment="Right"
TextAlignment="Right"
/>
</StatusBar>
</DockPanel>
更新:感谢这些建议,我已经能够通过执行以下操作使黄色文本正常工作。绿色成功消息仍然不走运。我阅读了大量有关异步的资料,以充分理解执行流程和建议的全部含义(上次我做类似的事情时,BackgroundWorkers 风靡一时)。我感谢任何其他 hints/explanations.
private async void OnTryP4Login(object sender, RoutedEventArgs e)
{
statusText.Text = string.Format("Connecting to {0} as '{1}'.",
textBoxUri.Text, textBoxUser.Text); // <-- Works now
statusBar.Background = Brushes.DarkGoldenrod; // <-- Works now
Task<bool> getConnection = m_Manager.P4Login(textBoxUri.Text, textBoxUser.Text, textBoxClientSpec.Text, passwordBox.Password);
bool connection = await getConnection;
if (connection)
{
statusText.Text = "SUCCESS!"; // <-- Still doesn't work (stays DarkGoldenrod)
statusBar.Background = Brushes.Green; // <-- Still doesn't work (stays DarkGoldenrod)
Thread.Sleep(2000);
this.Close();
}
else
{
statusBar.Background = Brushes.Red; // <-- Still works
statusText.Text = "Error connecting to Perforce server!"; // <-- Still works
buttonConnect.Visibility = Visibility.Collapsed;
buttonOK.Visibility = Visibility.Visible;
}
}
public async Task<bool> P4Login(string uri, string user, string clientSpec, string password)
{
await Task.Delay(100);
return m_PI.Connect(uri, user, clientSpec, password);
}
我想整个代码 运行 都在 GUI 线程中并阻止 WPF 更新 GUI。
试试这个:
private async void OnTryP4Login(object sender, RoutedEventArgs e)
{
statusText.Text = string.Format("Connecting to {0} as '{1}'.",
textBoxUri.Text, textBoxUser.Text); // <-- Doesn't work
statusBar.Background = Brushes.Yellow; // <-- Doesn't work
await Task.Delay(100);
if (m_Manager.P4Login(textBoxUri.Text, textBoxUser.Text,
textBoxClientSpec.Text, passwordBox.Password))
{
statusText.Text = "SUCCESS!"; // <-- Doesn't work
statusBar.Background = Brushes.Green; // <-- Doesn't work
Thread.Sleep(2000);
this.Close();
}
else
{
statusBar.Background = Brushes.Red; // <-- THIS works
statusText.Text = "Error connecting to Perforce server!"; // <-- THIS works
buttonConnect.Visibility = Visibility.Collapsed;
buttonOK.Visibility = Visibility.Visible;
}
}
如果可行,您可能应该 运行 像这样的实际登录异步:
var loginSucceeded = await Task.Run(() => m_Manager.P4Login(textBoxUri.Text, textBoxUser.Text,
textBoxClientSpec.Text, passwordBox.Password));
而不是使用 Task.Delay(100)
。
在单线程应用程序中,在事件处理程序完成并且可以更新屏幕之前,对控件的更新不会产生影响。你的 Thread.Sleep(2000);
挡住了整个 UI.
创建方法 async
并将睡眠替换为 await Task.Delay( 2000 )
。这将允许正常的事件循环在这两秒内接管并且 UI 将得到更新。
编辑: 您的 "Connecting" 和黄色背景仍然无法正常工作,因为(我假设)m_Manager.P4Login()
是一个同步函数。如果您不能使其异步,请在调用它之前执行此操作:
await Dispatcher.Yield( DispatcherPriority.ApplicationIdle );
这让事件循环在您的代码继续之前短暂接管并处理所有未完成的事件(包括您的文本和背景颜色更新)。