简体   繁体   中英

Unable to cast object of type 'System.Windows.Application' to type 'applicationName.App' error

Setup:

  • Visual studio 2015 Community
  • WPF 4.5 application
  • MVVM design structure

Problem:

Error which is causing problems is somehow related to the application naming and also to common casting, however, this error appeared after my attempt to alter the project properties on Visual Studio 2015 . Particularly the Assembly name and Default namespace . Now I am unable to find the cause and fix it.

Any tips about what would be the right way to proceed?

This error is appearing on the App.xaml :

Severity Code Description Project File Line Suppression State Error Unable to cast object of type 'System.Windows.Application' to type 'FxConnection.App'. AppName C:\\Users*\\Documents\\Visual Studio 2015\\Project1\\App.xaml 11

The line in question is:

<Application.Resources>
        <ResourceDictionary>
            <viewModels:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />

I am using Mvvm light and its viewModelLocator on my project. This error is causing some binding errors when I am trying to use the Locator to find the viewmodels.

EDIT1:

App.xaml

<Application x:Class="FxConnection.App" 
             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" 
             d1p1:Ignorable="d" xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:viewModels="clr-namespace:FxConnection.ViewModels" 
             xmlns:ds="clr-namespace:FxConnection.Helpers">
    <!-- StartupUri="Views/MainWindow.xaml"-->
    <Application.Resources>
        <ResourceDictionary>
            <viewModels:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
            <BitmapImage x:Key="FidelixLogo" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" UriSource="/**/logo.png" />
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="*******" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

App.xaml.cs

using System;
using System.Deployment.Application;
using System.Diagnostics;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using FxConnection.Properties;
using FxConnection.Views;
using FxConnection.Views.ProjectView;
using GalaSoft.MvvmLight.Threading;

namespace FxConnection
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            // Handle globally exceptions
#if DEBUG
            //Application.Current.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(AppDispatcherUnhandledException);
#else
            Application.Current.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(AppDispatcherUnhandledException);
#endif
            new ProjectDeviceDatabaseView().Show();
            base.OnStartup(e);
            Settings.Default.Reload();
            DispatcherHelper.Initialize();  // allows messenger functionality between threads !
        }

        protected override void OnExit(ExitEventArgs e)
        {
            Settings.Default.Save();
            base.OnExit(e);
        }

    }
}

Make sure that your App class in App.xaml.cs derives from System.Window.Application :

public partial class App : System.Window.Application
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM