我剛剛瞭解到Nuget包Loop Helper for WebMatrix。不幸的是,我似乎無法找到任何文檔或項目網站。有沒有人有關於如何使用此項目或項目網站的信息?教程使用循環助手的WebMatrix
回答
我認爲所提供的樣品幾乎涵蓋了你需要知道的一切,循環跟蹤器並不像你想象的那樣神祕。
所有Loop.OuterLoop
做,如果你在一個是被引用父環,它的被跟蹤,例如:
@{
int[] outerThings = new int[] {1,2,3,4};
var innerThings = new[] {"a","b","c", "d"};
foreach (int outerThing in outerThings.Track())
{
<li>@outerThing
@foreach(var innerThing in innerThings.Track())
{
<li>---->@innerThing
@if (Loop.OuterLoop.First)
{
<strong>Start of outer loop (index is: @Loop.OuterLoop.Index)</strong>
}
@if (Loop.OuterLoop.Last)
{
<strong>end of outer loop (index is: @Loop.OuterLoop.Index)</strong>
}
</li>
}
</li>
}
}
引擎蓋下的幫手只是包裝你的數組,列表和集合訪問並通過遞增的內部索引值跟蹤這些項目的向前移動。
更新:
下面是使用三個嵌套循環與非常內環引用最外環(outerThings
)的示例:
@{
int[] outerThings = new int[] {1,2,3,4};
var middleThings = new[] { "A", "B", "C", "D" };
var innerThings = new[] { "a", "b", "c", "d" };
foreach (int outerThing in outerThings.Track())
{
<li>@outerThing
@foreach(var middleThing in middleThings.Track())
{
<li>---->@middleThing
@foreach(var innerThing in innerThings.Track())
{
<li>-----+---->@innerThing
@if (Loop.OuterLoop.OuterLoop.First)
{
<strong>
Start of outerThing loop (index is: @Loop.OuterLoop.OuterLoop.Index)
</strong>
}
@if (Loop.OuterLoop.OuterLoop.Last)
{
<strong>
End of outerThing loop (index is: @Loop.OuterLoop.OuterLoop.Index)
</strong>
}
</li>
}
</li>
}
</li>
}
}
如果有第三個循環會發生什麼? – 2011-05-12 12:56:26
@chris - 我添加了另一個使用三個嵌套循環的例子。您只需通過添加一個額外的'OuterLoop'來引用內部循環的外部循環,例如'Loop.OuterLoop.OuterLoop' – Kev 2011-05-12 13:14:30
有人說,我敢打賭,編程這個屬性並不好玩。 – 2011-05-12 13:43:49
跟蹤OuterLoop並不完全困難。該幫助器將其狀態存儲在確保請求級別隔離的Context.Items中。每次跟蹤一個新循環時,它都會檢查Context.Items中是否存在一個值,並將其作爲OuterLoop屬性分配給新循環。當枚舉器被放置時,Context.Items中的值被設置爲OuterLoop,從而有效地彈出當前循環。
foo.Track()
fooWrapper.OuterLoop = Content.Items["Loop"] // null at this point.
Context.Items["Loop"] = fooWrapper
bar.Track()
barWrapper.OuterLoop = Content.Items["Loop"] // fooWrapper
Context.Items["Loop"] = barWrapper
bar.Dispose()
Context.Items["Loop"] = barWrapper.OuterLoop // Current in fooWraper
這意味着你可以有任何深度的循環,包括跨越頁面的循環,它仍然可以工作。
注意,助手將不會在一個多線程的情況下工作,如果你無法處理枚舉可能工作不正常。
我對你在處理枚舉數時提出的陳述感到困惑,沒有一個代碼示例顯示處理枚舉數。 'Wrap()'從哪裏來? – 2011-05-12 15:02:52
@Chris我的不好,我的意思是說跟蹤但寫了換行。我在迴應中改變了這一點。此外,IEnumerator
在第二行到最後的代碼行應該是bar.Dispose()? – 2011-05-12 16:55:49
- 1. WebMatrix中:使用@FileUpload助手
- 2. 在桌面應用程序中使用WebMatrix助手
- 3. 在普通的ASP.NET MVC項目中使用Webmatrix助手
- 4. 循環中的路徑助手
- 5. 蘋果幫助手冊教程
- 6. 在助手內使用Meteor.call時的無限循環
- 7. 什麼時候使用傳統的循環數組助手
- 8. CodeIgniter教程中foreach循環的錯誤
- 9. dust.js - 我可以使用助手循環逗號分隔值嗎?
- 10. 使用循環的初學Python幫助
- 11. WebMatrix - 在foreach循環內計數
- 12. 新手Java程序員需要幫助涉及循環
- 13. 天文學助手Java菜單驅動程序與循環
- 14. 爲什麼使用WebMatrix Facebook助手verus只使用Facebook生成器?
- 15. 把手內環路助手
- 16. 第一個插件幫助 - 通過windows循環循環使用for循環
- 17. 幫助循環,存儲和使用CGPoint
- 18. DXL門需要幫助使用循環
- 19. 循環賽請教我
- 20. 新手需要關於Python教程的幫助
- 21. 幫助PHP循環
- 22. While循環幫助
- 23. C#循環幫助
- 24. While循環幫助
- 25. 幫助foreach循環
- 26. 新手while循環
- 27. PHP foreach循環的幫助
- 28. foreach循環的幫助
- 29. (幫助)的SqlTransaction循環
- 30. tkinter中的循環 - 幫助!
FWIW我已經看到它添加到項目中的cshtml文件,但他們沒有真正討論任何關於'Loop.OuterLoop'的問題。 – 2011-05-11 18:39:52