0

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

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

在控制台应用程序中,我通过以下代码执行此操作:(代码是读取二进制文件作为控制台应用程序的唯一参数并读取该文件中的符号,这些symbol可能是int32/int16/int64/UInt32等等)。所以必须将此Symbol变量设为“通用”(<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 应用程序)中实现同样的事情,不同之处在于我已经通过按钮单击(通过浏览它)上传和存储了文件(而我在控制台应用程序中作为唯一参数上传/读取文件),这个文件上传部分我已经完成了。

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

注意:请参阅我在代码中的第 1、2、3、4、5 行中使用过(因此必须在下面的代码中实现相同(或不同,如果您有其他方法)以制作“符号”类型 )

因为在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 应用程序的代码,该代码与该控制台应用程序代码完全相同(我的意思是制作“ Symbol variable as generic<T>)”)

编辑:当我这样做时:

(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 应用程序中实现同样的目标吗?将是一个很大的帮助,谢谢。

4

2 回答 2

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);
           }
       }
   }

我想你问这种例子。

于 2014-04-05T14:09:55.477 回答
1

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

创建另一个类并使用它。我想它对你有帮助。

于 2014-04-04T15:03:21.713 回答