ThienThanCNTT
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

[Chia sẻ] Cách sử dụng Snippet tăng tốc độ gõ lệnh trong C#

Go down

[Chia sẻ] Cách sử dụng Snippet tăng tốc độ gõ lệnh trong C# Empty [Chia sẻ] Cách sử dụng Snippet tăng tốc độ gõ lệnh trong C#

Bài gửi by nth 28/04/11, 10:15 am

ác bạn thường phải hì hục gõ các câu lệnh C# một cách thủ công, từng câu lệnh môt..
nhưng có bao giờ các bạn nghĩ rằng.. bạn có thể sử dụng phím tắt cho những câu lệnh đó ko

VD: chỉ cần gõ cw là sẽ hiển thị Console.WriteLine()

Visual Studio đã hỗ trợ tính năng đó giúp bạn bằng snippet

Visual hỗ trợ khá nhiều lệnh tắt cho các bạn
[You must be registered and logged in to see this image.]
Để sử dụng, bạn gõ tên snippet và nhấn tab 2 lần
Một số lệnh cơ bản
cw :  Console.WriteLine();
class: tạo lớp MyClass tự động
sim: tạo hàm main tự động
prop: properties
......

rất nhiều
Bạn có thể tham khảo tất cả các snippet của C# bằng cách
- Chuột phải vào file Code của mình
- Chọn Insert Spinnet -> Visual C#

lúc đó sẽ hiển thị lên tất cả các snippet do C# định nghĩa
Tuy nhiên, nhiêu đó có là đủ với bạn
BẠn mún code của mình tự động nhiều hơn nữa Smile).... ví dụ
trong Class chỉ cần gõ một lệnh sẽ tự động tạo 1 field và properties cung cấp cả hàm get và set
Sau đây mình sẽ hướng dẫn cách bạn làm được như thế Wink)

Đầu tiên mở Notepad
copy code sau vào
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="[You must be registered and logged in to see this link.]">
<CodeSnippet Format="1.0.0">
<Header>
<Title>propf</Title>
<Shortcut>propf</Shortcut>
<Description>Field + property </Description>

<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[
private $type$ $property$;

public $type$ $property$
{
get{ return $property$;}
set{ $property$= value;}
}
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>

Bạn lưu lại file này với tên là thienthancnttzone.snippet

Lưu ý: bắt buộc phải là đuôi .snippet
Bạn copy file đó vào C:\Users\tenUser\Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets
Ở đây mình sử dụng Visual Studio 2k8, những bạn sử dụng phiên bản khác có thể tìm kiếm đường dẫn tương tự..

Bây giờ, mở C# lên nào... bạn thử gõ propf+ 2 tab xem kêt quả nhá)
nth
nth
Admin
Admin

Tổng số bài gửi : 550
Số điểm : 1113
Số lần được cám ơn : 33
Ngày đến diễn đàn: : 01/08/2009
Tuổi : 35
Đến từ : Thiên Đường

https://thuhuong.forumvi.net

Về Đầu Trang Go down

[Chia sẻ] Cách sử dụng Snippet tăng tốc độ gõ lệnh trong C# Empty Re: [Chia sẻ] Cách sử dụng Snippet tăng tốc độ gõ lệnh trong C#

Bài gửi by nth 28/04/11, 10:48 am

Mình đã text thử bài trên. Thực ra mình thấy khi mình dùng 2k10, mình chỉ cần bấm prop và enter là nó đã hiện ra property rồi, không cần phải dùng rườm rà như vậy.
Nhưng cách trên cũng có cái hay khi mình muốn tự tạo thêm 1 số cái riêng mà mình hay dùng, cho việc code tiện lợi hơn. Ví dụ:
cr (Console.ReadLine())
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="[You must be registered and logged in to see this link.]">
<CodeSnippet Format="1.0.0">
<Header>
<Title>cr</Title>
<Shortcut>cr</Shortcut>
<Description>Chay lenh Console.ReadLine() </Description>
<Author>nth</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal Editable="false">
<ID>SystemConsole</ID>
<Function>SimpleTypeName(global::System.Console) </Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[$SystemConsole$.ReadLine($end$);]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
nth
nth
Admin
Admin

Tổng số bài gửi : 550
Số điểm : 1113
Số lần được cám ơn : 33
Ngày đến diễn đàn: : 01/08/2009
Tuổi : 35
Đến từ : Thiên Đường

https://thuhuong.forumvi.net

Về Đầu Trang Go down

[Chia sẻ] Cách sử dụng Snippet tăng tốc độ gõ lệnh trong C# Empty Code Snippets in Visual Studio 2010

Bài gửi by nth 28/04/11, 11:08 am

Visual Studio allows developers to save snippets of code which can be inserted at a later time. This saves on retyping blocks of code that are used often. I also find it very useful when I have to show code during presentations. Instead of typing everything live, I find it far easier to simply add the code block by block using code snippets.

VS 2010 improves the snippet functionality, and makes it easier to create code snippets. There’s two types of snippets:
- Expansion snippets are inserted at the cursor
- SurroundsWith snippets wraps around selected code
Creating a Custom Snippet:
Let’s go ahead and create a try-catch-finally Expansion snippet:
- Insert a new XML file to the project and call it TryCatchFinally.snippet. Make sure the file name ends with .snippet
- Right-click on the editor window and select Insert Snippet->Snippet. This creates a basic XML snippet template as shown below:
<CodeSnippet Format="1.0.0"
xmlns="[You must be registered and logged in to see this link.]">
<Header>
<Title>title</Title>
<Author>author</Author>
<Shortcut>shortcut</Shortcut>
<Description>description</Description>
<SnippetTypes>
<SnippetType>SurroundsWith</SnippetType>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>name</ID>
<Default>value</Default>
</Literal>
</Declarations>
<Code Language="XML">
<![CDATA[<test>
<name>$name$</name>
$selected$ $end$</test>]]>
</Code>
</Snippet>
</CodeSnippet>

- As you can see, by default the template contains both snippet types. Since I am creating an Expansion snippet, let’s go ahead and get rid of the SurroundsWith tag.
- Update the Title to "Try Catch Finally", Shortcut to "trycf" and Description to "Adds a try-catch-finally block".
- Literal tag enables us to define editable values that are inserted into the snippet. In the try-catch-finally snippet, we want to allow the user to change the type of the Exception being caught. The ID tag is the name of the editable value so change it to "ExceptionName". The Default tag specifies the default value for our editable field. I want to catch all exceptions, so change the Default tag’s value to "Exception".
- Finally, the code section contains what will be added when the snippet is inserted. Change the language to "CSharp", and the body to the following:
try
{

}
catch($ExceptionName$)
{

}
finally
{

}

- The final version of the TryCatchFinally.snippet file is shown below. For more details about the XML schema, take a look at the MSDN code snippets Schema Reference
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippet Format="1.0.0"
xmlns="[You must be registered and logged in to see this link.]">
<Header>
<Title>Try Catch Finally</Title>
<Author>umair</Author>
<Shortcut>trycf</Shortcut>
<Description>Adds a try-catch-finally block</Description>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>ExceptionName</ID>
<Default>Exception</Default>
</Literal>
</Declarations>
<Code Language="CSharp">
<![CDATA[
try
{

}
catch($ExceptionName$)
{

}
finally
{

}
]]>
</Code>
</Snippet>
</CodeSnippet>

Loading the Snippet into Visual Studio:
There are two ways to insert the above snippet into Visual Studio.

The most straightforward way is to put the .snippet files in VS 2010's code snippets directory. The default location for it is C:\Users\\Documents\Visual Studio 2010\Code Snippets\. This folder contains sub-directories based on the language of the snippet. For my example, the snippet goes under Visual C#. Once put here, VS 2010 automatically picks up the snippets without the need to restart.

The second option is to import the .snippet file into Visual Studio.
- Inside Visual studio, go to Tools->Code Snippets Manager (Ctrl+K,Ctrl+B). This brings up the Snippets Manager.
- Press Import button, and navigate to the location where the snippet was saved. Select the .snippet file.
- Press OK.
Using the Code Snippet:
Type the name of the snippet (the shortcut), and pressing tab expands it.
[You must be registered and logged in to see this image.]
You can also press Ctrl+K and Ctrl+X which brings up the "Insert Snippet" menu as shown below:
[You must be registered and logged in to see this image.]
We can navigate to My Code Snippets, and select the TryCatchFinally that we’ve just added:
[You must be registered and logged in to see this image.]

Copyright: [You must be registered and logged in to see this link.]
nth
nth
Admin
Admin

Tổng số bài gửi : 550
Số điểm : 1113
Số lần được cám ơn : 33
Ngày đến diễn đàn: : 01/08/2009
Tuổi : 35
Đến từ : Thiên Đường

https://thuhuong.forumvi.net

Về Đầu Trang Go down

[Chia sẻ] Cách sử dụng Snippet tăng tốc độ gõ lệnh trong C# Empty Re: [Chia sẻ] Cách sử dụng Snippet tăng tốc độ gõ lệnh trong C#

Bài gửi by Sponsored content


Sponsored content


Về Đầu Trang Go down

Về Đầu Trang

- Similar topics

 
Permissions in this forum:
Bạn không có quyền trả lời bài viết