Hallo,
während InitalizeComponent wird folgende Exception geworfen:> Fehlermeldung:
System.Reflection.TargetInvocationException wurde nicht behandelt.
HResult=-2146232828
Message=Ein Aufrufziel hat einen Ausnahmefehler verursacht.
Dies tritt auf wenn ich versuche eine Textbox mit Konverter an einen Double zu binden:
class StringDoubleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || value == DependencyProperty.UnsetValue) return DependencyProperty.UnsetValue;
return value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
double retVal;
var isDecimal = double.TryParse((string)value, out retVal);
return isDecimal ? retVal : 0;
}
}
<TextBox Text="{Binding Coords.Latitude, Mode=OneWayToSource, Converter={StaticResource stringDoubleConverter}}" PreviewTextInput="NumberValidationTextBox"/>
Wieso wird diese Exception geworfen?
Hallo,
hast Du mal die InnerException bzw. den Stacktrace der Exception angesehen?
Da sollten etwas brauchbarere Informationen zu finden sein.
Gruß, MarsStein
Non quia difficilia sunt, non audemus, sed quia non audemus, difficilia sunt! - Seneca
StaticResource stringDoubleConverter
Ich denke stringDoubleConverter sollte genau so heißen wie die Klasse => StringDoubleConverter.
Wenn ich es richtig im Kopf habe sind Resourcen Case Sensetive.
Sollte man mal gelesen haben:
StaticResource stringDoubleConverter
Ich denke stringDoubleConverter sollte genau so heißen wie die Klasse => StringDoubleConverter.
Wenn ich es richtig im Kopf habe sind Resourcen Case Sensetive.
Nö, kommt drauf an wie er die Resource definiert hat. =)
Hier die genauere Fehlerinformation:> Fehlermeldung:
System.Reflection.TargetInvocationException wurde nicht behandelt.
HResult=-2146232828
Message=Ein Aufrufziel hat einen Ausnahmefehler verursacht.
Source=mscorlib
StackTrace:
bei System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
bei System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
bei System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
bei System.Activator.CreateInstance(Type type, Boolean nonPublic)
bei System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
bei System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
bei System.Activator.CreateInstance(Type type, Object[] args)
bei System.Xaml.Schema.SafeReflectionInvoker.CreateInstanceCritical(Type type, Object[] arguments)
bei System.Xaml.Schema.SafeReflectionInvoker.CreateInstance(Type type, Object[] arguments)
bei System.Xaml.Schema.XamlTypeInvoker.CreateInstance(Object[] arguments)
bei MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstanceWithCtor(XamlType xamlType, Object[] args)
bei MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstance(XamlType xamlType, Object[] args)
bei MS.Internal.Xaml.Runtime.PartialTrustTolerantRuntime.CreateInstance(XamlType xamlType, Object[] args)
bei System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx)
bei System.Xaml.XamlObjectWriter.WriteStartMember(XamlMember property)
bei System.Xaml.XamlWriter.WriteNode(XamlReader reader)
bei System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack1 stack, IStyleConnector styleConnector) bei System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) bei System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri) bei System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream) bei System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc) bei System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties) bei System.Windows.Application.DoStartup() bei System.Windows.Application.<.ctor>b__1_0(Object unused) bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) bei System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) bei System.Windows.Threading.DispatcherOperation.InvokeImpl() bei System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) bei MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state) bei System.Windows.Threading.DispatcherOperation.Invoke() bei System.Windows.Threading.Dispatcher.ProcessQueue() bei System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) bei MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) bei MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) bei System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) bei System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) bei MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) bei MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) bei System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) bei System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) bei System.Windows.Application.RunDispatcher(Object ignore) bei System.Windows.Application.RunInternal(Window window) bei System.Windows.Application.Run(Window window) bei System.Windows.Application.Run() bei OpenWeather.GUI.App.Main() in C:\Users\eagle\Dropbox\Programmierung\NET\C#\OpenWeather\OpenWeather.GUI\obj\Debug\App.g.cs:Zeile 0. bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() bei System.Threading.ThreadHelper.ThreadStart_Context(Object state) bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) bei System.Threading.ThreadHelper.ThreadStart() InnerException: HResult=-2147467261 Message=Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. Source=PresentationFramework StackTrace: bei System.Windows.Data.BindingExpression.IsValidValueForUpdate(Object value, Type sourceType) bei System.Windows.Data.BindingExpression.ConvertProposedValue(Object value) bei System.Windows.Data.BindingExpressionBase.UpdateValue() bei System.Windows.Data.BindingExpression.Activate(Object item) bei System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt) bei System.Windows.Data.BindingExpression.AttachOverride(DependencyObject target, DependencyProperty dp) bei System.Windows.Data.BindingExpressionBase.OnAttach(DependencyObject d, DependencyProperty dp) bei System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal) bei System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(Object instance, Object value) bei MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member, Object obj, Object value) bei MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value) bei MS.Internal.Xaml.Runtime.PartialTrustTolerantRuntime.SetValue(Object obj, XamlMember property, Object value) bei System.Xaml.XamlObjectWriter.Logic_ApplyPropertyValue(ObjectWriterContext ctx, XamlMember prop, Object value, Boolean onParent) bei System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentProperty(ObjectWriterContext ctx) bei System.Xaml.XamlObjectWriter.Logic_AssignProvidedValue(ObjectWriterContext ctx) bei System.Xaml.XamlObjectWriter.WriteEndObject() bei System.Xaml.XamlWriter.WriteNode(XamlReader reader) bei System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack
1 stack, IStyleConnector styleConnector)
bei System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
bei System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
bei System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
bei System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
bei OpenWeather.GUI.OpenWeatherWindow.InitializeComponent() in ...\OpenWeather\OpenWeather.GUI\OpenWeatherWindow.xaml:Zeile 1.
bei OpenWeather.GUI.OpenWeatherWindow..ctor() in ...\OpenWeather\OpenWeather.GUI\OpenWeatherWindow.xaml.cs:Zeile 26.
InnerException:
Ja, der Ressource-Key ist "stringDoubleConverter" =)
Meine Vermutung: Convert-Instanz kann nicht erstellt werden, dadurch gibts ne NullReferenceException (siehe InnerException) und dadurch wird die TargetInvocationException gefeuert.
Evtl. hat der Converter einfach keinen Parameterlosen Konstruktor, sofern das nicht der vollständige Code ist.
Tipp1: in ConvertBack
solltest Du ebenfalls einen potentielle Nullvalue von string abfangen.
Tipp2: ToString() immer mit der jeweiligen Culture ausführen.
Du hast hier die CultureInfo in der Parameterliste, dann gib sie auch ToString mit. Ansonsten sind Fehler bezüglich Komma und Co vorprogrammiert.
- performance is a feature -
Microsoft MVP - @Website - @AzureStuttgart - github.com/BenjaminAbt - Sustainable Code
Muss der Converter nicht public sein, sonst kann doch kein Objekt davon erstellt werden außerhalb des Projektes (in dem Fall der ausführenden Engine)
Mit freundlichen Grüßen
lutzeslife
Jop, sollte er (eigentlich).
- performance is a feature -
Microsoft MVP - @Website - @AzureStuttgart - github.com/BenjaminAbt - Sustainable Code
Ich muss mich entschuldigen, der Fehler ist aus einer vergessenen Zuweisungen im ViewModel entstanden. (Coords war null)
Seht es als beantwortet. Danke!
Muss der Converter nicht public sein, sonst kann doch kein Objekt davon erstellt werden außerhalb des Projektes (in dem Fall der ausführenden Engine)
Nein, der Converter muss nicht zwangsläufig public
sein, internal
ist in vielen Fällen ausreichend.