gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:07:58 33 4
gpt4 key购买 nike

设置:

  • 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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com