WPF应用程序使用MVVM工具包在启动事件处理程序时无法编译

3

我有一个WPF应用程序,当我尝试向App类添加任何事件处理程序时,它无法编译。

以下是所有代码和我得到的异常。该应用程序使用MVVM工具包 - 所以这可能是一个因素。

如果有人能告诉我可能缺少或执行不正确的内容,那将不胜感激。


App.xaml代码:

<Application x:Class="MyClient.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:vm="clr-namespace:Sample.ViewModel"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d" Startup="Application_Startup">

<Application.Resources>
    <!--Global View Model Locator-->
    <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
    <!-- Resources scoped at the Application level should be defined here. -->
    <Style x:Key="TextBlockStyleFooter" TargetType="TextBlock">
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Margin" Value="1"/>
    </Style>
    <Style x:Key="TextBlockStyleClock" TargetType="TextBlock">
        <Setter Property="FontFamily" Value="Arial"/>
        <Setter Property="Foreground" Value="White"/>
        <!--<Setter Property="Margin" Value="0, -1,"/>-->
        <Setter Property="TextAlignment" Value="Center"/>
    </Style>
    <Style x:Key="BorderStyle1" TargetType="Border">
    </Style>
</Application.Resources>

App.xaml.cs 代码:

using System;
using System.Windows;
using System.Windows.Threading;
using GalaSoft.MvvmLight.Threading;

namespace Sample
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        static App()
        {
            DispatcherHelper.Initialize();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {

        }
    }
}

当我尝试编译这个时,我会收到以下异常:

Error   1   'MyClient.App' does not contain a definition for 'Application_Startup' and no extension method 'Application_Startup' accepting a first argument of type 'EdgePokerClient.App' could be found (are you missing a using directive or an assembly reference?)  C:\projects.git\MyClient\src\MyClient\App.xaml  7   73  MyClient
2个回答

3
问题在于你的XAML引用了MyClient.App,而你的代码后台文件中的partial类在Sample命名空间中。对于编译器来说,这是两个不同的类。因此,你在一个类(Sample.App)中定义的事件处理程序在生成的类MyClient.App中不存在。
你只需要修复代码后台文件中的命名空间或者XAML文件中的x:Name属性即可。
我还要提醒你小心App上的静态构造函数。我不确定代码生成器是否会添加一个公共的无参构造函数,但如果没有的话,只有一个静态构造函数将有效地意味着无法实例化App。

你是在指这个吗?xmlns:vm="clr-namespace:Sample.ViewModel"?因为我认为它不是问题所在。这是引用了一个不同的命名空间,以便可以在下面的<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True"/>标记中使用。或者我没有理解你的意思? - Joseph DeCarlo
不,<Application x:Class="MyClient.App" - Josh
啊..明白了...就是这样。非常感谢你,Josh。 - Joseph DeCarlo

0
我想为其他遇到同样错误的人发布这个评论。我曾经遇到过同样的错误,但是我的x:class命名空间与代码后台命名空间并没有不同。一些教程说,在XAML中放置Startup="Application_Startup"后,您的代码后台应该会自动更新。但实际上并没有发生,导致了这个错误。
我的解决方法-更新代码后台。
private void Application_Startup(object sender, StartupEventArgs e){}

添加之前

Startup="Application_Startup"

到XAML中。 希望这对某人有用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接