Mvvm light messenger初始化

时间:2015-07-22 13:25:25

标签: c# wpf xaml mvvm

我正在使用mvvm light messenger创建一些具有许多不同视图的项目。 所有视图都相互通信。简而言之,我有内容控制切换视图(SomeInfoCarsView,SomeInfoBicyclesView)。这两个视图(SomeInfoCarsView,SomeInfoBicyclesView)都包含“BuyPanelView”。 切换(SomeInfoBicyclesView)后发送消息到“BuyPanelView”。

        SendData();
        //Action TimeDelay = new Action(Hold);
        //IAsyncResult result =TimeDelay.BeginInvoke(null,null);

消息没有出现在“BuyPanel”中,因为他们的初始化速度不同。但是如果消息发送有一些时间延迟,它的工作正常。 1)在另一个视图中使用一个视图是否正常? 2)在这种情况下如何毫不拖延地发送消息?

MainWindow.xaml:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Button Content="DisplayCars" HorizontalAlignment="Left" Margin="20,10,0,0" VerticalAlignment="Top" Width="75" Command="{Binding DisplayCars}"/>
    <Button Content="DisplayBicycles" HorizontalAlignment="Left" Margin="20,35,0,0" VerticalAlignment="Top" Width="75" Command="{Binding DisplayBicycles}"/>
    <ContentControl Grid.Column="1" Content="{Binding CurrentViewModel}" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch"/>
</Grid>

MainWindows.cs

namespace WpfApplication95.ViewModel
{
public class MainViewModel : ViewModelBase
{
    readonly static Cars cars = new Cars();
    readonly static Bicycles bicycles = new Bicycles();

    private ViewModelBase currentViewModel ;
    public ViewModelBase CurrentViewModel
    {
        get
        {
            return currentViewModel;
        }
        set
        {
            if (currentViewModel == value)
                return;
            currentViewModel = value;
            RaisePropertyChanged("CurrentViewModel");
        }
    }
            public ICommand DisplayCars { get;set; }
            public ICommand DisplayBicycles { get; set; }
    public MainViewModel()
    {
        CurrentViewModel = MainViewModel.cars;
        DisplayCars = new RelayCommand(() => SetCars());
        DisplayBicycles = new RelayCommand(()=>SetBicycles());
    }
    public void SetCars()
    {
        CurrentViewModel = MainViewModel.cars;
    }
    public void SetBicycles()
    {
        CurrentViewModel = MainViewModel.bicycles;
    }
}
}

的App.xaml:

    <DataTemplate DataType="{x:Type vm:Cars}">
        <views:SomeInfoCarsView />
    </DataTemplate>
    <DataTemplate DataType="{x:Type vm:Bicycles}">
        <views:SomeInfoBicyclesView />
    </DataTemplate>

SomeInfoBicycles.xaml:

<Grid>
    <StackPanel>
        <ListView ItemsSource="{Binding ItemsBicycles}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Label Content="{Binding .}"></Label>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <views:BuyPanelView></views:BuyPanelView>
    </StackPanel>
</Grid>

SomeInfoBicycle.cs:

 public class SomeInfoBicycles : ViewModelBase
{
    /// <summary>
    /// Initializes a new instance of the SomeInfoBicycles class.
    /// </summary>
    /// 
    public ObservableCollection<string> ItemsBicycles { get; set; }
    public SomeInfoBicycles()
    {
        ItemsBicycles = new ObservableCollection<string>();
        ItemsBicycles.Add("First Bicycle");
        ItemsBicycles.Add("Second Bicycle");
        ItemsBicycles.Add("Third Bicycle");
        SendData();
        //Action TimeDelay = new Action(Hold);
        //IAsyncResult result =TimeDelay.BeginInvoke(null,null);
    }
    public void Hold()
    {
        System.Threading.Thread.Sleep(3000);
        SendData();
    }
    public void SendData()
    {
        Messenger.Default.Send<MessageCommunicator>(new MessageCommunicator { Data = ItemsBicycles[1] });
    }

}

BuyPanel.cs:

 public class BuyPanel : ViewModelBase
{
    /// <summary>
    /// Initializes a new instance of the BuyPanel class.
    /// </summary>
    /// 
    private string inputData;
    public string InputData 
    {
        get { return inputData; }
        set { inputData = value; RaisePropertyChanged("InputData"); }
    }
    public BuyPanel()
    {
        Messenger.Default.Register<MessageCommunicator>(this, (emp) => { InputData = emp.Data; });
        Debug.WriteLine(InputData);
    }
}

BuyPanel.View:

    <StackPanel>
    <Label Content="{Binding InputData}"></Label>
    <Button>Buy</Button>
</StackPanel>

0 个答案:

没有答案