C# MVVM不动态加载视图

C# MVVM不动态加载视图,c#,wpf,mvvm,C#,Wpf,Mvvm,我编写了一个wpf应用程序,根据条件动态加载视图。XAML如下所示: <Window x:Class="Backup.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microso

我编写了一个wpf应用程序,根据条件动态加载视图。XAML如下所示:

<Window x:Class="Backup.MainWindow"
        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"
        xmlns:ignore="http://www.ignore.com"
        xmlns:views="clr-namespace:Backup.Views"
        xmlns:vm="clr-namespace:Backup.ViewModel"
        mc:Ignorable="d ignore"
        Height="300"
        Width="300"
        Title="Backup V1.0"
        WindowStartupLocation="CenterScreen"
        ResizeMode="NoResize"
        WindowStyle="None"
        DataContext="{Binding Main, Source={StaticResource Locator}}">

    <Window.Resources>
        <DataTemplate DataType="{x:Type vm:DeniedViewModel}">
            <views:DeniedView />
        </DataTemplate>

        <DataTemplate DataType="{x:Type vm:CopyViewModel}">
            <views:CopyView />
        </DataTemplate>

    </Window.Resources>

    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <ContentControl Content="{Binding CurrentVm}" Grid.Column="0" Grid.Row="0" />
    </Grid>
</Window>

我该怎么办?

吞咽异常?如果你有权访问另一台机器,请对其进行监视。浏览可视化树并查看绑定。它会告诉你问题是什么。我认为DataContext设置不正确。请检查此DataContext={Binding Main,Source={StaticResource Locator}}。@AbinMathew如何绑定DataContext然后更正检查此链接:编辑中的错误表示它与DataContext相关:-您需要在ViewModelLocator中使用名为Main的属性
using System;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using Backup.Model;
using GalaSoft.MvvmLight;
using Libraries.Extension;
using Libraries.Interfaces;

namespace Backup.ViewModel
{
    /// <summary>
    ///     This class contains properties that the main View can data bind to.
    ///     <para>
    ///         See http://www.galasoft.ch/mvvm
    ///     </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private readonly ViewModelLocator _locator;
        //private readonly IDataService _dataService;
        private readonly IPermissionService _permission;
        private readonly IPersistence _persistence;
        private ViewModelBase _currentVm;

        /// <summary>
        ///     Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IPermissionService permission, IPersistence persistence)
        {

            if (_GetOSInfo() != "7")
            {
                Application.Current.Shutdown();
                return;
            }

            _permission = permission;
            _persistence = persistence;
            _locator = (ViewModelLocator) Application.Current.TryFindResource("Locator");

            CurrentVm = _locator.DeniedVm;


            //Validate if the user is in ad group
            //_persistence.QueryGroups((groups, err) =>
            //{
            //    if (!_permission.Check(groups))
            //    {
            //        CurrentVm = _locator.DeniedVm;
            //        Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
            //        {
            //            Thread.Sleep(3000);
            //            Application.Current.Shutdown();
            //        }));
            //        return;
            //    }

            //    CurrentVm = _locator.CopyVm;
            //});
        }

        public ViewModelBase CurrentVm
        {
            get { return _currentVm; }
            set
            {
                _currentVm = value;
                RaisePropertyChanged(MethodBase.GetCurrentMethod().GetPropertyName());
            }
        }

        private string _GetOSInfo()
        {
            //Get Operating system information.
            var os = Environment.OSVersion;
            //Get version information about the os.
            var vs = os.Version;

            //Variable to hold our return value
            var operatingSystem = "";

            if (os.Platform == PlatformID.Win32Windows)
            {
                //This is a pre-NT version of Windows
                switch (vs.Minor)
                {
                    case 0:
                        operatingSystem = "95";
                        break;
                    case 10:
                        if (vs.Revision.ToString() == "2222A")
                            operatingSystem = "98SE";
                        else
                            operatingSystem = "98";
                        break;
                    case 90:
                        operatingSystem = "Me";
                        break;
                    default:
                        break;
                }
            }
            else if (os.Platform == PlatformID.Win32NT)
            {
                switch (vs.Major)
                {
                    case 3:
                        operatingSystem = "NT 3.51";
                        break;
                    case 4:
                        operatingSystem = "NT 4.0";
                        break;
                    case 5:
                        if (vs.Minor == 0)
                            operatingSystem = "2000";
                        else
                            operatingSystem = "XP";
                        break;
                    case 6:
                        if (vs.Minor == 0)
                            operatingSystem = "Vista";
                        else
                            operatingSystem = "7";
                        break;
                    default:
                        break;
                }
            }
            return operatingSystem;
        }
    }
} 
System.Windows.Data Error: 17 : Cannot get 'Main' value (type 'MainViewModel') from '' (type 'ViewModelLocator'). BindingExpression:Path=Main; DataItem='ViewModelLocator' (HashCode=39201736); target element is 'MainWindow' (Name=''); target property is 'DataContext' (type 'Object') TargetInvocationException:'System.Reflection.TargetInvocationException