2015-11-30 1 views
0

Je suis désolé si le titre est un peu confus, mais je ne sais pas trop comment décrire brièvement ce problème. J'ai une application Windows wpf qui fonctionne bien dans Windows 8 et 10, mais se bloque sur Windows 7 lors du chargement de l'interface principale. L'exception la demande a été retunring était le suivant:WPF sur Windows 7: GetCultureInfo dans ValueConverter provoquant l'exception StaticResourceExtension sur Provide Value

sur apportent une valeur 'System.Windows.StaticResourceExtension' a jeté une exception . StackTrace: à System.Windows.Markup.WpfXamlLoader.Load (XamlReader XamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, objet RootObject, XamlObjectWriterSettings paramètres, Uri baseURI) à System.Windows.Markup.WpfXamlLoader.LoadBaml (XamlReader XamlReader, skipJournaledProperties booléennes, d'objets RootObject, XamlAccessLevel AccessLevel, Uri baseURI) à System.Windows.Markup.XamlReader.LoadBaml (flux stream, ParserContext parserContext, parent de l'objet, Boolean closeStream) à System.Windows.Application.LoadBamlStreamWithSyncInfo (flux stream, ParserContext pc) à System.Windows.Application.LoadComponent (Uri resourceLocator, Boolean bSkipJournaledProperties) à System.Windows.Application.DoStartup() à System.Windows.Application. < .ctor> b__1 (Object utilisé) à System.Windows.Threading.ExceptionWrapper.InternalRealCall (délégué rappel , objet args, Int32 numArgs) à MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen (source objet , procédé délégué, args Object, Int32 numArgs, délégué catchHandler)

Google m'a dit que la question peut être l'ordre dans lequel je définissais des ressources statiques (Provide value on 'System.Windows.StaticResourceExtension) dans mon fichier XAML. En fait, j'ai quelques DataTemplate défini comme ressources statiques dans la balise Window.Resources, mais ce poste a dit, ils sont définis dans le bon ordre et la balise Window.Resources est le premier enfant de la balise de fenêtre:

<Window x:Class="FlyMasterSyncGui.Forms.FlightLog" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:FlyMasterSyncGui.Forms" 
    xmlns:formsUtils="clr-namespace:FlyMasterSyncGui.FormsUtils" 
    Title="FlymasterSync" Height="372" Width="608" 
    Icon="../Assets/icon.ico" 
    WindowStartupLocation="CenterScreen" 
    Closed="FlightLog_OnClosed" 
    Closing="FlightLog_OnClosing" Loaded="FlightLog_OnLoaded"> 

<Window.Resources> 

    <CollectionViewSource x:Key="groupedFlights" Source="{Binding TracksDb.Entries}"> 
     <CollectionViewSource.GroupDescriptions> 
      <PropertyGroupDescription PropertyName="FlightInfo.Date" Converter="{formsUtils:YearConverter}" /> 
      <PropertyGroupDescription PropertyName="FlightInfo.Date" Converter="{formsUtils:MonthConverter}" /> 
     </CollectionViewSource.GroupDescriptions> 
    </CollectionViewSource> 

    <DataTemplate x:Key="InnerTemplate"> 
     <Grid Margin="0,10,0,0"> 
      <Border VerticalAlignment="Bottom" Background="#FF9CA7B4" 
Padding="5,0,0,0"> 
[...] 

Bien que, dans ce xaml j'utilise des ValueConverters et après quelques tentatives, j'ai découvert que c'est MonthConverter qui déclenche l'exception. Le code de conversion est le suivant:

public class MonthConverter : MarkupExtension, IValueConverter 
{ 
    private MonthConverter _converter; 

    public MonthConverter() 
    { 
    } 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     DateTime d = (DateTime)value; 

     //Exception Here 
     var monthName = CultureInfo.GetCultureInfo("en-en").DateTimeFormat.GetMonthName(d.Month);       
     return char.ToUpper(monthName[0]) + monthName.Substring(1); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 

    public override object ProvideValue(IServiceProvider serviceProvider) 
    { 
     if (_converter == null) _converter = new MonthConverter(); 
     return _converter; 
    } 
} 

L'exception est appelée quand je l'appelle:

CultureInfo.GetCultureInfo("en-en").DateTimeFormat.GetMonthName(d.Month); 

Et je ne comprends pas vraiment pourquoi. En ce moment j'ai juste fait une fonction personnalisée qui convertit le numéro du mois en son nom, donc je n'ai pas besoin d'appeler CultureInfo et j'ai corrigé le problème, mais j'aimerais savoir pourquoi cette exception est déclenchée et pourquoi seulement se produit sur Windows 7. xaml est-il analysé différemment dans Windows 7? Les données CultureInfo sont-elles chargées plus tard dans Windows 7?

Merci à l'avance pour toute réponse :)

Répondre

2

Dans Windows 7 le CultureInfo.GetCultureInfo("en-en") jette un CultureNotFoundException. Essayez d'utiliser simplement en ou une culture spécifique à une région, telle que en-US.Cependant, lors de la conversion et de la chaîne, j'utiliser le CultureInfo.InvariantCulture

Voir les noms de culture disponibles ici: http://www.csharp-examples.net/culture-names/

+0

Je n'ai pas remarqué j'ai utilisé une culture unexisting! Probablement sur Windows 8 et 10, quand cela arrive, il revient à InvariantCulture, mais pas sur Windows 7. Quoi qu'il en soit, invariantCulture a résolu le problème! Je pense que l'exception qu'il tirait était une conseguence de l'exception CultureNotFoundException. Merci beaucoup :) – Eux