WPF mvvm messenger析构函数

时间:2015-08-19 14:55:03

标签: c# wpf xaml mvvm

我有MainWindow它在MainViewModel中持有<ContentControl ... Content="{Binding CurrentViewModel}"我在两个视图FirstViewSecondView之间切换。 FirstView包含用于实现向ContentFirstView发送异步数据的usercontrol ContentSecondViewModel。数据发送时间延迟1000毫秒。主要问题是为什么当我点击1,2,1,2,1,2按钮时,Count中更新标签ContentSecondView的速度会大大增加?我认为ContentSecondViewModel没有处理,每当我点击按钮2时,它会创建一个Messenger.gister的新对象......

MainModel.xaml:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <ContentControl Grid.Column="1" Content="{Binding CurrentViewModel}" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch"/>
    <Button Content="1" Command="{Binding FirstCommand}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75"/>
    <Button Content="2" Command="{Binding SecondCommand}" HorizontalAlignment="Left" Margin="10,78,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>

MainViewModel.cs:

public class MainViewModel : ViewModelBase
{
    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    /// 

    public RelayCommand FirstCommand
    {
        get
        {
            return new RelayCommand(() => SwitchFirst());
        }
    }

    public RelayCommand SecondCommand
    {
        get
        {
            return new RelayCommand(()=>SwitchSecond());
        }
    }

    public static readonly FirstViewModel firstViewModel = new FirstViewModel();
    public static readonly SecondViewModel secondViewModel = new SecondViewModel();

    ViewModelBase currentViewModel;
    public ViewModelBase CurrentViewModel
    {
        get
        {
            return currentViewModel;
        }
        set
        {
            if (currentViewModel == value)
                return;
            currentViewModel = value;
            RaisePropertyChanged("CurrentViewModel");
        }
    }
    public void SwitchFirst()
    {
        CurrentViewModel = MainViewModel.firstViewModel;
    }
    public void SwitchSecond()
    {
        CurrentViewModel = MainViewModel.secondViewModel;
    }
    public MainViewModel()
    {
        CurrentViewModel = MainViewModel.secondViewModel;
        ////if (IsInDesignMode)
        ////{
        ////    // Code runs in Blend --> create design time data.
        ////}
        ////else
        ////{
        ////    // Code runs "for real"
        ////}
    }
}

SecondView.xaml:

  <Grid>
    <views:ContentSecondView></views:ContentSecondView>
</Grid>

FirstView.xaml:

    <Grid>
    <views:ContentFirstView></views:ContentFirstView>
</Grid>

ContentFirstView.xaml:

<Grid>
    <Label>View That sending data</Label>
</Grid>

ContentSecondView.xaml:

    <Grid>
    <Label>View That sending data</Label>
</Grid>

ContentFirstViewModel.cs:

public class ContentFirstViewModel
{
    public RelayCommand SendMessage
    {
        get
        {
            return new RelayCommand(() => Send());
        }
    }
    public void Send()
    {
        Messenger.Default.Send<MessageCommuniactor>(new MessageCommuniactor {State=1 });
    }
      public void Increase()
    {
        while(true)
        {
            System.Threading.Thread.Sleep(1000);
            Messenger.Default.Send<MessageCommuniactor>(new MessageCommuniactor { State = 1 });
        }

    }
      public ContentFirstViewModel()
    {
        Action A = new Action(Increase);
        IAsyncResult result = A.BeginInvoke(null, null);
    }
}

ContentSecondViewModel.cs:

public class ContentSecondViewModel:ViewModelBase
{
    private int count;

    public int Count
    {
        get
        {
            return this.count;
        }

        set
        {
            this.count = value;
            this.RaisePropertyChanged("Count");
        }
    }

    public ContentSecondViewModel()
    {
        Messenger.Default.Register<MessageCommuniactor>(this, (key) => 
        { 
            Count += key.State; 
        });
    }
}

1 个答案:

答案 0 :(得分:0)

每次ContentFirstViewModel存在时,DataTemplate都会确保创建新视图。相反,采用单例方法,在需要时注入单例视图。您可以使用Prism的区域管理器或简单绑定。