UnitTest

时间:2017-01-09 10:02:56

标签: c# wpf unit-testing

我正在尝试使用以下命令设置wpf控件的焦点:

[TestClass]
[TestFixture, Apartment(ApartmentState.STA)]
public class OnEnter_MoveFocus_Test
{
    [TestInitialize]
    public void TestInitialize()
    {
        if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
        {
            throw new ThreadStateException("The current threads apartment state is not STA");
        }
    }
    [TestMethod]
    public void TestMoveFocus()
    {
        TextBox TextBox1 = new TextBox() { Name = nameof(TextBox1), Focusable=true };
        var result = TextBox1.Focus(); //results in false
        Keyboard.Focus(TextBox1);
    }
}

TextBox1.Focus();返回false(显然TextBox1可以关注并启用),使用Keyboard.Focus(TextBox1);时,Keyboard.FocusedElement仍为null

我该如何解决这个问题?

0 个答案:

没有答案