Tôi có một cửa sổ đơn giản với tham chiếu đến một StaticResource trong App.xaml.Kiểm tra cửa sổ WPF với StaticResources
định nghĩa tài nguyên App.xaml:
<!-- Standard Text Box Style -->
<Style x:Key="textBoxStyleStd" TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="14" />
</Style>
componets Window bằng cách sử dụng tài nguyên:
<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}}"/>
Khi cố gắng để kiểm tra đơn vị Window này tôi nhận được lỗi:
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.
Is có cách nào xung quanh điều này? Mã kiểm tra đơn vị của tôi là:
[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();
}
Giải pháp có thể có thể thay thế StaticResource với liên kết DynamicResource – Boogier