简体   繁体   English

错误事件“已加载”

[英]Error event “loaded”

I don`t understand this error occurs, though I have declared this method. 我已经声明了此方法,但我不理解会发生此错误。 look at this code and error. 看看这段代码和错误。

'WpfApplication5.MonoBehaviour' does not contain a definition for 'event_rotate' and no extension method 'event_rotate' accepting a first argument of type could be found (are you missing a using directive or an assembly reference?) 'WpfApplication5.MonoBehaviour'不包含'event_rotate'的定义,也找不到找不到接受第一个类型参数的扩展方法'event_rotate'(您是否缺少using指令或程序集引用?)

Code c# 1: 代码C#1:

private void event_rotate(object sender, RoutedEventArgs e)
{
    MessageBox.Show("asdasada");
}

Code c# 2: 代码C#2:

using UnityEngine;
using System.Collections;
namespace WpfApplication5
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : MonoBehaviour

Code xaml: 代码xaml:

<Window
    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" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfApplication5.MonoBehaviour"
    Title="Window2" Height="300" Width="300" Loaded="event_rotate">

the x:Class attribute has the wrong value. x:Class属性的值错误。 the x:Class attribute specifies the codebehind class that should be merged with the XAML code. x:Class属性指定应与XAML代码合并的代码隐藏类。 you specified the MonoBehavior class in XAML. 您在XAML中指定了MonoBehavior类。 It should have been Window2. 应该是Window2。 Window2 does contain the event declaration & that does not need to be public. Window2确实包含事件声明&,不需要公开。

you can tell from the error message. 您可以从错误消息中分辨出来。 after merging the files, the code exects the event_rotate method to be present in the codebehind. 合并文件后,代码将执行event_rotate方法以显示在后面的代码中。 this method is declared in Window2 not in the specified MonoBehavior like x:Class sais. 此方法在Window2中声明,而不是在指定的MonoBehavior中声明,例如x:Class sais。

The Unity MonoBehaviour class is not a WPF Window base class and is therefore not appropriate to use as the base class for your Window2 class. Unity MonoBehaviour类不是 WPF Window基类,因此不适合用作Window2类的基类。 It could however be a property of that class (assuming you can instantiate it). 但是,它可能是该类的一个属性(假设您可以实例化它)。

C# code: C#代码:

using UnityEngine;
using System.Collections;
namespace WpfApplication5
{
    public partial class Window2 : Window
    {
        // You must implement your own MyScriptClass that has MonoBehaviour as it's
        // base class and provide an appropriate constructor to use here, in the
        // Window2 constructor or some other appropriate location.
        private MonoBehaviour _monoBehaviour = new MyScriptClass();

        // Default constructor
        public Window2()
        {
            // Set up your _monoBehaviour here

            // Initialise the WPF Window GUI object
            InitializeComponent();
        }

        // Called by the WPF FrameworkElement.Loaded Event
        public void event_rotate(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("asdasada");
        }
    }
}

XAML code: XAML代码:

<Window x:Class="WpfApplication5.Window2"
        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"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="Window2"
        Height="300" Width="300"
        Loaded="event_rotate"
        >

I'm not familiar with the Unity framework and don't know how you link it's GUI implementation with the XAML Window. 我不熟悉Unity框架,也不知道如何将其GUI实现与XAML窗口链接。 I'd assume that you will want to data bind an approriate UserControl container class to a public property that exposes the private _monoBehaviour implementation. 我假设您想将一个适当的UserControl容器类数据绑定到一个公开私有_monoBehaviour实现的公共属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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