模式弹出和C#的意见下MVVM模式通信 [英] Modal Popup and views communication under MVVM pattern on C#

查看:147
本文介绍了模式弹出和C#的意见下MVVM模式通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的项目,其中VB6我一直在努力升级到了数个月的新技术进行编码。我的项目包括在客户端 - 服务器应用程序下团聚6个行政模块。从VB来了,我的合乎逻辑的选择是升级到.NET。大量的研究后,我决定使用 C# WPF 和MVVM模式(与卡利微)。

I have a large project coded with VB6 which I've been trying to upgrade to new technologies for a few months. My project consist on 6 administrative modules reunited under a client-server application. Coming from VB, my logical choice was to upgrade to .NET. After a lot of research I decide to use C#, WPF and the MVVM pattern (with Caliburn Micro).

在beggining我有一些问题,但我设法解决这些问题。但现在我已经到了一个地步,我需要(像每一个复杂的应用程序),通过模态弹出不同的看法及其相应的视图模型(或其他技术)进行通信。而在这个问题上的MVVM模式似乎是非常严格和复杂。一个简单的你确定要删除这条记录(是/否)是一个非常复杂的任务。于是我找了意见,无需复杂的工件作为EventAgregators如何沟通意见。

At the beggining I had some problems, but I managed to resolve them. But now I've come to a point where I need (like every complex application) to communicate with different views and their corresponding viewModel through modal popups (or some other technique). And in this matter the MVVM pattern seems to be very restrictive or complex. A simple "Are you sure you want to delete this record (yes/no)" is a very complex task. So I'm looking for advice as how communicate views without complex artifacts as EventAgregators.

到目前为止,唯一可行的替代方案,我发现是使用 ModalContentPresenter 类,从的这个博客。这种解决方案的问题是:

So far the only possible alternative I've found is to use the ModalContentPresenter class from this blog. The problems with this solution are:


  • 我需要写上同样的观点父亲认为XAML和模态XAML
  • $。 b $ b
  • 我不能从同一个视图中的多个popus。

  • I need to write the father view XAML and modal XAML on the same view.
  • I cannot have multiple popus from same view.

我想在哪里使用模式弹出窗口的一些例子是:

Some examples of where I'd like to use modal popups is:


  • 放在一个视图中的按钮,选择客户端。它应该打开一个弹出所有更多钞票的客户,并让用户选择之一。

  • 添加产品弹出来一个客户订单。

任何意见或建议?一些示例代码,将不胜感激?谢谢!

Any ideas or suggestions? Some sample code would be appreciated? Thanks!

推荐答案

我的链接 ModalContentPresenter 控件的作者如此我将设法解决您的一些问题和疑虑。

I am the author of the linked ModalContentPresenter control so I will attempt to address some of your questions and concerns.

我需要写上同样的观点父亲认为XAML和模态XAML。

I need to write the father view XAML and modal XAML on the same view.

您可以实际编写的两个在单独的文件的意见。该视图可以使用动态加载的DataTemplates 将取决于视图模型绑定到无论是内容 ModalContent 属性。

You can actually write both views in separate files. The views can then be loaded dynamically using DataTemplates which will depend on the ViewModel that is bound to either the Content or ModalContent properties.

请参阅的这个其中描述了可以实现这个视图切换的一般方式。

See this which describes the general way in which this view switching can be achieved.

您可以有一个 MainViewModel 这有两个属性, PrimaryViewModel SecondaryViewModel 它会返回相应的视图模型这为主体和模态内容属性和命令。

You could have a MainViewModel which has two properties, PrimaryViewModel and SecondaryViewModel which return appropriate view models which provide the properties and commands for the main and modal content.

您可以在 XAML以下安装

<DataTemplate DataType="{x:Type FooViewModel}">
    <Controls:FooView />
</DataTemplate>

<DataTemplate DataType="{x:Type BarViewModel}">
    <Controls:BarView />
</DataTemplate>

<controls:ModalContentPresenter 
              Name="modalPresenter"
              Content={Binding DataContext.PrimaryViewModel}
              ModalContent={Binding DataContext.SecondaryViewModel} />

在该 IsModal 属性是,只有你的 PrimaryView 将被显示。只要你的 IsModal 属性设置为真正 ModalContentPresenter 将显示你的 SecondaryView

When the IsModalproperty is false, only your PrimaryView will be displayed. As soon as you set the IsModal property to true the ModalContentPresenter will display your SecondaryView.

我不能从同一个视图中的多个popus。

I cannot have multiple popus from same view.

我想你的意思是你要能够显示来自不同模态的内容的在不同的时间的同样的主视图。

I take it you mean you want to be able to display different modal content at different times from the same main view.

使用上面的方法,这是因为开关一样简单视图模型绑定到 ModalContent 属性(通过设置 IsModal 显示它之前真正) 。只要你有一个的DataTemplate 视图模型绑定(和你的 MainViewModel 工具 INotifyPropertyChanged的正确),正确的内容将被显示。

Using the above technique this is as simple as switching the ViewModel that is bound to the ModalContent property (before displaying it by setting IsModal to true). As long as you have a DataTemplate for the ViewModel that is bound (and your MainViewModel implements INotifyPropertyChanged correctly), the correct content will be displayed.

这是我想在哪里使用模式弹出窗口的一些例子是:

Some example on where i'd like to use modal popups is:

放在一个视图中的按钮,选择客户端。它应该打开与
所有可能的客户端的弹出,让用户选择之一。

Put a button on a view to select a Client. It should open a popup with all possible clients and let the user chose one.

添加产品弹出来一个客户订单。

Add a product popup to a customer order.

一旦你明白你上面描述的技术应该能够看到,只要你有一个查看视图模型对,你可以,涵盖你能想到的任何情况。

Once you understand the technique described above you should be able to see that the as long as you have a View and ViewModel pair you can cover any scenario you can think of.

作为一个例子,假设有以下接口的ViewModels:

As an example, consider viewModels that have the following interfaces:

public interface SelectCustomerViewModel : INotifyPropertyChanged {
    event EventHandler CustomerSelected;        
    public ObservableCollection<Customer> Customers { get; }
    public Customer Customer { get; set; }
    public Command CustomerSelectedCommand { get; }
}

public interface MainViewModel : INotifyPropertyChanged {
    public SelectCustomerViewModel ModalContent { get; }
    public Command SelectCustomerCommand { get; }
    public bool IsSelectingCustomer { get; }
}

您可以有 XAML 看起来是这样的:

You could have XAML that looks something like this:

<Window x:Class="ModalContentTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Select a customer">

    <DataContext>
        <vm:MainViewModel />
    </DataContext>

    <DataTemplate DataType="{x:Type SelectCustomerViewModel}">
        <Controls:SelectCustomerView />
    </DataTemplate>

    <c:ModalContentPresenter Name="modalPresenter"
                             ModalContent={Binding ModalContent}
                             IsModal={Binding IsSelectingCustomer}>

        <!-- This is the primary content! -->
        <Grid>
            <Button Content="Select a customer"
                    Command={Binding SelectCustomerCommand} />
        </Grid>

    </c:ModalContentPresenter>

</Window>

下面是它如何工作的:


  1. IsSelectingCustomer 属性 MainViewModel 将开始为

  2. 单击主视图按钮将调用 SelectCustomerCommand 对象。然后,命令会告诉 MainViewModel IsSelectingCustomer 属性更改为真正

  3. ModalContentPresenter 将显示在数据模板指定的视图。用户现在只能用选择客户视图进行互动。

  4. 一旦客户被选中,一个按钮可以点击(这势必会在 CustomerSelectedCommand SelectCustomerViewModel ),这反过来会提高 CustomerSelected 事件。

  5. MainViewModel 将有一个事件处理程序,将在 CustomerSelected 事件作出响应。然后,处理程序会读取来自 SelectedCustomer 属性 SelectCustomerViewModel 最后,它会设置 IsSelectingCustomer 属性返回假,导致模态的内容被关闭。

  1. The IsSelectingCustomer property of the MainViewModel would start off as false.
  2. Clicking the button in the main view would invoke the SelectCustomerCommand object. The command would then tell the MainViewModel to change the IsSelectingCustomer property to true.
  3. The ModalContentPresenter would display the view specified by the data template. The user can now only interact with the 'select customer view'.
  4. Once a customer has been selected, a button can be clicked (which is bound to the CustomerSelectedCommand of the SelectCustomerViewModel) which in turn would raise the CustomerSelected event.
  5. The MainViewModel would have an event handler that would respond to the CustomerSelected event. The handler would then read the SelectedCustomer property from the SelectCustomerViewModel and finally, it would set the IsSelectingCustomer property back to false, causing the modal content to be closed.

这篇关于模式弹出和C#的意见下MVVM模式通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆