即使可见性设置为可见,控制也不可见

时间:2018-02-16 12:17:48

标签: c# .net wpf entity-framework linq

我正在创建一个窗口,其中点击保存与数据库通信并将数据保存在数据库中。我想在点击保存时显示进度图像,直到数据未保存在数据库中。我已将图像的可见性设置为true,但仍然无法看到进度图像。

我已经完成了以下代码...

在xml文件中..

                                           

    <TextBox x:Name="txt_Comment" VerticalScrollBarVisibility="Auto"  TextWrapping="Wrap" AcceptsReturn="True" MaxLength="5000"  Margin="2,2,2,0"></TextBox>
    <WrapPanel Grid.Row="1" HorizontalAlignment="Center" Margin="10,-15,0,0" VerticalAlignment="Center">
        <Button x:Name="btn_Ok" Margin="0,0,0,0" HorizontalAlignment="Center"  VerticalAlignment="Center" Click="btn_Save_Click"  Height="25" Width="55">Save</Button>
        <Button x:Name="btn_Cancel" Margin="10,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center"  Height="25" Width="55" Content="Cancel" Click="btn_Cancel_Click" >
        </Button>
    </WrapPanel>

    <Grid Grid.RowSpan="2" Background="Black" Opacity="0.25" Name="LoadingAdorner" Visibility="Hidden"/>
    <Border Width="400" Grid.RowSpan="2"  Opacity="1" Height="180" Visibility="Hidden" HorizontalAlignment="Center" VerticalAlignment="Center" CornerRadius="2" x:Name="loaderBorder">
        <Border.Background>
            <LinearGradientBrush  EndPoint="1,1" StartPoint="0,0" >
                <GradientStop Color="#EBF6FA" Offset="0.3"/>
                <GradientStop Color="#b7d9e5" Offset="1.0"/>
            </LinearGradientBrush>
        </Border.Background>
        <Border.Effect>
            <DropShadowEffect BlurRadius="5" Color="#FFB0B0B0" ShadowDepth="3" />
        </Border.Effect>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition/>
                <RowDefinition Height="95"/>
            </Grid.RowDefinitions>
            <Controls:LoadingAnimation Grid.Row="1" HorizontalAlignment="Center" FontWeight="Bold" LoadingText="Loading..."  VerticalAlignment="Center" />
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" FontFamily="Arial" Foreground="#647883" FontSize="14" Grid.Row="2" Text="Saving..."></TextBlock>
        </Grid>

    </Border>
</Grid>

在页面背后的代码中..

BackgroundWorker worker = new BackgroundWorker { WorkerSupportsCancellation = true };
worker.DoWork += delegate(object sender1, DoWorkEventArgs e1)
{
    CurrentDispatcher.Invoke(
        new Action(() =>
        {
            if (!string.IsNullOrEmpty(txt_Comment.Text))
            {
                LoadingAdorner.Visibility = Visibility.Visible;
                loaderBorder.Visibility = Visibility.Visible;
                using (Entities DB = new Entities(settings.LinqConnection))
                 {
                    if (txt_Comment.Text.Length > 1000)
                    {//In this case loading image is visible
                        MessageBox.Show("Comment is too large.", "Alert !", MessageBoxButton.OK, MessageBoxImage.Warning);
                        return;
                    }

                    var comment = DB.Table1.Create();//Here loading image is not visible.
                    comment.value = txt_Comment.Text;
                    comment.Date = DateTime.Now;
                    comment.ModifiedBy = settings.CurrentUID;
                    DB.Table1.Add(comment);

                    DB.SaveChanges();
                }
            }
        }),
    DispatcherPriority.Normal);
};

worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
{
    this.DialogResult = true;
    this.Close();
    LoadingAdorner.Visibility = Visibility.Collapsed;
    loaderBorder.Visibility = Visibility.Collapsed;
};

worker.RunWorkerAsync();

当消息框可见时,加载图像可见,但是当数据保存在数据库中时,加载图像不可见。

2 个答案:

答案 0 :(得分:0)

using块移出CurrentDispatcher.Invoke(),然后拨打此电话。

BackgroundWorker worker = new BackgroundWorker { WorkerSupportsCancellation = true };
        worker.DoWork += delegate(object sender1, DoWorkEventArgs e1)
        {
            CurrentDispatcher.Invoke(
                                 new Action(() =>
                                 {
                                     if (!string.IsNullOrEmpty(txt_Comment.Text))
                                     {
                                         LoadingAdorner.Visibility = Visibility.Visible;
                                         loaderBorder.Visibility = Visibility.Visible;                                         
                                     }
                                 }),
                                 DispatcherPriority.Normal);

                                 using (Entities DB = new Entities(settings.LinqConnection))
                                         {
                                             if (txt_Comment.Text.Length > 1000)
                                             {//In this case loading image is visible

                                                 MessageBox.Show("Comment is too large.", "Alert !", MessageBoxButton.OK, MessageBoxImage.Warning);
                                                 return;
                                             }

                                             var comment = DB.Table1.Create();//Here loading image is not visible.
                                             comment.value = txt_Comment.Text;
                                             comment.Date = DateTime.Now;
                                             comment.ModifiedBy = settings.CurrentUID;
                                             DB.Table1.Add(comment);

                                             DB.SaveChanges();
                                         }
        };

所以这段代码对我有用:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var worker = new BackgroundWorker { WorkerSupportsCancellation = true };
    worker.DoWork += delegate (object sender1, DoWorkEventArgs e1)
    {
        Dispatcher.Invoke(
                             new Action(() =>{testCtl.Visibility = Visibility.Visible;}),
                             System.Windows.Threading.DispatcherPriority.Normal);
        Task.Delay(3000).Wait();
    };
    worker.RunWorkerCompleted += delegate (object s, RunWorkerCompletedEventArgs args)
    {
        testCtl.Visibility = Visibility.Collapsed;
    };
    worker.RunWorkerAsync();
}

答案 1 :(得分:0)

这就是我在这种情况下的表现:

创建委托以更新您的可见性和相应的方法

将您的匿名函数包装在与doWork事件兼容的函数中

创建新委托,并在需要更新可见性时使用dispatcher.BeginInvoke()调用update方法。

    private delegate void UpdateVisibilityDelegate(Visibility visibility);

    private void UpdateVisiblity(Visibility visibility)
    {
        //LoadingAdorner.Visibility = visibility;
        //loaderBorder.Visibility = visibility;
    }

    private void Worker_DoWork(object sender, DoWorkEventArgs e)
    {
        // Your Action() code here
        // Change the visibility using this method
        Dispatcher.CurrentDispatcher.BeginInvoke(new UpdateVisibilityDelegate(this.UpdateVisiblity), Visibility.Visible);

    }