XamlParseException for some certain StaticResource on some certain computers - wpf

We are developing a WPF application for .NET 4.
One day the client told me that the new release does not work (application just quits upon startup) on his Windows 7 machine and he found the exception log in the Windows Event Viewer:
Application: myapp.exe Framework Version: v4.0.30319 Description:
The process was terminated due to an unhandled exception. Exception Info: System.Windows.Markup.XamlParseException
Stack: at System.Windows.Markup.XamlReader.RewrapException(System.Exception, System.Xaml.IXamlLineInfo, System.Uri)
at System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader, System.Xaml.IXamlObjectWriterFactory, Boolean, System.Object, System.Xaml.XamlObjectWriterSettings, System.Uri)
at System.Windows.Markup.WpfXamlLoader.LoadBaml(System.Xaml.XamlReader, Boolean, System.Object, System.Xaml.Permissions.XamlAccessLevel, System.Uri)
at System.Windows.Markup.XamlReader.LoadBaml(System.IO.Stream, System.Windows.Markup.ParserContext, System.Object, Boolean)
at System.Windows.Application.LoadBamlStreamWithSyncInfo(System.IO.Stream, System.Windows.Markup.ParserContext)
at ... (I guess no point to continue...)
This problem is driving me crazy because nor me nor another developer cannot reproduce it on our machines, and we cannot reproduce it even on a clean Windows 7 install in VirtualBox.
When we tried to find the changes which broke the application for the client, we found the offending piece. Here is the difference:
This works fine:
<ComboBox x:Name="comboBoxZoom" Margin="130,10,0,0" HorizontalAlignment="Left" Width="40" FontFamily="Arial" FontSize="12" VerticalAlignment="Top" TabIndex="1" Panel.ZIndex="2" />
This breaks:
<ComboBox x:Name="comboBoxZoom" Style="{StaticResource comboBoxStyle}" Margin="130,10,0,0" HorizontalAlignment="Left" Width="40" FontFamily="Arial" FontSize="12" VerticalAlignment="Top" TabIndex="1" Panel.ZIndex="2" />
Essentially the difference is only
Style="{StaticResource comboBoxStyle}"
but ths style has been used in other parts of our application before without any issues! And the same file which causes this exception, contains many other Style="{StaticResource someotherstyle}" and they work fine on the client machine.
All these styles are located in one file ControlStyles.xaml. The comboBoxStyle is a modified style copied from some website, similar to this one:
http://social.msdn.microsoft.com/Forums/nl/wpf/thread/53134b87-1a99-4998-a1fb-b3d8a9bd2773
Why does Style="{StaticResource comboBoxStyle}" make my app crash only on some certain machines and how to fix this bug?

I had the same problem and after pulling my hair out for two days, I figured the solution using Windbg with the help of someone in the C# IRC channel.
So in my case, the exception was thrown because I hadn't set the type attribute for app.config's trace listeners, and it had nothing to do with XAML, although oddly enough when I removed the combobox binding in my XAML, I could run the program in release mode, but the real solution lied in the app.config. I could run the program fine in debug mode though. This sounds like a nasty bug with the compiler.
So look for possible oversights in your app.config, if everything seems right there, then just grab Windbg and debug it on your own until you find the root cause of the exception.

Related

The resource could not be resolved. (DynamicResource)

I used this tutorial to implement a multilingual WPF application. Everything compiles, runs and works. But there is a problem: whenever I try to refer to a DynamicResource:
<TextBlock Grid.Row="1" Grid.Column="0" Text="{DynamicResource subtimer}" />
Visual Studio (2015) shows me a warning: The resource could not be resolved. It does not prevent working but it is just annoying and distracting (50 warnings for my smallest app).

Why does my app ignore certain xaml code on Windows 7?

I'm creating a WPF C# .Net App with the Aero theme which is supposed to run on Win7 as well as Win10.
On Win10 everything works as it's supposed to, however on Win7 the GUI looks vastly different.
In particular, my MenuItems simply seem to ignore certain settings such as VerticalContentAlignmentand BorderThickness.
Pics:
Win7 vs
Win10
How do I fix this?
(Some Code:)
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
<MenuItem Header="Intranet"
Grid.Column="2"
x:Name="Menu_Intranet"
ToolTip="Intranet"
BorderBrush="white"
BorderThickness="5"
VerticalAlignment="Stretch"
Click="Menu_1_Click"
ToolTipService.ShowOnDisabled="true"
VerticalContentAlignment="Center"/>
Thanks in advance!
I edited the MenuItemControlTemplate (right click on the control you want to edit in the designer -> edit template) and applied it to the MenuItems via Template="{DynamicResource MyMenuItemControlTemplate}".
I guess this leaves no room for the OS/Theme to apply its own settings.

WPF Font Ms Gothic in .net 4.0 doesn't look the same in .net 3.0

I moved from .Net Framework 3.0 to 4.0 and the Japanese (and English as well) text became clear and lost weight
I want it back as it was in .Net 3
photo: new = .Net 4, old = .Net 3
I've followed this blog trying to fix it but no success yet
tried the following with different combinations:
<TextBlock Text="{Binding Text}" FontSize="16" Foreground="White" FontFamily="MS Gothic" RenderOptions.ClearTypeHint="Enabled" />
<TextBlock Text="{Binding Text}" FontSize="16" Foreground="White" FontFamily="MS Gothic" TextOptions.TextFormattingMode="Ideal"/>
<TextBlock Text="{Binding Text}" FontSize="16" Foreground="White" FontFamily="MS Gothic" TextOptions.TextRenderingMode="ClearType"/>
Any suggestions how to solve this?
It is not a solution, but on Microsoft's forum, there was an answer that the thicker font display is "caused by internal font optimization of WPF rendering system" for two fonts, MS UI Gothic and Bold MS UI Gothic.
As there are no solutions suggested there, I assume that you can not avoid that optimization. If MS UI Gothic are not MUST for what you are making, you may use Meiryo or other Japanese fonts to work around it.
This is also not a solution but I had a similar issue in that I wanted to use a particular font which was not available on Windows XP, but was in Vista and Win 7. I found an open source compatible font and included it as a resource in my application. This way I ensured that the application font looked the same across all versions of Windows.
You may be able to find a font that you could include in your application.

Why does Visual Studio 2010 crash when I add a binding to a TextBlock?

When I change my code from:
<TextBlock x:Name="HeaderText" Style="{StaticResource HeaderTextStyle}"
Text="My Page Header"/>
to:
<TextBlock x:Name="HeaderText" Style="{StaticResource HeaderTextStyle}"
Text="{Binding Path=ModuleStrings.Package_Name, Source={StaticResource ResourceWrapper}}"/>
Every single time! If I change it outside of the IDE, it crashes when I load up the file.
Context:
I'm using Visual Studio 2010 and .Net 4.0. I'm writing a Silverlight application using the Silverlight Business Application template. I have several modules that I'm discovering using MEF and downloading dynamically. This code exists in a Silverlight RIA Services Class Library that I'm using as a module.
Edit:
Problem signature:
Problem Event Name: APPCRASH
Application Name: devenv.exe
Application Version: 10.0.30319.1
Application Timestamp: 4ba1fab3
Fault Module Name: unknown
Fault Module Version: 0.0.0.0
Fault Module Timestamp: 00000000
Exception Code: c00000fd
Exception Offset: 0fd84cf0
OS Version: 6.1.7600.2.0.0.256.1
Locale ID: 1033
Additional information about the problem:
LCID: 1033
In order to provide a preview of the UI the XAML editor actually executes the coding of your bound getter within VS2010. If your getter depends on other software components which are not correctly initialised because the application is not actually running, your getter will possibly throw an exception.
Normally VS2010 will show this exception together with its stack trace in a yellow header line above the UI designer. In many cases VS2010 seems not capable to cope with this exception, with the result that the whole IDE crashes.
My approach in these cases is to surround the whole coding of the getter (in your example ResourceWrapper.ModuleStrings.Package_Name) with try/catch and show an Exception through via MessageBox.Show(). So you will at least be able to see the original cause of the error after a rebuild.
(There is also the possibility to debug VS itself with a second instance of the IDE...)
Then, when I located the error, in most cases it help to exclude some coding during design-time (i.e. when running in XAML editor and not as application)
// Detect if this coding is run by the IDE or within an actual application
bool inDesignMode = (bool) DesignerProperties.IsInDesignModeProperty.GetMetadata( typeof(DependencyObject) ).DefaultValue;
if(!inDesignMode)
{
// ... We are running as application ...
}
HTH

Weird UserControl image path behaviour in Blend 4 and WPF 4.0

I'm using Blend 4 and VisualStudio 2010 to build a kiosk WPF 4.0 app that has a wizard-like function. I've decided I'd like individual steps moved to separate UserControls, to keep the main window of the application as lean as possible. However, I'm having trouble including the UserControl into the main window, since image paths are being mangled after moving the XAML to a separate UserControl file.
This code works when it's put into the main window XAML:
<Button Grid.Row="1" Grid.Column="2" Height="{Binding ActualWidth, RelativeSource={RelativeSource Self}}" Margin="20" VerticalAlignment="Bottom" Style="{DynamicResource ImageButton}">
<Button.Background>
<ImageBrush ImageSource="/Images/Extra/motor_boat_2.jpg" Stretch="UniformToFill"/>
</Button.Background>
</Button>
But when I move that code to a separate user control and include it in the main window, the designer throws and exception:
IOException: Cannot locate resource 'images/extra/sailing_boat.jpg'.
I've tried using a couple of different syntaxes, but all fail, sometimes using the %ProgramFiles% directory, other times converting the absolute path to relative (as above). Sometimes, the Build Project operation in Blend 4 fails because of this.
All the images are marked as Resource with Build Action set to Copy Always.
VisualStudio has no problem building and running the application, the UserControl displays correctly within the running application with no thrown exceptions.
EDIT:
Using relative paths in the UserControl and then building and running the app from VisualStudio and switching back to Blend correctly displays the images, until the next modification to the UserControl.
Use Pack URI like following:
Uri uri = new Uri("pack://application:,,,/Images/Extra/motor_boat_2.jpg");

Resources