Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何测试使用依赖属性的转换器?_C#_.net_Windows 8.1_Dependency Properties_Ivalueconverter - Fatal编程技术网

C# 如何测试使用依赖属性的转换器?

C# 如何测试使用依赖属性的转换器?,c#,.net,windows-8.1,dependency-properties,ivalueconverter,C#,.net,Windows 8.1,Dependency Properties,Ivalueconverter,由于Windows 8.1应用程序不支持多值转换器,因此我选择使用依赖项属性。但后来,当我测试转换器并实例化它时,我得到以下错误: 有人知道如何测试这种转换器吗 以下是源代码的samplest版本: [TestClass] public class ConverterWithDependencyPropertyTests { [TestMethod] public void UnitTest() { var converter = new BinCode

由于Windows 8.1应用程序不支持多值转换器,因此我选择使用依赖项属性。但后来,当我测试转换器并实例化它时,我得到以下错误:

有人知道如何测试这种转换器吗

以下是源代码的samplest版本:

[TestClass]
public class ConverterWithDependencyPropertyTests
{
    [TestMethod]
    public void UnitTest()
    {
        var converter = new BinCodeToBinQuantityConverter();
    }
}

最后,我可以这样做:

    [TestMethod]
    public async Task Converter_With_Dependency_Property_Test()
    {
        var expectedResult = 3;
        var valueConverted = 0;

        await ExecuteOnUIThread(() =>
        {
            _converterWithDependencyProperty = new ConverterWithDependencyProperty();
            _converterWithDependencyProperty.DependencyProperty = _dummyValue;
            valueConverted = (int)_converterWithDependencyProperty.Convert(_dummyValueToConvert, typeof(int), null, null);
        });

        Assert.AreEqual(expectedResult, valueConverted);
    }

    public static IAsyncAction ExecuteOnUIThread(DispatchedHandler action)
    {
        return CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, action);
    }

你能发布你的测试代码吗?您似乎是从非UI线程访问转换器。我刚刚用一个简单版本的测试代码更新了这个问题,该测试代码引发了异常:-)BinCodeToBinQuantityConverter做什么?根据异常,他试图从后台线程访问UI控件。为此使用
Dispatcher
。正如我所说,我在这个转换器中使用了一个dependency属性,因此,这个转换器继承自DependencyObject。如果我删除了依赖属性相关的源代码,当我试图调用转换器时会出现任何异常,但我需要使用它。。。