通过点击按钮,使用Silverlight的MVVM方式打开多个视图(Opening multiple

2019-10-23 11:38发布

我想用MVVM的方式来实现类似下面:

我有一个主窗口在那里我有一个3个按钮,如:一)客户B)订单C)销售

通过点击按钮,就应该打开其各自的窗口/用户控件XAML与客户信息,订单信息,销售信息。

我曾尝试一切,但culdnt能够这样做。

如何实现这一目标使用MVVM模式。 请提供解决方案?

谢谢

Answer 1:

答案取决于你希望你的客户,订单和销售视图中显示。 如果你想显示他们在同一视图中,只需添加绑定属性在主视图模型内容的控制。

例如,如果您使用的MVVM光工具包 ,你的MainPage.xaml中可能看起来像...

<UserControl x:Class="MvvmLight2.MainPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             Height="300"
             Width="300"
             DataContext="{Binding Main, Source={StaticResource Locator}}">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Skins/MainSkin.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <StackPanel Orientation="Vertical">

        <StackPanel Orientation="Horizontal">
            <Button Content="Customers" Command="{Binding DisplayView}" CommandParameter="Customers" Margin="10" />
            <Button Content="Orders" Command="{Binding DisplayView}" CommandParameter="Orders" Margin="10" />
            <Button Content="Sales" Command="{Binding DisplayView}" CommandParameter="Sales" Margin="10" />
        </StackPanel>

        <ContentControl Content="{Binding CurrentView}" IsTabStop="False" Margin="10" />

    </StackPanel>
</UserControl>

而你MainPageViewModel会...

using System.Windows.Controls;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace MvvmLight2.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        public MainViewModel()
        {
            DisplayView = new RelayCommand<string>(DisplayViewCommandExecute);
        }

        #region Commands

        public RelayCommand<string> DisplayView { get; private set; }

        #endregion

        #region CurrentView Property

        public const string CurrentViewPropertyName = "CurrentView";

        private UserControl _currentView;

        public UserControl CurrentView
        {
            get { return _currentView; }
            set
            {
                if (_currentView == value)
                    return;

                _currentView = value;
                RaisePropertyChanged(CurrentViewPropertyName);
            }
        }

        #endregion

        private void DisplayViewCommandExecute(string viewName)
        {
            switch (viewName)
            {
                case "Customers":
                    CurrentView = new CustomersView();
                    break;
                case "Orders":
                    CurrentView = new OrdersView();
                    break;
                case "Sales":
                    CurrentView = new SalesView();
                    break;
            }
        }
    }
}

这一切都假定你有客户,订单和销售创建的视图和视图模型,并修改了ViewModelLocator,包括他们。

在这一点上,如果你需要在你的孩子视图来显示特定的信息,您可以在其中创建一个依赖属性,然后设置,从你MainViewModel您显示视图之前。



Answer 2:

你可能要考虑的调解模式 。 常见的实现是信使类的MVVM光工具包和事件汇总在PRISM。

使用这种模式有一个基本的工作流程...命令被称为上viewmodel1。 Viewmodel1注册与调解员的一些消息。 Viewmodel2订阅该消息,并且执行一些响应(如创建新视图2或改变视图2的视觉状态)。



Answer 3:

我想这使用的Sliverlight Naviagtion应用MVVM

http://garfoot.com/blog/2010/09/silverlight-navigation-with-the-mvvm-pattern/

很简单的例子。 无框架涉及如此。

但使用MVVM框架使生活更轻松以备将来使用。

对于MVVM和棱镜框架检查此链接..

http://blog.roboblob.com/2010/10/24/introducing-prism-navigation-framework-for-silverlight-mvvm-applications/



文章来源: Opening multiple views by clicking button using MVVM silverlight approach