我是使用silverlight 5的c#web应用程序初学者。在我使用c#console应用程序之前,我的代码体系就像这样。
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 ?
}
}
现在可以从另一个类调用button1_Click()函数"她" ?谢谢,如果有人可以解释我的编译器控制如何进行这个c#代码,而我无法看到任何" public static void Main(string[] args)"
函数。
非常感谢您回答这些问题。
答案 0 :(得分:2)
<强>已更新强>
我想我发现了你的错误。
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)。的InitializeComponent();
在我看来,我认为它拥有你所有的输出,而且它也存在于已编译的程序集中。
3)。 public static void Main(string [] args);
在WPF中,Silverlight WP8应用程序不需要这样,因为当构建App.Xaml时,将自动生成主方法。如果你看项目属性,你会发现有一个设置供你选择启动对象。因此,如果您愿意,您可以提供自己的实现Main()的类。