加载时WPF更新UI

时间:2015-01-29 04:11:02

标签: c# wpf backgroundworker threadpool dispatcher

我想在加载一些长时间的工作时更新UI。我想要的是当数据加载时我想显示UserControl这只是一个等待消息。我尝试了BackgroundWorkerDispatcher.BeginInvokeThreadPool.QueueUserWorkItem,但我不知道为什么它没有按照预期进行。请帮忙

Dispatcher.BeginInvoke(new Action(() => img.Visibility = Visibility.Visible));
//Load some long wok here           
Dispatcher.BeginInvoke(new Action(() => img.Visibility = Visibility.Collapsed), DispatcherPriority.Background);

我要找的是在加载时显示图像,加载完成后必须折叠。

修改:

<Grid Grid.Column="2"
      Name="DetailsPane">
    <Grid.Background>
        <ImageBrush ImageSource="img/back.jpg"
                    Stretch="UniformToFill" />
    </Grid.Background>
    <Image Name="img"
           Margin="5"
           Stretch="None"
           Visibility="Collapsed" 
           Source="img/Loading.png" />
</Grid>

我在Listbox SelectionChanged上进行加载时会显示一个图像。因此,在SelectionChanged事件中,我将在ListBox上的Cliked特定事物的Grid Called DetailsPane中加载详细信息。在加载信息时我应该如何在DetailsPane 上显示此图片?


到达此处

创建一个名为State of bool的依赖属性,然后

.CS

private void LstAllFeatures_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    State = false;
    // Loading Heavy Work 
     State = true;
}

XAML

<Image Name="img"
       Margin="5"
       Stretch="None">
    <Image.Style>
        <Style TargetType="Image">
            <Setter Property="Visibility"
                    Value="Collapsed" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding State}"
                             Value="False">
                    <Setter Property="Visibility"
                            Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>

DeatilsPane.Children - &gt; _visualChildren - &gt; InternalArray[0]是一张图片。打开时,在WPF Visualizer中显示图像可见性=可见,但它没有显示。请点赞。

2 个答案:

答案 0 :(得分:2)

第一个userControl必须实现INotifyPropertyChanged,如:

public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string      propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

然后国家必须定义如下:

public bool _state;
public bool State
    {
        get { return _state; }
        set
        {
            if (value != _state)
            {
                _state= value;
                RaisePropertyChanged();
            }
        }
    }

然后在代码中你想要调用LongWork()

State = true;
await Task.Run(()=>LongWork());
State = fasle;

然后在xaml中你写得像:

<Image Margin="5"
   Stretch="None">
<Image.Style>
    <Style TargetType="Image">
        <Setter Property="Visibility"
                Value="Collapsed" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding State,RelativeSource=   {RelativeSource AncestorType={x:Type test:UserControlName}},Mode=TwoWay}"
                         Value="False">
                <Setter Property="Visibility"
                        Value="Visible" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
   </Image.Style>
</Image>

答案 1 :(得分:0)

添加bool属性:

public bool IsLoading
{
    get { return isLoading; }
    set { isLoading = value; NotifyPropertyChanged("IsLoading"); }
}

数据将其绑定到Image.Visibility属性:

<Image Margin="5" Stretch="None" Source="img/Loading.png"> 
    <Image .Style>
        <Style>
            <Setter Property="Control.Visibility" Value="Collapsed" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsLoading}" Value="True">
                    <Setter Property="Control.Visibility" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>

在后台线程上启动长时间运行的任务时将其设置为true

IsLoading = true;

在长时间运行的过程完成后将其设置为false

IsLoading = false;

请注意,这仅适用于在后台线程上运行长时间运行的进程,否则,UI线程将忙,不会更新。