c# - 无法将类型为 'System.Windows.Application' 的对象转换为类型 'applicationName.App' 错误

标签 c# wpf application-name

设置:

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

问题:

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

关于什么是正确的继续进行的任何提示?

此错误出现在 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

有问题的行是:

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

我正在使用 Mvvm light及其 viewModelLocator在我的项目上。当我尝试使用 Locator 时,此错误导致一些绑定(bind)错误找到 View 模型。

编辑 1:

应用.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.cs 中的 App 类派生自 System.Window.Application:

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

关于c# - 无法将类型为 'System.Windows.Application' 的对象转换为类型 'applicationName.App' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42530803/

相关文章:

c# - Visual Studio C# 2010 Express 代码编辑器和 "-"个字符

c# - 错误 : No value given for one or more required parameters

wpf - 如何在单独的线程上创建和显示 WPF 窗口?

WPF TextBlock 动态粗体和斜体部分

android - 我可以在 google play 控制台中的应用程序中添加标签吗?

c# - FileSystemWatcher - 哪个进程导致了更新

c# - Xamarin Forms 更改主页 ios 错误

.net - DataGridRow 上的 WPF DataGrid MouseOver

java - 如何获取/设置 Groupcode 1001 的 .dxf 中的应用程序名称?

android - 在主屏幕上显示完整的应用程序名称