【问题标题】:Is it possible to create objects of MainPage class in silverlight webapplication using c#是否可以使用 C# 在 Silverlight Web 应用程序中创建 Main Page 类的对象
【发布时间】:2014-04-03 14:32:47
【问题描述】:

我是使用 silverlight 5 的 c# web 应用程序初学者。在我从事 c# 控制台应用程序之前,我的代码体是这样工作的。

    namespace check
    {
     public class main
     {
      public void FunctionDefinition1()
      {
      //Inside something
      }

     }

     public class second
     {
      public static void Main(string[] args)
      {
       main object = new main();//this is how the objects were created and here the constructor was called.
       object1.FunctionDefinition1(); //I was calling the FunctionDefinition1 like this here 
      }

     }

    }

但是当我尝试在 c# silverlight(asp.net web 应用程序)中创建一个 web 应用程序时。我不知道如何从其他类调用函数定义以及在该类中如何以及在何处创建对象?

I have created a small project names as "Fun". see the code below


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace Fun
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
            }

            public void merge(int head)
            {
                MessageBox.Show("check1");
            }
        }
        public class she
        {
            MainPage obj1 = new MainPage();
            obj1.merge(1); //It is not working gives red line, means error.
        }
    }

有人可以解释一下我用这个代码银光c#代码吗:

(1) 类的对象是如何创建的(我的意思是在我们创建该类的对象时初始化构造函数,这里是否相同,如果是,那么为什么它在 VS 2010 中在合并时给出红线( ) 使用 MainPage 类的 Object 调用函数。

(2) 这个 InitializeComponent() 是做什么的;做吗?

(3) 为什么里面没有public static void Main(string[] args)函数?那么控制是如何通过编译器传递的。

(4) 假设如果我在 GUI 中插入一个按钮,那么现在代码更改为:

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace Fun
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
            }

            public void merge_sort(int head)
            {
                MessageBox.Show("check1");
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {

            }
        }
        public class she
        {
            MainPage obj1 = new MainPage();
            obj1.button1_Click(....);//It don't recognize "obj1" object. Why ?
        }
    }

现在可以从另一个类“she”调用 button1_Click() 函数吗?感谢有人能解释一下编译器如何控制这个 c# 代码,而我看不到任何 " public static void Main(string[] args)" 函数。

非常感谢您回答这些问题。

【问题讨论】:

    标签: c# silverlight web-applications silverlight-5.0 initializecomponent


    【解决方案1】:

    更新

    我想我发现了你的错误。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    
    namespace Fun
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                merge_sort();
            }
    
            public void merge_sort()
            {
                she Object1 = new she();
                Object1.DoMethod(2);//Now this will do your work.
            }
    
        }
        public class she
        {
            public void DoMethod(int head)
            {
               //Do what do you want
            }
    
        }
    }
    

    请解释如何访问她的班级?

    2)。初始化组件();

    用我的话来说,我认为它有你所有的输出,而且它也存在于编译的程序集中。

    3)。 public static void Main(string[] args);

    在 WPF Silverlight WP8 应用程序中不需要这个,因为在 App.Xaml 构建时会自动生成 Main 方法。如果你查看项目属性你会发现有一个设置供你选择启动对象。因此,如果您愿意,您可以提供自己的实现 Main() 的类。

    【讨论】:

    • 在你的代码中你在哪里调用 merge_sort() 函数?您从未调用过它,因此永远不会调用 DoMethod。如果您通过调用 merge_sort 来调用它,那么您将在 MainPage 类中的哪个位置调用它?
    • 还有 she.DoMethod();将给出:错误:非静态字段、方法或属性“Fun.she.DoMethod()”需要对象引用
    • 我犯了一个错误,我会更新答案
    • 试试这个就OK了。第二个错误,因为她的类不是静态类,所以你不能那样访问,现在它会好的。在 WPF 和 Silverlight 应用程序中,默认构造函数将首先执行。这意味着 InitializeComponent() 方法包含代码块。现在当页面加载时 merge_sort() 方法将执行。
    • 防守 两者都有不同的结构。您不能在 Silverlight 中使用所有控制台应用程序功能。 Silverlight 使用 .net 框架,但它没有 .net 框架的所有功能。所以你在 Silverlight 而不是控制台应用程序中有一些限制。你是什么意思 main() 函数?是主方法吗?
    猜你喜欢
    • 2011-05-01
    • 1970-01-01
    • 2023-04-02
    • 2017-03-03
    • 2023-04-04
    • 2021-01-19
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多