Kulpemovitz
Kulpemovitz

Reputation: 541

Update 2 elements in the UI

I have a WPF application. I'm using an auto hide pane to host a very complex usercontrol. When the user click the pane, it takes about 10 sec. to load the control, I'm trying to creating a "Loading ... " window to inform the user the app is working. In this waiting window I have an animation to rotate an image. My problem is after 2 days I still can't make the wheel rotate, because I'm using the same thread as for the loading of the usercontrol. My "Loading" windows is working just fine if I run it without the pane update. I tried several things - BackgroundWorker, Dispatcher.Invoke. All the examples I found in the net is about doing a background task while keeping the UI active.

Any lead??

Upvotes: 1

Views: 71

Answers (1)

user1246682
user1246682

Reputation: 206

Though I am not getting this question clearly but I tried to give this answer as better as I can get the question.:-)

In my scenario, I have created one main window, that loads the time consuming control on a button click. I have kept a sleep of 10 seconds while loading the time consuming control.

Main Window code:

Xaml -

<Window x:Class="Wpf.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Wpf"
    Title="MainWindow" Height="350" Width="525"
    x:Name="root">
<Window.Resources>
    <DataTemplate x:Key="loadingTemplate" DataType="ContentControl">
        <StackPanel>
            <TextBlock Text="Loading..." Margin="5"/>
        </StackPanel>
    </DataTemplate>

</Window.Resources>

<StackPanel x:Name="stpRootLayout">
    <Button Content="Click to load pane" Click="Button_Click"/>
    <Border BorderThickness="5" BorderBrush="Black"  Height="100">
    <ContentControl x:Name="cctPlaceHolder">

    </ContentControl>
    </Border>
</StackPanel>

Xaml.cs -

public delegate void LoadTimeConsumingControlDelegate();

public void LoadTimeConsumingControl()
    {
        //set our progress dialog text and value
        cctPlaceHolder.Content = new TimeConsumingControl();
    }

private void Button_Click(object sender, RoutedEventArgs e)
    {
        BackgroundWorker worker = new BackgroundWorker();
        cctPlaceHolder.ContentTemplate = this.TryFindResource("loadingTemplate") as DataTemplate;
        System.Windows.Threading.Dispatcher cctDispatcher = cctPlaceHolder.Dispatcher;
        worker.DoWork += delegate(object s, DoWorkEventArgs args)
        {
            LoadTimeConsumingControlDelegate update = new LoadTimeConsumingControlDelegate(LoadTimeConsumingControl);
            cctDispatcher.BeginInvoke(update);
        };

        worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
        {
            cctPlaceHolder.ContentTemplate = null;
        };

        worker.RunWorkerAsync();
    }

Time Consuming Control:

Xaml -

<UserControl x:Class="Wpf.TimeConsumingControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <TextBlock Text="some text..................................................................................................."/>
</Grid>

Xaml.cs

public TimeConsumingControl()
    {
        InitializeComponent();
        Thread.Sleep(10000);
    }

Replace the data template with your loading theme.

Hope this was the thing that you were looking for. If not, you can get back to me.

Upvotes: 1

Related Questions