在c#web应用程序中使用泛型时,initializecomponent()在当前上下文中不存在

时间:2014-04-04 12:50:08

标签: c# asp.net silverlight generics initializecomponent

我正在尝试在c#web应用程序中创建泛型并使用silverlight-5。我已经在c#console应用程序中实现了。

我想在Vs-2010中使用asp.net,c#和silverlight(以及使用xaml的GUI)在webdevelopment中做同样的事情。运行代码时,Internet Explorer上显示的GUI(通过按钮单击事件)。

在控制台应用程序中,我通过以下代码执行此操作:(代码是在控制台应用程序上读取二进制文件作为唯一参数并读取该文件中的符号,这些symbol可以是int32/int16/int64/UInt32等)。因此,必须将此Symbol变量设为&#34; generic&#34;(<T>)。在控制台应用程序中,此代码可以正常工作。

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;



    namespace check 
    {
LINE:1  public class Huffman < T > where T: struct,IComparable < T >,IEquatable < T > 
        {
            public int data_size, length, i, is_there;
            public class Node 
            {
                public Node next;
line:2          public T symbol; // This symbol is of generic type.
                public int freq;
             }
            public Node front, rear;
LINE:3         public Huffman(string[] args, Func < byte[], int, T > converter) 
            {
                front = null;
                rear = null;
                int size = Marshal.SizeOf(typeof (T));
                using(var stream = new BinaryReader(System.IO.File.OpenRead(args[0]))) 
                {
                    long length = stream.BaseStream.Length;
                    for (long position = 0; position + size < length; position += size)
                    {
                        byte[] bytes = stream.ReadBytes(size);
LINE:4                  T processingValue = converter(bytes, 0); //**Here I read that symbol and store in processing value which is of type <T>** 
                        //Then  further i use this processingValue and "next" varible(which is on Node type)
                    }
                }
            }
        }

        public class MyClass
        {
            public static void Main(string[] args)
            {
    line:5            Huffman < long > ObjSym = new Huffman < long > (args, BitConverter.ToInt64); 
                    // It could be "ToInt32"/"ToInt16"/"UInt16"/"UInt32"/"UInt64" with respective 
                    //change in <int>/<short> etc.



                //Then i further use this ObjSym object to call function(Like Print_tree() here and there are many more function calls)   
                ObjSym.Print_tree(ObjSym.front);
            }
        }
    }

我必须在C#silverlight(Web应用程序)中实现同样的事情,区别在于我已经上传并通过按钮单击存储文件(通过浏览)(而我在控制台中上传/读取文件作为唯一参数应用程序),这个文件上传部分我已经完成了。

现在问题是如何制作这个&#34;符号&#34;变量泛型(<T>)这里因为我无法看到任何对象创建(在main(string [] args)方法中)我可以传递参数BitConverter.ToInt32 / 64/16(因为我我在控制台应用程序中,请参阅代码)。

注意:请注意我在我的代码中使用了LINE 1,2,3,4,5(因此必须实现相同(或者如果有其他方法则不同)在下面的代码中,使&#34;符号&#34;类型)

因为在c#中我得到像这样的代码

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;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace check
{
    public partial class MainPage : UserControl
    {
         public class Node
        {
            public Node next;
            public long symbol;   // This symbol is of generic type.
            public int freq;
        }
        public Node front, rear;

       public MainPage()
        {
            InitializeComponent();
        }

    }
}

有人可以帮助我更改此Web应用程序的代码与控制台应用程序代码完全相似(我的意思是制作&#34; Symbol variable as generic<T>)&#34;)< / p>

编辑:当我这样做时:

(1) public partial class MainPage <T> : UserControl, IComparable < T > where T: struct,IEquatable < T > 
(2) public T symbol; (In Node class)
(3) And all the buttons and boxes i created are given not existing in current context.

然后它给出了错误

Error :The name 'InitializeComponent' does not exist in the current context 

有人可以帮助我在c#silverlight Web应用程序中实现相同的目标吗?这将是一个很大的帮助,谢谢。

2 个答案:

答案 0 :(得分:1)

您可以使用generic,就像您不使用XAML一样。但是,如果您想使用XAML来定义控件,则不能使用泛型。这就是问题发生的原因。

创建另一个类并使用它。我认为这对你很有帮助。

答案 1 :(得分:1)

这是一个例子。

    namespace check
    {    
       public partial class MainPage : UserControl
       {
            public MainPage()
            {
                InitializeComponent();
                // Use the generic type Test with an int type parameter.
                Test<int> Test1 = new Test<int>(5);
                // Call the Write method.
                Test1.Write();

                // Use the generic type Test with a string type parameter.
                Test<string> Test2 = new Test<string>("cat");
                Test2.Write();
            }
       }

       class Test<T>
       {
           T _value;    
           public Test(T t)
           {
              // The field has the same type as the parameter.
              this._value = t;
           }    
           public void Write()
           {
              MessageBox.Show(this._value);
           }
       }
   }

我想你问这样的例子。