Code Monkey home page Code Monkey logo

Comments (5)

maxkatz6 avatar maxkatz6 commented on August 15, 2024

But MidiForMacrosViewModel doesn't have a public constructor, while you try to use it as a parameterless DataContext:
https://github.com/SimonORorke/FalconProgrammer/blob/main/FalconProgrammer/Views/MidiForMacrosView.axaml#L12

from avalonia-docs.

maxkatz6 avatar maxkatz6 commented on August 15, 2024

If you want to resolve previewing data context, you should define this context in C# code where you can access your DI container.

from avalonia-docs.

SimonORorke avatar SimonORorke commented on August 15, 2024

Thanks, @maxkatz6! I got it working. For anyone who encounters the same problem, here is an example. As I mentioned, the public view model constructors in my application all have parameters, like this:

public MidiForMacrosViewModel(IDialogService dialogService,
  IDispatcherService dispatcherService) : base(dialogService, dispatcherService) { }

So I had to define Design.DataContext in code behind instead of in XAML, like this:

public MidiForMacrosView() {
  InitializeComponent();
  // This only sets the DataContext for the previewer in the IDE.
  Design.SetDataContext(this,
    new MidiForMacrosViewModel(new DialogService(), new DispatcherService()));
}

I expect view model constructors with parameters are common, as my understanding is that it is part of the usual way dependency injection works. That is the purpose of the view model constructors with parameters in my project. But I could not find any documentation that covers using the previewer in such a scenario. I would expect to find it in the How To Use Live Preview page of Avalonia's documentation, but I don't see anything there.

Have I missed something in the documentation? If not, I'd be happy to have a go at improving the How To Use Live Preview page and submitting a pull request to the documentation project.

from avalonia-docs.

timunie avatar timunie commented on August 15, 2024

@SimonORorke

  • parameterless constructor may be internal, but it must be there
  • or as you said, add a designtime one
  • or use a static one with {x:Static my:MyDesignerVM.Instance}
  • or use if(Design.IsDesignTime) in code behind.

a PR to the docs may be a good idea, could also be cross-linked to this one: https://docs.avaloniaui.net/docs/guides/implementation-guides/how-to-implement-dependency-injection

from avalonia-docs.

SimonORorke avatar SimonORorke commented on August 15, 2024

@timunie
My pull request commit incorporates some of the points you make, though there were a couple I was not sure about and did not mention. If applicable, please let me know how I may improve the update.

from avalonia-docs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.