質問

In my setup we have a wpf application where the app.xaml have some styles set into it. then this file have a merged dictionary with more styles in it. so far so good.

The application have couple windows that eventually open another window that is in another DLL in the same solution (our View). the windows in the view use basic {StaticResource MyResource} and they perfectly see the dictionary of the application project.

Now i have a button style template that contain a Path in the content, and X for a custom close button as per below :

 <Style x:Key="MetroCloseButton" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="#FFC10000"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="10 5"/>
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="Content">
            <Setter.Value>
                <Path Data="F1M54.0573,47.8776L38.1771,31.9974 54.0547,16.1198C55.7604,14.4141 55.7604,11.6511 54.0573,9.94531 52.3516,8.23962 49.5859,8.23962 47.8802,9.94531L32.0026,25.8229 16.1224,9.94531C14.4167,8.23962 11.6511,8.23962 9.94794,9.94531 8.24219,11.6511 8.24219,14.4141 9.94794,16.1198L25.8255,32 9.94794,47.8776C8.24219,49.5834 8.24219,52.3477 9.94794,54.0534 11.6511,55.7572 14.4167,55.7585 16.1224,54.0534L32.0026,38.1745 47.8802,54.0534C49.5859,55.7585 52.3516,55.7572 54.0573,54.0534 55.7604,52.3477 55.763,49.5834 54.0573,47.8776z" Stretch="Uniform" Fill="White" Width="12" Margin="0,0,0,0" ></Path>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Border x:Name="Border" CornerRadius="3" Background="{TemplateBinding Background}" BorderBrush="Black" SnapsToDevicePixels="True" BorderThickness="{TemplateBinding BorderThickness}" />
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="OpacityMask" Value="DarkRed"/>
                            <Setter Property="Margin" Value="2 1" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderThickness" Value="0"/>
                            <Setter TargetName="Border" Property="Background" Value="#FFFF3A3A"/>
                            <Setter Property="Foreground" Value="White"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="Border" Property="Background" Value="#FFEFEFEF"/>
                            <Setter TargetName="Border" Property="BorderBrush" Value="LightGray"/>
                            <Setter Property="Foreground" Value="#FF9E9E9E"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

In a window no matter if it's in the application project or the DLL view they use the following for applying the style :

<Button Name="cmdClose" Style="{StaticResource MetroCloseButton}" Margin="5" HorizontalAlignment="Right" Width="80" Click="cmdClose_Click"/>

Now the issue is that some computer this works perfectly. other computer when i open the window the first time it work like a charm, then you close the window and reopen it then it crash with an exception

Set property 'System.Windows.FrameworkElement.Style' threw an exception.

this error does NOT get catch by a try catch and doesn't show a message when running from the EXE. It fails on the Window wnd = new Window();

Now here's the catch, if i put the style into the <Window.Resource> itself, the window open and close perfectly works #1. After more test i have find out that if i remove the content :

<Setter Property="Content">
            <Setter.Value>
                <Path Data="F1M54.0573,47.8776L38.1771,31.9974 54.0547,16.1198C55.7604,14.4141 55.7604,11.6511 54.0573,9.94531 52.3516,8.23962 49.5859,8.23962 47.8802,9.94531L32.0026,25.8229 16.1224,9.94531C14.4167,8.23962 11.6511,8.23962 9.94794,9.94531 8.24219,11.6511 8.24219,14.4141 9.94794,16.1198L25.8255,32 9.94794,47.8776C8.24219,49.5834 8.24219,52.3477 9.94794,54.0534 11.6511,55.7572 14.4167,55.7585 16.1224,54.0534L32.0026,38.1745 47.8802,54.0534C49.5859,55.7585 52.3516,55.7572 54.0573,54.0534 55.7604,52.3477 55.763,49.5834 54.0573,47.8776z" Stretch="Uniform" Fill="White" Width="12" Margin="0,0,0,0" ></Path>
            </Setter.Value>
        </Setter>

So if i remove the path, i can keep the style in the app.xaml and it works perfectly. so i have a bug that seems like <Path /> in a style CANNOT be used twice in a whole application lifetime on SOME computers. so far it's 50% work 50% don't work. We also trace that issue to be happening with simple image such as :

<BitmapImage x:Key="Image_Print" UriSource="Images\Print.png" />

The above is not working on the second time used on the same computers that have the button style problem.

We are developing on VS2010 and we are running 4.0 full profile if that might help. Seems like Microsoft bug. Right now the only solution we have is literally copy paste every style into each individual window and user control resource and that work.

We do not alter the dictionary manually in anyway. Resources are read only.

EDIT

We noticed in Windows\Microsoft.NET\Framework\WPF\ on computer with the issue it is missing some files such as PresentationCore.DLL. We installed .Net 4.5 runtime on one of the computer's with the issue and the missing DLL go created. Now with the .NET 4.5 installed the problem is completely gone, hence the issue IS Microsoft related. The problem is that we cannot deploy .Net 4.5 application with VS2010 so i still need a solution to patch the .NET 4.0

役に立ちましたか?

解決

We are opting for going remote assistance to install manually .net 4.5 to 600-650 users around the globe until we have the budget to by new license for all our developers on new visual studio. We are aiming at holding this until .net 5.0 to not get caught in another stupid .net issue such as this.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top