These are chat archives for AvaloniaUI/Avalonia

20th
Dec 2018
Jonathan
@vanillajonathan
Dec 20 2018 12:13 UTC
Should I set the DataContext with code in the MainWindow.xaml.cs, or using XAML?
In XAML the <Button> element does have a DataContext attribute, but my <local:FooView> does not seem to have any DataContext attribute. I don't know why.
Jonathan
@vanillajonathan
Dec 20 2018 13:41 UTC
When I try tro reach this.DataContext from the constructor of MainWindow.xaml.cs it is null.
danwalmsley
@danwalmsley
Dec 20 2018 13:43 UTC
thats when you need to set it, DataContext = new MyMainWindowViewModel()
Jonathan
@vanillajonathan
Dec 20 2018 13:47 UTC
I see. I thought the MainWindow.xaml would somehow set already. Because in Program.cs it instantiates something. BuildAvaloniaApp().Start<MainWindow>(() => new MainWindowViewModel());
danwalmsley
@danwalmsley
Dec 20 2018 13:48 UTC
in that case it will be set after the ctor
Jonathan
@vanillajonathan
Dec 20 2018 14:08 UTC
Ah, I see. Is there any method that gets called after the constructor, which I can use to read the DataContext into a class variable? So I don't have to typecast it everytime?
danwalmsley
@danwalmsley
Dec 20 2018 14:14 UTC
why do you need to do that?
DataContext should normally be accessed by the view from xaml, you shouldn't need to access DataContext programmatically for 99% of use cases
Jonathan
@vanillajonathan
Dec 20 2018 14:15 UTC
From my MainWindow, I want to listen to events and have the callback update the view models for the child views of the MainWindow.
danwalmsley
@danwalmsley
Dec 20 2018 14:16 UTC
you should really be doing that using bindings
otherwise you are defeating the point of having a separate view and viewmodel
perhaps if you post something to a GitHub repo we can take a look and advise?
Jonathan
@vanillajonathan
Dec 20 2018 14:17 UTC
Hmm. But my FooView.xaml uses Text={Binding Text}
I don't have it on GitHub
So my View is bound to its view model. But shouldn't I update that View Model from the MainWindow?
danwalmsley
@danwalmsley
Dec 20 2018 14:18 UTC
MainWindow is just the controls, you shouldn't put any app logic or ui logic in there
ViewModel should contain UI logic
Jonathan
@vanillajonathan
Dec 20 2018 14:18 UTC
Ah, so the event listener that updates the view model should be in the view model
danwalmsley
@danwalmsley
Dec 20 2018 14:18 UTC
if you can post some code and explain what your application is trying to do?
Jonathan
@vanillajonathan
Dec 20 2018 14:18 UTC
I think, I get it now
danwalmsley
@danwalmsley
Dec 20 2018 14:18 UTC

Ah, so the event listener that updates the view model should be in the view model

sounds about right

your event thingy is probably what would normally be part of the Model
so your viewmodel is ui logic (usually wrapping or with access to a model
Model is a class that has business/domain logic
and your View (MainWindow in this case) is just a dumb ui with bindings, completely not dependent or attached to your viewmodel or model code
Valters
@FaithLV
Dec 20 2018 14:20 UTC
hey, is there any documentation on how to load an actual bitmap to image? I tried setting Image.Source to a bitmap loaded from file, but it doesn't seem to render anything
Jonathan
@vanillajonathan
Dec 20 2018 14:20 UTC
So the the view model should expose a public method that is a callback, and the MainWindow hook up that method as a listener for events
danwalmsley
@danwalmsley
Dec 20 2018 14:21 UTC
your mainwindow shouldn't do anything in code (c#)
you should use your events in your viewmodel to update a property
that way the ui will be updated
Jonathan
@vanillajonathan
Dec 20 2018 14:21 UTC
But who registers the subscription to the event?
danwalmsley
@danwalmsley
Dec 20 2018 14:21 UTC
the viewmodel
its really hard, though im only guessing at what your trying to do
if you post some code I can be much more helpful
Jonathan
@vanillajonathan
Dec 20 2018 14:22 UTC
I see. But the view model doesn't have any instance of the object that emits events.
danwalmsley
@danwalmsley
Dec 20 2018 14:22 UTC
you should pass the Model instance to its ctor, or it instantiate its own object
or use some other form of dependency injection to get an instance of the object
Jonathan
@vanillajonathan
Dec 20 2018 14:23 UTC
I see
Jonathan
@vanillajonathan
Dec 20 2018 14:28 UTC
I don't know what passing the Model instance to its ctor means
danwalmsley
@danwalmsley
Dec 20 2018 14:32 UTC
var model = new MyModel();
var vm = new MyViewModel (model);
BuildAvaloniaApp().Start<MainWindow>(() => vm);
Jonathan
@vanillajonathan
Dec 20 2018 14:33 UTC
Ahh!
Jonathan
@vanillajonathan
Dec 20 2018 14:39 UTC
Then my MainWindowViewModel needs two constructors. One that takes the MyModel parameter, and one empty constructor for the application to be able to run without exception?
Jonathan
@vanillajonathan
Dec 20 2018 14:59 UTC
Else I get this mean System.Reflection.TargetInvocationException if I don't have any empty public ctor
My ChildView look like <TextBlock Text="{Binding Child1.Text}" /> then it works. If I do Text instead of Child1.Text then it don't find it.
Jonathan
@vanillajonathan
Dec 20 2018 15:55 UTC
I don't know what <Design.DataContext> does.
ahopper
@ahopper
Dec 20 2018 16:03 UTC
It gives the previewer a data context to use at design time so you get to see what your app looks like without running it
Weston
@ronnyek
Dec 20 2018 17:39 UTC
so you guys got apps in production with avalonia ui?
is there a way with a net core app, to detect ui environment and conditionally start the avalonia ui?
eg, if it was an app running in linux and had ui available, render ui, otherwise execute as a console app
Nikita Tsukanov
@kekekeks
Dec 20 2018 18:43 UTC
You can check DISPLAY variable
Or simply try XOpenDisplay from libX11.so.6
XOpenDisplay
But usually people just ship the executable alongside with startup scripts
One for console and one for gui
Miha Markič
@MihaMarkic
Dec 20 2018 20:13 UTC
Or just make add an argument that lets user decide
Jonathan
@vanillajonathan
Dec 20 2018 21:29 UTC
What's better MVVM or Flux/Redux (Single Source of Truth)?
Jeremy Koritzinsky
@jkoritzinsky
Dec 20 2018 22:32 UTC
Both have their use cases, but Avalonia's design better lends itself to MVVM architectures