已“加载”错误事件

本文关键字:错误 事件 加载 | 更新日期: 2023-09-27 18:13:45

我不明白这个错误发生了,虽然我已经声明了这个方法。看看这段代码和错误。

WpfApplication5。MonoBehaviour'不包含的定义'event_rotate'和没有扩展方法'event_rotate'接受a可以找到类型的第一个参数(您是否缺少using指令还是程序集引用?)

代码c# 1:

private void event_rotate(object sender, RoutedEventArgs e)
{
    MessageBox.Show("asdasada");
}
代码c# 2:
using UnityEngine;
using System.Collections;
namespace WpfApplication5
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : MonoBehaviour

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">

已“加载”错误事件

x: class属性值错误。Class属性指定应该与XAML代码合并的代码隐藏类。您在XAML中指定了MonoBehavior类。应该是windows 2。Window2确实包含事件声明&不需要是公开的

可以从错误消息中看出。合并文件后,代码期望在代码隐藏中出现event_rotate方法。此方法在Window2中声明,而不是在指定的MonoBehavior中声明,如x:Class。

Unity MonoBehaviour类是不是一个WPF窗口基类,因此不适合用作你的Window2类的基类。然而,它可以是该类的一个属性(假设你可以实例化它)。

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代码:

<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"
        >

我不熟悉Unity框架,不知道你如何链接它的GUI实现与XAML窗口。我假设您希望将适当的UserControl容器类数据绑定到公开私有_monoBehaviour实现的公共属性。