2011-09-21 10 views
7

के साथ मिश्रित मैं रेजर सिंटैक्स में कोई गुरु नहीं हूं, लेकिन एक फ्लैंट-स्टाइल à là Telerik के GUI घटकों का उपयोग करके एक सामान्य लाइब्रेरी बनाने की कोशिश कर रहा हूं।रेजर Func <ऑब्जेक्ट, ऑब्जेक्ट> MvcHtmlString

public static MyBox Box(this HtmlHelper helper) 
{ 
    return new MyBox(helper.ViewContext); 
} 

और:

/// <summary> 
/// http://geekswithblogs.net/shaunxu/archive/2010/04/10/lt-gt-htmlencode-ihtmlstring-and-mvchtmlstring.aspx 
/// </summary> 
public class MyBox : IHtmlString 
{ 
    private readonly ViewContext _viewContext; 
    private string _content; 
    private string _title; 

    public MyBox(ViewContext viewViewContext) 
    { 
     _viewContext = viewViewContext; 
    } 

    /// <summary> 
    /// See: http://haacked.com/archive/2011/02/27/templated-razor-delegates.aspx 
    /// </summary> 
    /// <param name="value"></param> 
    /// <returns></returns> 
    public MyBox Content(Func<object, object> value) 
    { 
     _content = value.DynamicInvoke(_viewContext).ToString(); 
     return this; 
    } 

    /// <summary> 
    /// See: http://haacked.com/archive/2011/02/27/templated-razor-delegates.aspx 
    /// </summary> 
    /// <param name="values"></param> 
    /// <returns></returns> 
    public MyBox Content(params Func<object, object>[] values) 
    { 
     foreach (var value in values) 
     { 
      _content += value.DynamicInvoke(_viewContext).ToString(); 
     } 
     return this; 
    } 

    public MyBox Content(MvcHtmlString content) 
    { 
     _content = content.ToString(); 
     return this; 
    } 

    public MyBox Title(string title) 
    { 
     _title = title; 
     return this; 
    } 

    public string ToHtmlString() 
    { 
     using (var stringWriter = new StringWriter()) 
     { 
      WriteHtml((TextWriter)stringWriter); 
      return stringWriter.ToString(); 
     } 
    } 

    public void Render() 
    { 
     using (var writer = new HtmlTextWriter(_viewContext.Writer)) 
      WriteHtml(writer); 
    } 

    protected virtual void WriteHtml(TextWriter writer) 
    { 
     writer.WriteLine("<!-- START BOX -->"); 
     writer.WriteLine("<h1>" + _title + "</h1>)); 
     writer.WriteLine(_content); 
     writer.WriteLine("<!-- END BOX -->"); 
    } 
} 

मैं भी एचटीएमएल विस्तार तरीकों कि MvcHtmlString की वापसी का एक सेट है

मैं निम्नलिखित टुकड़े (लगभग) है। एक उदाहरण (सरलीकृत) है:

public static class GuiElementExtensions 
    { 
     private const string FieldContainerHeadingTemplate = @" 
<tr><th style=""text-align:left"" colspan=""2"">{0}</th></tr> 
"; 

    public static MvcHtmlString GuiFieldContainerHeading(this HtmlHelper helper, string text) 
     { 
      return new MvcHtmlString(String.Format(FieldContainerHeadingTemplate, text)); 
     } 
} 

फिर, मेरी .cshtml फ़ाइल में, मैं निम्न कार्य करें:

@using (Html.BeginForm()) 
     { 
      @(Html.Gui() 
         .Box() 
         .Title("Hello World!") 
         .Content(
           @<h1>Hello World! This is the cool Gui.Box</h1> 
         ) 
     ) 
} 

कौन सा public MyBox Content(Func<object, object> value) कहता है, और काम करता है। इसी प्रकार, जब मैं निम्न प्रयास करें:

@using (Html.BeginForm()) 
     { 
      @(Html.Gui() 
         .Box() 
         .Title("Hello World!") 
         .Content(
           Html.GuiFieldContainerHeading("SubHeading 1") 
         ) 
     ) 
} 

यह खुशी से public MyBox Content(MvcHtmlString content) कॉल करता है और अपेक्षा के अनुरूप काम करता है।

लेकिन, हालांकि, जब मैं निम्नलिखित करने का प्रयास करता हूं, तो मैं अपने सिर को लपेट नहीं सकता कि रेजर कंपाइलर इंजन कैसे काम करता है। मैं इसे कैसे

  • @<h1>Hello World! This is Gui().Box()</h1> की राशि (जो एक समारोह है)
  • Html.GuiFieldContainerHeading("SubHeading 1") (जो एक MvcHtmlString है)

वापस जाने के लिए या तो एक वस्तु के रूप में मिलता है (यह एक समारोह, MvcHtmlString हो ?, जो कुछ भी है, या वस्तुओं की एक सूची मैं अपने MyBox कक्षा में सामग्री कार्य करने के लिए पैरामीटर सूची के अंदर सामान्य उस्तरा वाक्य रचना लिखने के लिए, इस तरह करना चाहते हैं:

@using (Html.BeginForm()) 
     { 
      @(Html.Gui() 
         .Box() 
         .Title("Hello World!") 
         .Content(
           @<h1>Hello World! This is Gui().Box()</h1> 
           Html.GuiFieldContainerHeading("SubHeading 1") 
       Html.TextBoxFor(model => model.Name) 
       @<h2>Hello there!</h2> 
         ) 
     ) 
} 

क्या मैं सही रास्ते पर हूं, या क्या मैं चाहता हूं कि ऐसा करने का एक आसान तरीका है? मैं एक सामान्य डीएलएल में हमारे सिस्टम में सभी "सामान्य गुई तत्व" इकट्ठा करना चाहता हूं, इसलिए मेरे संगठन में हर डेवलपर को प्रत्येक परियोजना पर पहिया को फिर से शुरू करने की आवश्यकता नहीं है।

किसी भी मदद की सराहना की।


ठीक है, अगले समस्या:

मैं एक कंटेनर में बॉक्स सामान्यीकृत, और दो उपवर्गों, बॉक्स और HighlightArea बनाया है। हालांकि, निम्नलिखित कोड का उपयोग कर, रेजर संकलक मुझे संदेश

इनलाइन मार्कअप ब्लॉक (@ < पी > सामग्री </p >) नेस्ट नहीं किया जा सकता के साथ शुरूआत की। इनलाइन मार्कअप के केवल एक स्तर की अनुमति है।

कोड काम नहीं कर रहा, यह है:

@(Html.Gui() 
       .Box() 
       .Title("BoxTitle") 
       .Content(@<text> 
         <h1>Hello World! This is the box content</h1> 
         @Html.GuiFieldContainerHeading("This is the heading") 
         @(Html.Gui().HighlightArea() 
           .Content(
           @Html.ValidationSummary() 
           @<h1>Jalla</h1> 
          ) 
         ) 
         </text> 
       ) 

हम इस के लिए एक समाधान है? या मेरा दृष्टिकोण संभव नहीं है?

+1

आपका उदाहरण 'यह है गुई()। बॉक्स()' थोड़ा उलझन में है। ऐसा लगता है कि इसमें कुछ सी # कोड शामिल हैं लेकिन शायद यह नहीं होना चाहिए। – Codo

+0

धन्यवाद। मैंने किसी भी भ्रम से बचने के लिए प्रश्न अपडेट किया है। भ्रम के अलावा सवाल पर कोई विचार? –

+0

अगली समस्या (धन्यवाद, बिल द लिज़र, प्रश्न को अद्यतन करने के लिए ...:)) ... उस पर कोई विचार (प्रश्न देखें) –

उत्तर

7

आप सब कुछ एक पैरामीटर में गठबंधन करने के लिए <text> टैग का उपयोग कर सकते हैं।

@using (Html.BeginForm()) 
{ 
    @(Html.Gui() 
     .Box() 
     .Title("Hello World!") 
     .Content(
      @<text> 
       <h1>Hello World! This is Gui.Box</h1> 
       @Html.GuiFieldContainerHeading("SubHeading 1") 
       @Html.TextBoxFor(model => model.Name) 
       <h2>Hello there!</h2> 
      </text> 
     ) 
    ) 
} 

मुझे लगता है कि आपका उदाहरण वास्तव में काम नहीं कर रहा है। अगर मैं गलत नहीं कर रहा हूँ आप इस तरह से काम कर रहा यह मिल सकता है:

@<h1>Hello World! This is Gui.Box</h1> 
+ Html.GuiFieldContainerHeading("SubHeading 1") 
+ Html.TextBoxFor(model => model.Name) 
+ @<h2>Hello there!</h2> 

लेकिन <text> टैग आसान लगता है।

+0

एक आकर्षण की तरह काम किया। धन्यवाद! :) –

संबंधित मुद्दे