DispatchHelper打开新窗口

时间:2014-08-18 01:17:47

标签: wpf mvvm mvvm-light

我正在使用MVVM Light中的DispatchHelper在执行计算时打开一个新窗口。该窗口包含一个简单的标签和按钮。当我使用Show()方法时,窗口将在计算开始时出现,并在计算结束时关闭,但标签和按钮不在窗口中。

我在计算开始之前尝试使用ShowDialog()方法,并在窗口上显示标签和按钮。但是,ShowDialog在窗口关闭之前不会返回。因此,窗口保持打开状态,并且不会执行计算。

有人会就如何解决这个问题提出一些建议吗?

public RelayCommand ShowPopupControl
{
    get
    {
        return _showPopupControl 
            ?? (_showPopupControl = new RelayCommand(
                () => {                        
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        var ww = new WorkingWindow();
                        ww.ShowDialog();
                        var loop = new LoopData();
                        loop.DoSomeLoops();
                        ww.Close();

                    });                                    
                }));
    }
}

public class LoopData
{     
    public void DoSomeLoops()
    {          
        for (int i = 0; i < 10000; i++)
        {
            System.Diagnostics.Debug.WriteLine(i);
        }

    }
}

<controls:MetroWindow
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Custom="http://metro.mahapps.com/winfx/xaml/controls" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    x:Class="MetroSpinnerPractice.WorkingWindow"
  xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    Title="WorkingWindow" Height="200" Width="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>  

    <StackPanel HorizontalAlignment="Center" Height="75" Margin="5" Grid.Row="0" VerticalAlignment="Center" Width="275" Orientation="Horizontal">       

        <Label Content="Working...." 
                VerticalAlignment="Center" 
                Margin="60,20,0,20" 
                FontSize="21.333" 
                HorizontalAlignment="Right" 
                Width="120" 
                Height="40"/>
    </StackPanel>

    <Button x:Name="btnCancelWork" Grid.Row="1"
            Content="Cancel" 
            Width="100" 
            Height="20" 
            HorizontalAlignment="Center" 
            VerticalAlignment="Center" 
            Margin="0" 
            ToolTip="cancel current routine"/>

</Grid>

0 个答案:

没有答案