Comments (5)
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.
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.
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.
- 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.
@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)
- DataGrid documentation page wrongly states that DataGrid is editable HOT 5
- Community feedback request regarding docs discoverability HOT 1
- update dependencies
- FormattedText.Spans does not exist HOT 2
- TabControl.ContentTemplate uses wrong DataContext #2 HOT 7
- Is Test Drive inconsistent? HOT 1
- MSAL Authentication Library cannot open browser on mobile HOT 6
- MaskedTextBox — mask characters table renders incorrectly
- Images on the "Developer Tools" page overflow the content area
- XAML Using Prefix can reference a namespace in another assembly HOT 2
- Dark mode syntax highlighting is mediocre HOT 1
- Improve Custom Font Documentation
- Clarify RenderOptions
- Localizing using ResX should indicate that PublicResXFileCodeGenerator is to be used for Xaml support HOT 2
- Guide to Customizing Existing Controls
- update docusaurus
- Mac App Store Deployment Guide is Out-of-Date and Incorrect HOT 4
- Remove async void usage from "How to Access the UI Thread"
- update docusaurus
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from avalonia-docs.