C# 是否可以使用c在silverlight webapplication中创建MainPage类的对象#
我是使用silverlight 5的c#web应用程序初学者。在我开发c#控制台应用程序之前,我有很多代码是这样工作的C# 是否可以使用c在silverlight webapplication中创建MainPage类的对象#,c#,silverlight,web-applications,silverlight-5.0,initializecomponent,C#,Silverlight,Web Applications,Silverlight 5.0,Initializecomponent,我是使用silverlight 5的c#web应用程序初学者。在我开发c#控制台应用程序之前,我有很多代码是这样工作的 namespace check { public class main { public void FunctionDefinition1() { //Inside something } } public class second { public
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.
}
}
有人能解释一下我使用的这个代码silver light c#代码是:
(1) 类的对象是如何创建的(我的意思是,当我们创建该类的对象时,构造函数是初始化的,这里是相同的,如果是,那么为什么在VS2010中使用MainPage类的对象调用merge()函数时,它在这里给出红线)
(2) 这个InitializeComponent()是什么;是吗
(3) 为什么它里面没有公共静态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\u Click()函数了吗?如果有人能向我解释编译器的控件是如何在c代码上运行的,而我却看不到任何“publicstaticvoidmain(string[]args)”
函数,那就太感谢了
非常感谢您回答这些问题。更新 我想我发现了你的错误
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) 。公共静态void Main(字符串[]args)
在WPF Silverlight中,WP8应用程序不需要此选项,因为当App.Xaml为时,将自动生成主方法。如果查看项目属性,您会发现有一个设置供您选择启动对象。因此,如果需要,可以提供自己的类来实现Main()。在代码中,在哪里调用merge_sort()函数?你从来没有叫过它,因此永远不会叫DoMethod。若您通过调用merge_sort来调用它,那个么您将在MainPage类中调用它?和she.DoMethod();will give:Error:非静态字段、方法或属性'Fun.she.DoMethod()'需要对象引用。我犯了一个错误,我会更新答案的。试试这个就可以了。第二个错误,因为she类不是静态类,所以您不能这样访问,现在就可以了。在WPF和Silverlight应用程序中,默认构造函数将首先执行。这意味着InitializeComponent()方法包含代码块。现在,当页面加载时,将执行merge_sort()方法。从防御性角度来说,这两种方法都有不同的结构。不能在Silverlight中使用所有控制台应用程序函数。Silverlight使用.net framework,但它没有.net framework的所有功能。因此,Silverlight中存在一些限制,而不是控制台应用程序。main()函数是什么意思?这是主要方法吗?