Quantcast
Channel: Caliburn.Micro: Xaml Made Easy
Viewing all articles
Browse latest Browse all 1760

New Post: Communication between popup and long running process

$
0
0
What I want to achieve is having a popup show up when I want to run a long running task in my application. The popup will show in a label the value sent from the task. The value is simulated using Thread.Sleep in the following example. My current code has two problems.

1) How to show a popup from a child view model? I get an error about not finding the view model. If I use ShowDialog instead of ShowPopup it seems to work.
2) How to pass information from the long running process to the popup?
public class ShellViewModel : Screen
    {
        private ChildViewModel _childViewModel = null;
        
        public ChildViewModel ChildViewModel
        {
            get
            {
                return _childViewModel;
            }
            set
            {
                _childViewModel = value;
                NotifyOfPropertyChange(() => ChildViewModel);
            }
        }
       
        [ImportingConstructor]
        public ShellViewModel()
        {
            ChildViewModel = new ChildViewModel();
            ChildViewModel.ConductWith(this);            
        }
    }
 [Export(typeof(ChildViewModel))]
    public class ChildViewModel : Screen
    {
        public IEnumerable<IResult> RunTask()
        {
            IWindowManager windowManager = IoC.Get<IWindowManager>();
            dynamic settings = new ExpandoObject();
            settings.Placement = PlacementMode.Center;
            settings.PlacementTarget = GetView(null);

            windowManager.ShowPopup(new ProcessingViewModel(), "Popup", settings);

            yield return new LongProcess();
        }
        
        [ImportingConstructor]
        public ChildViewModel()
        {

        }
    }
[Export(typeof(ProcessingViewModel))]
    public class ProcessingViewModel : PropertyChangedBase
    {
        private string statusMessage;
        public string StatusMessage
        {
            get { return statusMessage; }

            set
            {
                statusMessage = value;
                NotifyOfPropertyChange(() => StatusMessage);
            }
        }
    }
public class LongProcess : IResult
    {       
        public void Execute(ActionExecutionContext context)
        {            
            ThreadPool.QueueUserWorkItem(item =>
            {
                for (int i = 1; i < 10; i++)
                {
                    int milliseconds = i * 100;
                    Thread.Sleep(milliseconds);
                    System.Diagnostics.Debug.WriteLine(milliseconds);
                    // Send i value to popup 
                }
                
                Completed(this, new ResultCompletionEventArgs());
            });
        }

        public event EventHandler<ResultCompletionEventArgs> Completed;
    }

Viewing all articles
Browse latest Browse all 1760

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>