I wrote a wpf application that should load view dynamically, depends on condition.The XAML looks like:
<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>
and the view model
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;
}
}
}
When I executed the application on my computer, everything works fine, but on other computer it shows only blank wpf surface.
I suspect, that the view is not loading correctly, maybe contentcontrol does not load the view.
What am I doing wrong?
Update
I analysed wpf with snoop following error occurs:
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
What should I do?