【问题标题】:Unable to cast object of type 'System.Windows.Application' to type 'applicationName.App' error无法将“System.Windows.Application”类型的对象转换为“applicationName.App”错误
【发布时间】:2017-03-01 11:42:47
【问题描述】:

设置:

  • Visual Studio 2015 社区
  • WPF 4.5 应用程序
  • MVVM 设计结构

问题:

导致问题的错误在某种程度上与应用程序命名和常见转换有关,但是,在我尝试更改 Visual Studio 2015 上的 project properties 后出现此错误。特别是Assembly name 和默认namespace。现在我无法找到原因并修复它。

关于如何进行的正确方法的任何提示?

这个错误出现在App.xaml:

严重性代码描述项目文件行抑制状态 错误无法将“System.Windows.Application”类型的对象转换为 类型 'FxConnection.App'。应用名称 C:\Users*\Documents\Visual 工作室 2015\Project1\App.xaml 11

有问题的行是:

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

我在我的项目中使用Mvvm light 及其viewModelLocator。当我尝试使用 Locator 查找视图模型时,此错误会导致一些绑定错误。

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);
        }

    }
}

【问题讨论】:

  • 请发布您的 App.xaml 文件的完整标记以及 App.xaml.cs 文件的代码。

标签: c# wpf application-name


【解决方案1】:

确保App.xaml.cs 中的App 类派生自System.Window.Application:

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

【讨论】:

  • 似乎井然有序。 Resharper 建议:Base type 'Application' is already specified in other parts
猜你喜欢
  • 2018-03-22
  • 2023-04-03
  • 1970-01-01
  • 2014-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多