首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在c# web应用程序中使用泛型时,在当前上下文中不存在initializecomponent()

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

Stack Overflow用户
提问于 2014-04-04 12:50:08
回答 2查看 1.4K关注 0票数 0

我试图在c# web应用程序中创建一个泛型,并使用silverlight-5。我已经在c#控制台应用程序中实现了这一点。

我试图在Vs-2010中使用asp.net、c#和silverlight (以及GUI使用xaml)进行and开发。其GUI在运行代码时显示在internet上(通过按钮单击事件)。

在控制台应用程序中,我这样做是通过以下代码:(代码是将二进制文件作为控制台应用程序中的唯一参数读取,并读取该文件中的符号,这些symbol可以是int32/int16/int64/UInt32等)。因此,必须将这个Symbol变量设置为“泛型”(<T>)。在控制台应用程序中,这段代码运行良好。

代码语言:javascript
复制
    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应用程序)中实现与我已经通过按钮(通过浏览)上传和存储文件的不同之处(而我在控制台应用程序中作为唯一参数上传/读取文件),这个文件上传部分我已经做过了。

现在的问题是如何使这个“符号”变量generic(**<T>**)在这里,因为我无法看到任何对象创建(在main(string[] args)方法中),其中我可以传递参数BitConverter.ToInt 32/64/16(正如我在控制台应用程序中所做的那样,请参见代码)。

注意:请注意,我在代码中使用了第1行、第2行、第3行、第4行、第4行和第5行(因此,必须在下面的代码中实现相同的(或不同的方法),以生成类型的“符号”)。

因为在c#中,我得到了如下代码

代码语言:javascript
复制
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应用程序的代码与控制台应用程序代码完全类似(我的意思是制作"Symbol variable as generic(<T>)")

编辑:当我这样做时:

代码语言:javascript
复制
(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.

然后给出错误

代码语言:javascript
复制
Error :The name 'InitializeComponent' does not exist in the current context 

可以帮助我在c# silverlight web应用程序中实现同样的功能吗?将是一个很大的帮助,谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-05 14:09:55

下面是一个例子。

代码语言:javascript
复制
    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);
           }
       }
   }

我想你是在问这样的例子。

票数 1
EN

Stack Overflow用户

发布于 2014-04-04 15:03:21

您可以像不使用XAML一样使用泛型。但是,如果要使用XAML定义控件,则不能使用泛型。这就是问题出现的原因。

创建另一个类并使用它。我觉得这能帮到你。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22863338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档