【问题标题】:Error event "loaded"错误事件“已加载”
【发布时间】:2014-07-21 20:43:27
【问题描述】:

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

'WpfApplication5.MonoBehaviour' 不包含对 'event_rotate' 并且没有扩展方法 'event_rotate' 接受 可以找到类型的第一个参数(您是否缺少 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设置为"WpfApplication5.MonoBehaviour",不应该是"WpfApplication5.Window2"吗?
  • 尝试使其对 XAML 可见:public void event_rotate(object sender, RoutedEventArgs e)
  • 如果我设置它会收到此错误“部分声明不得指定不同的基类”
  • 我试过了,现在不行

标签: c# xaml unity3d


【解决方案1】:

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

你可以从错误信息中看出。合并文件后,代码执行 event_rotate 方法以显示在代码隐藏中。此方法在 Window2 中声明,而不是在指定的 MonoBehavior 中,如 x:Class sais。

【讨论】:

    【解决方案2】:

    Unity MonoBehaviour 类不是 WPF Window 基类,因此不适合用作 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 实现的公共属性。

    【讨论】:

    • 我明白你的意思,但我有一个问题。例如,如果我想旋转一个立方体,我应该创建一个名为“mycuberotate”的类并初始化对象_monobehaviour=new mycuberotate?
    • @DiacoenscuCristian 我不是游戏开发者,无法回答有关如何呈现窗口内容的问题。我已将 Unity3D 标签添加到您的问题中,因此具有该专业知识的人可能会选择它。我的建议是咨询Unity scripting tutorials
    猜你喜欢
    • 2017-01-10
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 2022-12-17
    • 2018-07-09
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多