wpf - 使用 MVVM-Light 的消息传递问题

标签 wpf mvvm .net-4.0 mvvm-light

最初我想在两个 VM 之间传递一个“ObservableCollection”,但即使是简单的消息传递也不适合我。

主视图型号

 private void openNewSpecialCustomer()
    {
        GalaSoft.MvvmLight.Messaging.Messenger.Default.Send("Musaab");
        Console.WriteLine("send done");
        AddNewSpecialCustomer a = new AddNewSpecialCustomer();
        _dialogService.showDialoge(a);
    }

AddNewSpecialCustomerViewModel
public AddNewSpecialCustomerViewModel()
    {
        GalaSoft.MvvmLight.Messaging.Messenger.Default.Register<string>(this,  doSomething);
        Console.WriteLine("Should now Receive");
        validProperties = new Dictionary<string, bool>();
        validProperties.Add("specialCustomerName",false);
        validProperties.Add("tel", false);
        allPropertiesValid = false;

    }

    public void doSomething(string s)
    {
        Console.WriteLine("Should be received");
        specialCustomerName = s;
        Console.WriteLine("s value " + s);
    }

    public String specialCustomerName
    {
        get { return _specialCustomerName; }
        set
        {
            if (_specialCustomerName != value)
            {
                _specialCustomerName = value;
               OnPropertyChanged("specialCustomerName");

            }
        }
    }

现在 XAML 用于 AddNewSpecialCustomer
<Window FlowDirection="RightToLeft" x:Class="GlassStore.AddNewSpecialCustomer"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:local="clr-namespace:GlassStore.ViewModels"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AddNewSpecialCustomer" Height="318" Width="458">

<Window.DataContext>
    <local:AddNewSpecialCustomerViewModel/>
</Window.DataContext>
<Grid Background="{DynamicResource NormalBrush}">
    <Button Command="{Binding Save}" Content="موافق" Height="29" HorizontalAlignment="Left" Margin="31,218,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
    <Label Content="إسم العميل" Height="27" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label1" VerticalAlignment="Top" Width="120" />
    <TextBox Text="{Binding specialCustomerName,Mode=TwoWay,ValidatesOnDataErrors=True,ValidatesOnExceptions=True,UpdateSourceTrigger=PropertyChanged}" Height="27" HorizontalAlignment="Left" Margin="155,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="210" />
    <Label Content="المنطقة/المكان" Height="27" HorizontalAlignment="Left" Margin="12,67,0,0" Name="label2" VerticalAlignment="Top" Width="120" />
    <TextBox Text="{Binding region}" Height="27" HorizontalAlignment="Left" Margin="155,67,0,0" Name="textBox2" VerticalAlignment="Top" Width="210" />
    <TextBox Text="{Binding tel,ValidatesOnDataErrors=True,ValidatesOnExceptions=True,UpdateSourceTrigger=PropertyChanged}" Height="27" HorizontalAlignment="Left" Margin="155,119,0,0" Name="textBox3" VerticalAlignment="Top" Width="210" />
    <Label Content="رقم الهاتف " Height="27" HorizontalAlignment="Left" Margin="12,119,0,0" Name="label3" VerticalAlignment="Top" Width="120" />
    <Button Content="إلغاء" Height="29" HorizontalAlignment="Left" Margin="143,218,0,0" Name="button2" VerticalAlignment="Top" Width="75" />
    <Label Content="" Height="29" HorizontalAlignment="Left" Margin="12,177,0,0" Name="label4" VerticalAlignment="Top" Width="412" />
</Grid>

您可以看到有一个 textBox,其 Text 属性绑定(bind)到 specialCustomerName 属性,我正在尝试通过 messenger 更改它,数据绑定(bind)模式是 TwoWay,所以我希望我的 textBox 在加载时有我的名字,(我通过信使发送我的名字)事实并非如此,我希望这可能是更清晰的代码

提前致谢

最佳答案

您的第二个窗口构造函数是在第一个窗口构造函数之后调用的吗?
您应该先注册,然后才能发送消息。没有收到以前发送的消息。很抱歉成为 C.O.但这种错误很有可能:)

关于wpf - 使用 MVVM-Light 的消息传递问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6479860/

相关文章:

.net - 如何右键单击以选择 TreeView 控件中的节点

wpf - 如何从源代码构建和使用自定义 WPF 控件

wpf - MVVM 模型责任

c# - 使用 MVVM 在 WPF 中绑定(bind)命令

wpf - 依赖属性如何保持值(value)

c# - ToolStripTextBox 延迟 TextChanged

wpf - 您如何知道您的 WPF 控件是否正在呈现?

c# - 设置背景颜色或 WPF (4.0) ListBox - Windows 8

c# - 更改主窗口名称后出错

android - 如何在 ViewModel 中从图库中选择照片