AvaloniaUI / AvaloniaVS

Visual Studio Extension for Avalonia
MIT License
421 stars 81 forks source link

Preview not work when using ViewModel with params #392

Closed IgelsT closed 11 months ago

IgelsT commented 11 months ago

Describe the bug

Preview not work when using ViewModel with params

C# code InitializeComponent(); vm = new ucAddViewViewModel(id); DataContext = vm; when vm = new ucAddViewViewModel(); all works.

To Reproduce

Create ViewModel with params constructor

AvaloniaVS plugin version

11.1

Avalonia version

11.0.4

Visual Studio version

2022

Relevant log output

20:34:55.536 [Error] 8308 <= "Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object."
20:34:55.546 [Error] 8308 <= "   at camera.ViewModels.ucAddViewViewModel.initModel(String id) in D:\Projects\Develop\net\avalonia\camera\ViewModels\ucAddViewViewModel.cs:line 30"
20:34:55.546 [Error] 8308 <= "   at camera.ViewModels.ucAddViewViewModel..ctor(String id) in D:\Projects\Develop\net\avalonia\camera\ViewModels\ucAddViewViewModel.cs:line 19"
20:34:55.546 [Error] 8308 <= "   at camera.Views.Pages.ViewList.ucAddView.initControl(String id) in D:\Projects\Develop\net\avalonia\camera\Views\Pages\ViewList\ucAddView.axaml.cs:line 19"
20:34:55.546 [Error] 8308 <= "   at camera.Views.Pages.ViewList.ucAddView..ctor() in D:\Projects\Develop\net\avalonia\camera\Views\Pages\ViewList\ucAddView.axaml.cs:line 14"
20:34:55.546 [Error] 8308 <= "   at camera.Views.Pages.ViewList.ucViewList.BtnAddClick(Object sender, RoutedEventArgs args) in D:\Projects\Develop\net\avalonia\camera\Views\Pages\ViewList\ucViewList.axaml.cs:line 19"
20:34:55.546 [Error] 8308 <= "   at Avalonia.Interactivity.Interactive.<AddHandler>g__InvokeAdapter|4_0[TEventArgs](Delegate baseHandler, Object sender, RoutedEventArgs args)"
20:34:55.546 [Error] 8308 <= "   at Avalonia.Interactivity.Interactive.<>c__4`1.<AddHandler>b__4_1(Delegate baseHandler, Object sender, RoutedEventArgs args)"
20:34:55.546 [Error] 8308 <= "   at Avalonia.Interactivity.EventRoute.RaiseEventImpl(RoutedEventArgs e)"
20:34:55.546 [Error] 8308 <= "   at Avalonia.Interactivity.EventRoute.RaiseEvent(Interactive source, RoutedEventArgs e)"
20:34:55.546 [Error] 8308 <= "   at Avalonia.Interactivity.Interactive.RaiseEvent(RoutedEventArgs e)"
20:34:55.546 [Error] 8308 <= "   at Avalonia.Controls.Button.OnClick()"
20:34:55.561 [Error] 8308 <= "   at Avalonia.Controls.Button.OnPointerReleased(PointerReleasedEventArgs e)"
20:34:55.561 [Error] 8308 <= "   at Avalonia.Input.InputElement.<>c.<.cctor>b__32_9(InputElement x, PointerReleasedEventArgs e)"
20:34:55.561 [Error] 8308 <= "   at Avalonia.Reactive.LightweightObservableBase`1.PublishNext(T value)"
20:34:55.561 [Error] 8308 <= "   at Avalonia.Interactivity.EventRoute.RaiseEventImpl(RoutedEventArgs e)"
20:34:55.561 [Error] 8308 <= "   at Avalonia.Interactivity.EventRoute.RaiseEvent(Interactive source, RoutedEventArgs e)"
20:34:55.567 [Error] 8308 <= "   at Avalonia.Interactivity.Interactive.RaiseEvent(RoutedEventArgs e)"
20:34:55.567 [Error] 8308 <= "   at Avalonia.Input.MouseDevice.MouseUp(IMouseDevice device, UInt64 timestamp, IInputRoot root, Point p, PointerPointProperties props, KeyModifiers inputModifiers, IInputElement hitTest)"
20:34:55.567 [Error] 8308 <= "   at Avalonia.Input.MouseDevice.ProcessRawEvent(RawPointerEventArgs e)"
20:34:55.567 [Error] 8308 <= "   at Avalonia.Input.MouseDevice.ProcessRawEvent(RawInputEventArgs e)"
20:34:55.567 [Error] 8308 <= "   at Avalonia.Input.InputManager.ProcessInput(RawInputEventArgs e)"
20:34:55.567 [Error] 8308 <= "   at Avalonia.Controls.TopLevel.HandleInput(RawInputEventArgs e)"
20:34:55.567 [Error] 8308 <= "   at Avalonia.Controls.Remote.Server.RemoteServerTopLevelImpl.<>c__DisplayClass13_0.<OnMessage>b__5()"
20:34:55.567 [Error] 8308 <= "   at Avalonia.Threading.DispatcherOperation.InvokeCore()"
20:34:55.567 [Error] 8308 <= "   at Avalonia.Threading.DispatcherOperation.Execute()"
20:34:55.567 [Error] 8308 <= "   at Avalonia.Threading.Dispatcher.ExecuteJob(DispatcherOperation job)"
20:34:55.567 [Error] 8308 <= "   at Avalonia.Threading.Dispatcher.ExecuteJobsCore(Boolean fromExplicitBackgroundProcessingCallback)"
20:34:55.567 [Error] 8308 <= "   at Avalonia.Threading.Dispatcher.Signaled()"
20:34:55.567 [Error] 8308 <= "   at Avalonia.Controls.Platform.ManagedDispatcherImpl.RunLoop(CancellationToken token)"
20:34:55.567 [Error] 8308 <= "   at Avalonia.Threading.DispatcherFrame.Run(IControlledDispatcherImpl impl)"
20:34:55.583 [Error] 8308 <= "   at Avalonia.Threading.Dispatcher.PushFrame(DispatcherFrame frame)"
20:34:55.583 [Error] 8308 <= "   at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken)"
20:34:55.583 [Error] 8308 <= "   at Avalonia.DesignerSupport.Remote.RemoteDesignerEntryPoint.Main(String[] cmdline)"
20:34:55.583 [Error] 8308 <= "   at Avalonia.Designer.HostApp.Program.Main(String[] args)"
20:34:59.646 [Information]  Process exited

Additional context

No response

maxkatz6 commented 11 months ago

20:34:55.536 [Error] 8308 <= "Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object." 20:34:55.546 [Error] 8308 <= " at camera.ViewModels.ucAddViewViewModel.initModel(String id) in D:\Projects\Develop\net\avalonia\camera\ViewModels\ucAddViewViewModel.cs:line 30" 20:34:55.546 [Error] 8308 <= " at camera.ViewModels.ucAddViewViewModel..ctor(String id) in D:\Projects\Develop\net\avalonia\camera\ViewModels\ucAddViewViewModel.cs:line 19"

Looks like there is an exception in your code. Something is null in initModel method on line 30. Doesn't seem to be a problem with Avalonia, so closing it.

Keep in mind, that previewer doesn't call Main method, and instead it calls BuildAvaloniaApp method directly. If you do any initialization in Main method, you need to move it outside. For example, to the Application itself.