2009-04-14 4 views
11

के साथ एक WPF विंडो का परीक्षण करना मेरे पास App.xaml में एक StaticResource के संदर्भ के साथ एक साधारण विंडो है।StaticResources

App.xaml संसाधन परिभाषा:

<!-- Standard Text Box Style --> 
<Style x:Key="textBoxStyleStd" TargetType="{x:Type TextBox}"> 
    <Setter Property="FontSize" Value="14" /> 
</Style> 

विंडो componets संसाधन का उपयोग कर:

System.Windows.Markup.XamlParseException: Cannot find resource named '{textBlockStyleStd}'. Resource names are case sensitive. Error at object 'stationIdTitle' in markup file 'Zpg;component/guicomponenets/screens/enterstationidscreen.xaml' Line 23 Position 71.

है:

<TextBlock Grid.Column="1" Grid.Row="0" Name="stationIdTitle" 
      Style="{StaticResource textBlockStyleStd}" 
      VerticalAlignment="Center" HorizontalAlignment="Center" 
      Text="{LocText Key=Title, Dict={StaticResource Dictionary}, 
       Assembly={StaticResource Assembly}}"/> 

इकाई परीक्षण करने की कोशिश कर जब यह विंडो मैं त्रुटि मिलती है इस के आसपास कोई रास्ता है? मेरा यूनिट टेस्ट कोड है:

[Test] 
public void TestEnterKeyPressedNoText() 
{ 
    IPickingBusinessObject pickingBusinessObject = mock.StrictMock<IPickingBusinessObject>(); 

    EnterStationIdScreen objectUnderTest = new EnterStationIdScreen(pickingBusinessObject); 

    Assert.AreEqual(Visibility.Visible, objectUnderTest.stationIdError.Visibility); 

    Assert.AreEqual("werwe", "oksdf"); 

    Replay(); 

    objectUnderTest.EnterKeyPressed(); 

    Verify(); 
} 
+0

संभव समाधान StaticResource को डायनामिक रिसोर्स बाध्यकारी – Boogier

उत्तर

12

धन्यवाद केंट,

मैं अपने सुझाव को देखा और सबसे परिदृश्यों में मैं मॉडल का इस्तेमाल किया जाना चाहिए और फिर भी परीक्षण किया सहमत हूँ, वहाँ नियंत्रण (जैसे पाठ बॉक्स दृश्यता) मैं अभी भी परीक्षण करना चाहता था के साथ जुड़े कुछ कोड है। इसे पाने के लिए आप अपने आवेदन का एक उदाहरण बना सकते हैं (लेकिन इसे प्रारंभ नहीं करते) और संसाधनों को मैन्युअल रूप से जोड़ें। इससे ऐप.एक्सएएमएल और बेस यूनिट टेस्ट में डुप्लिकेशंस होता है लेकिन इससे मुझे परीक्षणों को पूरा करने की अनुमति मिलती है।

 if (Application.Current == null) 
     { 
      App application = new App(); 

      #region Add Static Resources from the App.xaml 

      Style textBoxStyle = new Style(typeof(TextBox)); 
      textBoxStyle.Setters.Add(new Setter(TextBox.FontSizeProperty, 14d)); 

      Style textBlockStyle = new Style(typeof(TextBlock)); 
      textBlockStyle.Setters.Add(new Setter(TextBlock.FontSizeProperty, 14d)); 

      application.Resources.Add("TextBoxStyleStd", textBoxStyle); 
      application.Resources.Add("TextBlockStyleStd", textBlockStyle); 
      application.Resources.Add("TextBlockStyleError", textBlockStyle); 
      application.Resources.Add("Assembly", "Zpg"); 

      #endregion 
     }  
+0

+1 के साथ प्रतिस्थापित करने के लिए हो सकता है क्योंकि यह एक समान बाध्य से बाहर होने में मदद करता है .. धन्यवाद! – PaulJ

+4

में एक समान समस्या थी, बस ऐप कॉल करने के लिए ऐप कॉल करें। प्रारंभिक कॉम्पोनेंट()। संसाधन शब्दकोश पॉप्युलेट हो जाएगा। वास्तव में यह है कि मुख्य विधि क्या करती है। यह मुख्य विधि से स्निपेट है, आप ऐप को छोड़ सकते हैं। जैसा कि हम यहां यूनिट परीक्षण कर रहे हैं। [System.STAThreadAttribute()] [System.Diagnostics.DebuggerNonUserCodeAttribute()] सार्वजनिक स्थैतिक शून्य मुख्य() { WPFComboBox.App ऐप = नया WPFComboBox.App(); ऐप। प्रारंभिक कॉम्पोनेंट(); ऐप।भागो(); } – ioWint

+1

इसके अलावा, यदि आप डिफ़ॉल्ट रूप से 'UserControl' 'स्टाइल' को आधार पर स्थिर करने के लिए एक स्थिर संसाधन का उपयोग कर रहे हैं, जैसे'