Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Mvvm light messenger初始化_C#_Wpf_Xaml_Mvvm - Fatal编程技术网

C# Mvvm light messenger初始化

C# Mvvm light messenger初始化,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我正在使用mvvm light messenger制作一些具有许多不同视图的项目。 所有视图都相互通信。简而言之,我有切换视图的内容控制(SomeInfoCarsView、SomeInfoBicyclesView)。这两个视图(SomeInfoCarsView、SomeInfoBicyclesView)都包含“BuyPanelView”。 切换(SomeInfoBicyclesView)后,向“BuyPanelView”发送消息 消息不会出现在“BuyPanel”中,因为它们的初始化速度不同。但

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

消息不会出现在“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>
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] });
    }

}
public类someinfobicles:ViewModelBase
{
/// 
///初始化SomeInfoBicycles类的新实例。
/// 
/// 
公共ObservableCollection项{get;set;}
公共自行车
{
ItemsBicycles=新的ObservableCollection();
添加(“第一辆自行车”);
添加(“第二辆自行车”);
添加(“第三辆自行车”);
SendData();
//动作延时=新动作(保持);
//IAsyncResult result=TimeDelay.BeginInvoke(null,null);
}
公开无效持有()
{
系统线程线程睡眠(3000);
SendData();
}
public void SendData()
{
Send(newmessagecommunicator{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:ViewModelBase
{
/// 
///初始化BuyPanel类的新实例。
/// 
/// 
私有字符串输入数据;
公共字符串输入数据
{
获取{return inputData;}
设置{inputData=value;RaisePropertyChanged(“inputData”);}
}
公共事务委员会()
{
Messenger.Default.Register(this,(emp)=>{InputData=emp.Data;});
Debug.WriteLine(输入数据);
}
}
BuyPanel.视图:

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

购买
 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);
    }
}
    <StackPanel>
    <Label Content="{Binding InputData}"></Label>
    <Button>Buy</Button>
</StackPanel>