简体   繁体   中英

WPF Popup and UI Thread

Some background...
I am trying to give visual feedback when a button is clicked...basically a loading popup message (using a wpf popup control). The problem is...because I am on the UI thread and in synchronous code the button click and logic happens then the popup shows up. I wanted the popup to show up on the button click, run through the some code, then go away (the popup should show up while the code is running).

I realize I can get it done with threading, but thats not an option right now. I have time constaints and don't have time to fix the app "right way".


  • I didn't write the app, just tasked with trying to improve performance (and/or perception of performance) without a full over-haul.
  • At some point I am hoping that I can re-tool this app, only little tweaks are going to cut it for now.
  • There is no ViewModel...code is really, really coupled to the UI. (Like I said, I didn't right the app)
  • For various reasons I can't use threading in this scenario, not without breaking too many other peices in the app and my time constraints.

So is there any way to accomplish this in this scenario (btw...there no ViewModel and I can't create one right now becuase of the tight Logic-UI coupling)?



Pop up control

<Popup Name="popLoading" Placement="Center">
            <Border CornerRadius="10" Background="#ffffff">
                <Grid Width="400" Height="200">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Image Grid.Column="0" Name="imgLoading" Source="/foo/Images/loading.png"  />
                    <TextBlock Name="txtLoading" Text="Retreiving data..."/>
                </Grid>
            </Border>
        </Popup>          

Button Click

private void MyButton_Click(object sender, RoutedEventArgs e)
    {
        popLoading.IsOpen = true;

        //Do Some Stuff hopefully popup is showing up on the screen. 

        popLoading.IsOpen = false;

    }

Is it really onerous to create a background worker for whatever long-running process is hosing the UI?

Another UI trick I use in WinForms is having a StatusBar at the bottom that would say when something is loading (back in the days when I would do exactly what this program is doing wrong)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM