Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Wpf MVVM model development
1. UI a ProgressControl.xaml, for the pop-up progress bar window file, the following major elements:
XML
<StackPanel>

        <TextBlock  Text="{Binding Caption}"  Style="{StaticResource CaptionText}"/>
        <Border Style="{StaticResource ContentBorder}">
            <StackPanel>
                <StackPanel Style="{StaticResource ContentMessageBorder}">
                    <TextBlock   Text="{Binding Message}" Style="{StaticResource ContentMessage}" />
                    <telerik:RadProgressBar  Minimum="0" Maximum="100" Margin="10,10,10,0"
                     Width="260" Height="25"  Value="{Binding Value}" Visibility="Visible" />
                </StackPanel>
                <StackPanel  Style="{StaticResource ButtonContainer}">
                    <telerik:RadButton Command="{Binding CancelCommand}" Content="{Resx CancelBtnText}" IsCancel="True"  />
                </StackPanel>
            </StackPanel>
        </Border>
    </StackPanel>


Corresponding VM attributes:

CSS
bool IsHidden { set; }
string Caption { get; set; }
bool IsVisible { get; set; }
string Message { get; set; }
int Value { get; set; }
ICommand CancelCommand { get; }


2. I export images and pop-up progress bar in another VM, the main code is as follows:

C#
_progressVM.IsVisible = true;
 _progressVM.Caption = "Exporting";

if (modeIndex == "1")
                    {
                        var navImages = NavBarSelectedDocument.DocumentImagesUC_VM.Images.Where(x => x.IsDefaultImage == false).ToList();
                        if (!isExportSplitPage)
                        {
                            navImages = NavBarSelectedDocument.DocumentImagesUC_VM.Images.Where(x => x.IsDefaultImage == false && x.IsSplitPage == false).ToList();
                        }
                        int i = 0;
                        int count = navImages.Count;
                        foreach (var item in navImages)
                        {
                            i++;
                            _progressVM.Message = item.LinkedDocument.Name + "--" + item.Image.Name;
                            _progressVM.Value = ((i * 100) / count);
                            _project.ExportDocument(item.ImagePath, exportPath, item.LinkedDocument.Name, item.Name);
                        }
                    }

_progressVM.IsVisible = false;


The code above can be a good progress bar window pops up, but I now want to cancel the function, that is, there is a Cancel button in the pop-up window, click Cancel, the pop-up window is closed, and stop the export picture. I set up in progress Cancel button bindings command IsVisible = false, but does not work, Editor's Note ah? What do I have to do in order to achieve this functionality, pursuing big God help, thank you very much
Posted

Try to use Thread to execute an abort the running export
C#
private System.Threading.Thread thread = new System.Threading.Thread(Export);
private void Export(){
_progressVM.IsVisible = true;
 _progressVM.Caption = "Exporting";
 
if (modeIndex == "1")
                    {
                        var navImages = NavBarSelectedDocument.DocumentImagesUC_VM.Images.Where(x => x.IsDefaultImage == false).ToList();
                        if (!isExportSplitPage)
                        {
                            navImages = NavBarSelectedDocument.DocumentImagesUC_VM.Images.Where(x => x.IsDefaultImage == false && x.IsSplitPage == false).ToList();
                        }
                        int i = 0;
                        int count = navImages.Count;
                        foreach (var item in navImages)
                        {
                            i++;
                            _progressVM.Message = item.LinkedDocument.Name + "--" + item.Image.Name;
                            _progressVM.Value = ((i * 100) / count);
                            _project.ExportDocument(item.ImagePath, exportPath, item.LinkedDocument.Name, item.Name);
                        }
                    }
 
_progressVM.IsVisible = false;
}

On click Export button, please call:
C#
thread.Start();

And on Cancel button click, call:
C#
if (thread.IsAlive || thread.IsBackground)
{
   thread.Abort();
}
 
Share this answer
 
Hi jerrykid,thanks your answer,but how to hide the pup-window of progress window?

I use the MVVM!!!
 
Share this answer
 
v3
To hide the pup-window of progress window
On Cancel button click, call:

SQL
if (thread.IsAlive || thread.IsBackground)
{
   thread.Abort();
   // Put your code here to hide popup window  
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900