的InitializeComponent(),而在C#中的Web应用程序使用泛型不当前上下文中 [英] initializecomponent() don't exist in current context while using generics in c# web application

查看:222
本文介绍了的InitializeComponent(),而在C#中的Web应用程序使用泛型不当前上下文中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想C#创建Web应用中的泛型和使用Silverlight-5。这个我已经在C#控制台应用程序实现的。

I am trying to create a generics in c# web application and using silverlight-5. This i have already implemented in c# console application.

我想在VS-2010使用asp.net,C#和Silverlight(和GUI使用XAML)做web开发中的相同。其GUI显示在Internet Explorer上运行的code(通过点击按钮事件)。

I am trying to do same in webdevelopment using asp.net,c# and silverlight (and GUI using xaml) in Vs-2010. Whose GUI is displayed on internet explorer on running the code (by button click events).

在控制台应用程序,我按照code这样做的:(的code是读一个二进制文件作为控制台应用程序的唯一参数,并在该文件中读取的符号,这些符号可能是 INT32 / INT16 / 64位整数/ UInt32的等)。因此,必须使这种符号变量作为通用(< T> )。而在控制台应用程序这个code正常工作。

In console application i do so by following code : (The code is to read a binary file as sole argument on console application and read the symbol in that file, These symbol could be int32/int16/int64/UInt32 etc.). So have to make this Symbol variable as "generic"(<T>). And in console application this code works fine.

    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应用程序)与我已经上传并存储按钮单击该文件(通过浏览它)的差异来实现(而我在上传/读取文件,如控制台唯一的参数同样的事情应用程序),这个文件上传部分我已经做了。

The same thing i have to achieve in C# silverlight(web application) with a difference that i have already uploaded and stored the file by button click (By Browsing it)(whereas i was uploading/reading file as sole argument in console application), This file upload part i have already done.

现在的问题是如何使这个符号变量通用的(&LT; T&GT; )这里,因为我无法看到任何对象创建(在main(字符串[] args)方法),在那里我可以传递参数BitConverter.ToInt32 / 64/16(如我在控制台应用程序做,请参阅code)。

The problem now is how to make this "symbol" variable generic(<T>) here because i am not able to see any Object creation (In main(string[] args) method) where i could pass parameter BitConverter.ToInt32/64/16 (as i am doing in console application, please see code).

注:请看到我在网上1,2,3,4,5在我的code使用的(这样相同(或如果你有其他的方法不同)有在下面的code来实现,使类型的符号)

NOTE: please see that i have used in LINE 1,2,3,4,5 in my code (so that the same(or different if you have other approach) has to be achieved in the code below to make "symbol" of type )

由于在C#中,我得到code这样的身体

Because in c# i get body of code like this

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应用程序的code正好类似于控制台应用程序code(我的意思是让符号变量作为通用&LT; T&GT; ))

编辑:当我做到这一点:

When i do this:

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

推荐答案

下面是一个例子。

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

我觉得你问这样一个例子。

I think you asking this kind of example.

这篇关于的InitializeComponent(),而在C#中的Web应用程序使用泛型不当前上下文中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆