Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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# WPF更改URI异步_C#_Wpf_Asynchronous - Fatal编程技术网

C# WPF更改URI异步

C# WPF更改URI异步,c#,wpf,asynchronous,C#,Wpf,Asynchronous,我的页面中有一个名为LinkURL的文本框。我试图用它的文本值创建Uri。并使用BitmapImage的Uri。因此,用户可以在键入URL时看到图像 但我的UI冻结,应用程序停止运行。我想用它来预览图像 public void LoadImage(object sender, TextChangedEventArgs e) { Uri uri = null; Dispatcher.BeginInvoke(new Action(delegate { uri

我的页面中有一个名为
LinkURL
的文本框。我试图用它的文本值创建Uri。并使用
BitmapImage
的Uri。因此,用户可以在键入URL时看到图像

但我的UI冻结,应用程序停止运行。我想用它来预览图像

public void LoadImage(object sender, TextChangedEventArgs e)
{
    Uri uri = null;
    Dispatcher.BeginInvoke(new Action(delegate
    {
        uri = new Uri(this.LinkURL.Text);
        WebClient wc = new WebClient();
        wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
        wc.OpenReadAsync(uri, wc);
    }));
}

private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.StreamSource = e.Result;
    this.Preview.Source = bi;
}  
我试过了,但没用


UI冻结并向我提供
系统。反射。TargetInvocationException
ArgumentNullException

“没有工作”并不是什么好事情。你能说得更具体一点吗?你忘了叫Endinit,你的BeginInvoke也没用;代码仍将在UI线程上执行。仅仅使用OpenReadAsync通常就足以避免阻塞UI。(我说“通常”是因为OpenReadAsync会同步执行一些操作,比如DNS解析)为了添加到@ThomasLevesque的注释中,
Dispatcher.BeginInvoke
无助于避免UI阻塞,如果
DNS解析在
OpenReadAsync
内部同步进行,它只会延迟它。检查解决方案。