【问题标题】:Cannot seem to communicate between views似乎无法在视图之间进行通信
【发布时间】:2010-05-17 12:29:34
【问题描述】:

我已经编写了一个 MVVM 原型作为学习练习,我正在努力理解如何在视图之间进行通信。让我解释一下

我在左侧有一个树视图(leftSideView)

右侧的ListView(rightSideView)

MainWindow(包括上面提到的 2 个视图和一个拆分器)

我实施的方法不起作用,如果您能指出我哪里出错或者是否有更好的方法,我希望您能指出。 你可以从这里下载快速原型

http://cid-9db5ae91a2948485.skydrive.live.com/self.aspx/.Public/WpfCommunicationBetweenViews.zip

我肯定在 bindind 上做错了什么。 也很高兴了解你是如何做到的。例如 左侧的 ListBox(一个视图)右侧的 ListView(另一个视图)您如何在两者之间进行通信。

非常感谢您的任何建议

【问题讨论】:

  • 你能用适当的语言标记这个吗?另外,如果可能的话,你能不能只发布相关的sn-p,它会更容易快速浏览。
  • 感谢您的回复,我不知道要放多少sn-p。对不起。我开始放它们,帖子太长了。我会标记它。我是 MVVM 并且认为合适。

标签: mvvm


【解决方案1】:

我查看了您的代码,进行了如下所示的修改,并且成功了。我将右侧视图更改为只有一个文本块以简化它。

MainWindow.xaml.cs(为两个要绑定的视图创建一个视图模型)

public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public static ProtoViewModel MainViewModel = new ProtoViewModel(Repository.GetContinents());
}

LeftSideView.xaml.cs(将此视图的数据上下文设置为视图模型,并在更改时更新视图模型的选定城市)

public partial class LeftSideView
{
    public LeftSideView()
    {
        InitializeComponent();

        this.DataContext = MainWindow.MainViewModel;
    }

    /// <summary>
    /// Update the selected city of the view model
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void OnTreeSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        (this.DataContext as ProtoViewModel).SelectedCity = e.NewValue as CityViewModel;
    }
}

RightSideView.xaml.cs(设置右侧视图使用相同的视图模型)

public partial class RightSideView
{
    public RightSideView()
    {
        InitializeComponent();

        this.DataContext = MainWindow.MainViewModel;
    }
}

在 RightSideView.xaml 中,我只是放置了如下所示的文本框:

<TextBlock Text="{Binding SelectedCity.Details.City.Name}"/>

When a city on the left view is selected, it will changed the selected city on the view model, therefore, it will updated the selected city name on the right view.

ProtoViewModel 类如下所示:

public class ProtoViewModel : Core.ViewModelBase
{
    public ProtoViewModel(IEnumerable<ContinentInfo> continents)
    {
        Continents =
            new ReadOnlyCollection<ContinentViewModel>(
                (from continent in continents
                 select new ContinentViewModel(continent)).ToList());
    }

    public ViewModels.CityViewModel SelectedCity
    {
        get { return selectedCity; }
        set
        {
            if(selectedCity != value)
            {
                selectedCity = value;
                OnPropertyChanged("SelectedCity");
            }
        }
    }
    private ViewModels.CityViewModel selectedCity;

    public ReadOnlyCollection<ContinentViewModel> Continents
    {
        get { return continents; }
        set
        {
            if (continents != value)
            {
                continents = value;
                OnPropertyChanged("Continents");
            }
        }
    }
    private ReadOnlyCollection<ContinentViewModel> continents;
}

我会与你分享修改后的文件,但我不知道该怎么做:)

【讨论】:

  • 哇。让我消化一下,然后回来。如果你想给我发电子邮件 dotnet@devnet247.com,你可以,但如果有麻烦,不要担心。我知道有很多方法可以给猫剥皮,但你会这样做吗?只是试图确定什么是正确的方法。我一直在阅读有关“Messenger”中介者模式等的信息……如果您愿意的话,只是不确定什么是“最佳实践”。
【解决方案2】:

您也可以考虑使用松耦合的中介者模式。例如:

MVVM Light Toolkit 中 Laurent Bugnion 的调解器 here

MVVM 基金会的信使here

Prism 的事件聚合器here

Josh Twist 的 ISuckLessEventAggregator here

【讨论】:

    猜你喜欢
    • 2021-01-16
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多