使用ProgressRing

时间:2015-05-29 04:00:07

标签: c# wpf xaml

HI!我有一个问题

<Button Margin="5"
        Visibility="{Binding IsVisibleProgressRing, Converter={StaticResource InvertBoolToVisibilityConverter}, Mode=TwoWay}">
        Content="Search"
</Button>

<mahApps:ProgressRing Margin="5"
                      Height="32" Width="32"
                      DockPanel.Dock="Right"
                      IsActive="{Binding IsActiveProgressRing, Mode=TwoWay}" 
                      Visibility="{Binding IsVisibleProgressRing, Mode=TwoWay}"/>

我也是

public class InvertBoolToVisibilityConverter : IValueConverter
{
    private readonly BooleanToVisibilityConverter _converter = new BooleanToVisibilityConverter();

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var result = _converter.Convert(value, targetType, parameter, culture) as Visibility?;
        return result == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var result = _converter.ConvertBack(value, targetType, parameter, culture) as bool?;
        return result != true;
    }
}

在VM中

private bool _isActiveProgressRing;

private Visibility _isVisibleProgressRing = Visibility.Collapsed;

IsActiveProgressRing = true;

IsVisibleProgressRing = Visibility.Visible;

.....................
.....................
<!-- SEARCH IN DB -->
.....................
.....................

IsVisibleProgressRing = Visibility.Collapsed;

IsActiveProgressRing = false;

我需要的是,当我在数据库中查找时,搜索按钮被隐藏并且&#34; ProgressRing&#34;可见且活跃。搜索结束后,一切都恢复正常。 问题在于&#34; progressRing&#34;出现在数据库和按钮搜索的末尾,永远不会消失。

1 个答案:

答案 0 :(得分:1)

如果您正在运行数据库搜索关闭UI线程(换句话说,由于按钮单击或其他GUI控件事件而调用该方法),它将使GUI无响应并且在UI线程时无法正确更新被数据库搜索占用。

数据库搜索应该移动到像Dennis建议的后台线程,如果需要,后台线程可以使用这种结构更新GUI,以指示GUI线程处理所需的GUI更改(后台线程应该永远不要尝试直接更新GUI:

this.Invoke((MethodInvoker)delegate {
    // this next line will run on the UI thread regardless of which thread called .Invoke()
    someLabel.Text = numDbResults.ToString() + " database matches found";
});