【发布时间】:2015-10-24 12:33:41
【问题描述】:
我收到InvalidOperationException。此代码在线程中运行。我已将其委托给 UI 线程来处理 UI。是不是因为我的资源问题?
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri("pack://application:,,,/LiarDice;component/" + imagePath);
logo.EndInit();
currentGuessDice.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
new Action(delegate()
{
currentGuessDice.Source = logo;
}));
我稍微更改了我的代码,现在错误变为:
Part URI cannot end with a forward slash.
新代码:
currentGuessDice.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
new Action(delegate()
{
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri("pack://application:,,,/LiarDice;component/" + imagePath);
logo.EndInit();
currentGuessDice.Source = logo;
}));
【问题讨论】:
-
是什么让您认为它与多线程或线程(UI)有关?异常清楚地表明这是 URI 解析错误。
-
在我编辑代码并将整个初始化放在 Dispatcher 中之前,它出现了 InvalidOperationException。
标签: c# wpf multithreading xaml