Blog Archives
system.windows.resourcedictionary. source threw an exception wpf c#
In general this issue is caused when a referenced resource cannot be found.
<Application x:Class="AppName.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
In my specific circumstance, as the xaml above details, this issue was caused by a reference to PresentationFramework.Aero DLL that was not contained in the output bin folder of the compiled project. Under references, setting Copy Local to true did the trick for me.
the calling thread cannot access wpf win form c# multi threading
If you receive this error:
The calling thread cannot access this object because a different thread owns it
It is a simple fix to add in some common multi-threading invocation mechanisms.
WPF:
//with delegate declared public delegate mydelegate; this.Dispatcher.Invoke(new mydelegate(funcname)); this.Dispatcher.Invoke((mydelegate)delegate() { funcname(); }); //does not require delegate declared, i usually prefer this approach but there are situations for either this.Dispatcher.Invoke((Action)delegate() { funcname(); }); this.Dispatcher.Invoke((Action)(()=>{ funcname();}));
WinForms:
this.Invoke(funcname, params); this.BeginInvoke(funcname, params);
WPF Quick Reference
get window handle
IntPtr windowHandle = new WindowInteropHelper(this).Handle;
bring to front
myWindow.Activate(); myWindow.TopMost = true;
common resource dictionaries for skinning (this blog)
References
wpf get window handle (stackoverflow) http://stackoverflow.com/questions/1556182/finding-the-handle-to-a-wpf-window
wpf bring to front (stackoverflow) http://stackoverflow.com/questions/257587/bring-a-window-to-the-front-in-wpf
Common WPF Resource Dictionaries
Skinning a WPF application is as simple as adding an assembly reference (PresentationFramework.Aero) and xml config change. See below.
<Application.Resources> <Application.Resources> <ResourceDictionary> <!-- --> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application.Resources>
Other sources:
<ResourceDictionary Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml"/> <ResourceDictionary Source="/PresentationFramework.Classic;component/themes/Classic.xaml"/> <ResourceDictionary Source="/PresentationFramework.Royale;component/themes/Royale.NormalColor.xaml"/> <ResourceDictionary Source="/PresentationFramework.Luna.Homestead;component/themes/Luna.Homestead.xaml"/> <ResourceDictionary Source="/PresentationFramework.Luna.Metallic;component/themes/Luna.Metallic.xaml"/> <ResourceDictionary Source="/PresentationFramework.Zune;component/themes/Zune.NormalColor.xaml"/>
References
StackOverflow, http://stackoverflow.com/questions/2075720/windows-7-theme-for-wpf
Silverlight Toolkit Default Path
Quick google search turned up nothing for this, so did a little digging on my own.
This path is useful/necessary if you want to manually utilize anything in the toolkit outside of Hilverlight, such as the themes, which are also compatible with WPF.
C:\Program Files\Microsoft SDKs\Silverlight\v4.0\Toolkit
URL Decode in WPF
Crossing from web to forms development you may notice System.Web is not available. You could extract it from the GAC, but would suffer from having to manually update it moving forward.
One solution on the web suggested using Microsoft.XSS library (which is up to version 4.0 at the time of this article).
This would work, but there are some differences in string conversion, especially regarding the “+” and “~” character between using the Web UrlEncode/UrlDecode found in the XSS library or using the Uri method illustrated below.
See references for links to XSS and/or information regarding the differences on how these strings are encoded differently with each method.
Uri videouri = new Uri(AppDomain.CurrentDomain.BaseDirectory + "../../Videos/directory/player.htm"); string videourl = Uri.UnescapeDataString(videouri.ToString()); webBrowser1.Navigate(videourl); //can actually accept uri or string
References
Microsoft Anti-XSS Library 4.0, http://www.microsoft.com/downloads/en/details.aspx?FamilyID=F4CD231B-7E06-445B-BEC7-343E5884E651
Nerdbank, http://blog.nerdbank.net/2009/05/uriescapedatapath-and.html
StackOverflow, http://stackoverflow.com/questions/36315/alternative-to-httputility-for-net-3-5-sp1-client-framework