C
C#ā€¢2y ago
antimatter8189

ā” Using mvvm toolkit- Why my view has no access to my command?

I got the following Code in the view model:
[RelayCommand]
public void CalculateAndSave()
[RelayCommand]
public void CalculateAndSave()
And the view:
And in the View:
<Window x:Class="Calculator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:VM="clr-namespace:Calculator.ViewModels"
xmlns:local="clr-namespace:Calculator"
mc:Ignorable="d"
Title="MainWindow" Height="525" Width="350">
<Window.DataContext>
<VM:CalculatorVM/>
</Window.DataContext>
And in the View:
<Window x:Class="Calculator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:VM="clr-namespace:Calculator.ViewModels"
xmlns:local="clr-namespace:Calculator"
mc:Ignorable="d"
Title="MainWindow" Height="525" Width="350">
<Window.DataContext>
<VM:CalculatorVM/>
</Window.DataContext>
I got a few button on the view, when it try to give the button a command to CalculateAndSave, method isn't available, is my binding wrong?
65 Replies
ACiDCA7
ACiDCA7ā€¢2y ago
is just the designer saying its not available or did you try it during running app to check if the method is called? if its just the designer.. well it isnt so smart.. you would have to tell it more explicitly of what type the datacontext is
antimatter8189
antimatter8189OPā€¢2y ago
Meaning what? i tried just now running it didnt hit the breakpoint of the command so it aint working for sure
ACiDCA7
ACiDCA7ā€¢2y ago
ok well since you didnt copy the part of the button i cant help further
antimatter8189
antimatter8189OPā€¢2y ago
Oh
<Button x:Name="oneButton"
Command="{Binding CalculateAndSave}"
Click="NumberButton_Click"
Style="{StaticResource numberButtonsStyle}"
Content="1"
Grid.Row="4"/>
<Button x:Name="oneButton"
Command="{Binding CalculateAndSave}"
Click="NumberButton_Click"
Style="{StaticResource numberButtonsStyle}"
Content="1"
Grid.Row="4"/>
@ACiDCA7 nothing to see much just want to bind a command to it
ACiDCA7
ACiDCA7ā€¢2y ago
click and command maybe clashing?
antimatter8189
antimatter8189OPā€¢2y ago
hmm Nop. now when i click nothing happens command isnt binded Probably the entire view model isnt binded properly no autocompletion on the method name
ACiDCA7
ACiDCA7ā€¢2y ago
try setting datacontext in codebehind/where oyu are creating the window
antimatter8189
antimatter8189OPā€¢2y ago
I did
public partial class MainWindow : Window
{
double lastNumber, result;
SelectedOperator selectedOperator;
public MainWindow()
{
InitializeComponent();
DataContext = new CalculatorVM();

acButton.Click += AcButton_Click;
negativeButton.Click += NegativeButton_Click;
percentageButton.Click += PercentageButton_Click;
equalButton.Click += EqualButton_Click;
}
public partial class MainWindow : Window
{
double lastNumber, result;
SelectedOperator selectedOperator;
public MainWindow()
{
InitializeComponent();
DataContext = new CalculatorVM();

acButton.Click += AcButton_Click;
negativeButton.Click += NegativeButton_Click;
percentageButton.Click += PercentageButton_Click;
equalButton.Click += EqualButton_Click;
}
didnt help either smth is quite odd here
ACiDCA7
ACiDCA7ā€¢2y ago
<Button x:Name="oneButton"
Command="{Binding CalculateAndSaveCommand}"
Style="{StaticResource numberButtonsStyle}"
Content="1"
Grid.Row="4"/>
<Button x:Name="oneButton"
Command="{Binding CalculateAndSaveCommand}"
Style="{StaticResource numberButtonsStyle}"
Content="1"
Grid.Row="4"/>
try this
hengi
hengiā€¢2y ago
Should be called CalculateAndSaveCommand
antimatter8189
antimatter8189OPā€¢2y ago
I have this code in place, it does nothing
ACiDCA7
ACiDCA7ā€¢2y ago
like hengi said Command is missing in the binding name dont change the methodname tho just where you are binding
antimatter8189
antimatter8189OPā€¢2y ago
GitHub
GitHub - KostaKing/Calculator
Contribute to KostaKing/Calculator development by creating an account on GitHub.
antimatter8189
antimatter8189OPā€¢2y ago
look at line 100 at the xaml i got it It doesnt work the breakpoint isn't being activated in the command
hengi
hengiā€¢2y ago
The CalculatorVM needs to be partial so it can generate the code And add it to the class
antimatter8189
antimatter8189OPā€¢2y ago
meaning?
public partial class CalculatorVM: ObservableObject
public partial class CalculatorVM: ObservableObject
whats next still doesnt work
hengi
hengiā€¢2y ago
Try to run it and see if there are binding errors Unless you set the design time type of the data context (don't remember exactly what it's called) it will not autocomplete and might give you a warning
antimatter8189
antimatter8189OPā€¢2y ago
Nothing shows up, btw you do have the full repo the codebase is rly simple , just trying to convert it to using mvvm u can run it on your own machine, press 1 and see nothing happens it just doesnt go in the method
hengi
hengiā€¢2y ago
1 sec
ACiDCA7
ACiDCA7ā€¢2y ago
grr i couldnt get it to work with the attribute aswell but if you go the "oldshool" way it works
antimatter8189
antimatter8189OPā€¢2y ago
but i dont wanna go oldschool lol thats the point why doesnt the attribute works? like tf šŸ˜„
hengi
hengiā€¢2y ago
seems like the source generator doesn't work? need ilspy oof
antimatter8189
antimatter8189OPā€¢2y ago
im not on that lvl mate hah btw can u give code?
hengi
hengiā€¢2y ago
yes it seems like .net 4.8 doesn't like the source generator of mvvm toolkit
antimatter8189
antimatter8189OPā€¢2y ago
thats interesting any fix?
ACiDCA7
ACiDCA7ā€¢2y ago
oh i didnt even check which target was set.. good point
antimatter8189
antimatter8189OPā€¢2y ago
yup am stuck on that 4.8
ACiDCA7
ACiDCA7ā€¢2y ago
public RelayCommand CalculateAndSaveCommand { get; set; }
public CalculatorVM()
{
CalculateAndSaveCommand = new RelayCommand(CalculateAndSave);
}
public RelayCommand CalculateAndSaveCommand { get; set; }
public CalculatorVM()
{
CalculateAndSaveCommand = new RelayCommand(CalculateAndSave);
}
add this to calculator vm
antimatter8189
antimatter8189OPā€¢2y ago
Yup That works but how do we make source generators work with 4.8?
ACiDCA7
ACiDCA7ā€¢2y ago
why are you stuck on net framework? you should not go in with the mindset that new features get backported to old runtimes
antimatter8189
antimatter8189OPā€¢2y ago
legacy project mate not my choice
hengi
hengiā€¢2y ago
ohhhh
antimatter8189
antimatter8189OPā€¢2y ago
millions of lines of code wont be switched to .net core overnight
hengi
hengiā€¢2y ago
you need to do right click on the calculator project and migrate to PackageReference it's packages.config not working
antimatter8189
antimatter8189OPā€¢2y ago
hengi sorry how do i migrate? what? give more details mate šŸ˜„
ACiDCA7
ACiDCA7ā€¢2y ago
hengi
hengiā€¢2y ago
in the solution explorer in visual studio, expand your Calculator project, right click "References" and select "Migrate packages.config to PackageReference" also that, you need to set the LangVersion of the Project to at least 8..
ACiDCA7
ACiDCA7ā€¢2y ago
net framework is stuck on 7.3
antimatter8189
antimatter8189OPā€¢2y ago
yup it is
hengi
hengiā€¢2y ago
it's not stuck, just edit the csproj
antimatter8189
antimatter8189OPā€¢2y ago
shit lol
ACiDCA7
ACiDCA7ā€¢2y ago
i dont think srcgens are included in latest but lemme try
hengi
hengiā€¢2y ago
open the csproj with editor and add <LangVersion>9.0</LangVersion> under "TargetFrameworkVersion" works for me when migrated and the LangVersion manually added
antimatter8189
antimatter8189OPā€¢2y ago
hengi
in the solution explorer in visual studio, expand your Calculator project, right click "References" and select "Migrate packages.config to PackageReference"
also that, you need to set the LangVersion of the Project to at least 8..
in the solution explorer in visual studio, expand your Calculator project, right click "References" and select "Migrate packages.config to PackageReference"
also that, you need to set the LangVersion of the Project to at least 8..
after selecting refernces where do i find migrate packages.config?
hengi
hengiā€¢2y ago
The NuGet Blog
Migrate to PackageReference with 3 clicks - The NuGet Blog
Last year, we introduced the option to make PackageReference the default package management format for managing NuGet dependencies when installing the first NuGet package for a newly created projects. With Visual Studio Version 15.7 Preview 3, we have introduced the capability to migrate existing projects that use the packages.config format to u...
ACiDCA7
ACiDCA7ā€¢2y ago
indeed it does work
antimatter8189
antimatter8189OPā€¢2y ago
sec doing it
ACiDCA7
ACiDCA7ā€¢2y ago
i was under the impression that the MS sourcegens were jsut for net5+.. well TIL
antimatter8189
antimatter8189OPā€¢2y ago
soz am kinda lost lol trying but
hengi
hengiā€¢2y ago
it's actually .NET Standard 2.0 I guess..
antimatter8189
antimatter8189OPā€¢2y ago
no Default package management format exsist in the setting
antimatter8189
antimatter8189OPā€¢2y ago
The NuGet Blog
Migrate to PackageReference with 3 clicks - The NuGet Blog
Last year, we introduced the option to make PackageReference the default package management format for managing NuGet dependencies when installing the first NuGet package for a newly created projects. With Visual Studio Version 15.7 Preview 3, we have introduced the capability to migrate existing projects that use the packages.config format to u...
hengi
hengiā€¢2y ago
oh you only need to do the right click thing you don't need to change the setting
antimatter8189
antimatter8189OPā€¢2y ago
hhaha well i did any chance 1 of you gives me an updated repo that works? clearly above my head I just need to update the lang ver u saying?
hengi
hengiā€¢2y ago
@AntiMatter you can either look at the branch or just merge, it's the new LangVersion and changing to ProjectReference
antimatter8189
antimatter8189OPā€¢2y ago
I merged jesus lol thank you so much i will go over what u did and learn from it
hengi
hengiā€¢2y ago
np lol, we also have a project at work with .net 4.8 because of WCF, we'll probably never drop that it's good to know that source generators are indeed working
ACiDCA7
ACiDCA7ā€¢2y ago
meeh i was just about to create a quick gif how to do it..xD
antimatter8189
antimatter8189OPā€¢2y ago
do it would love to save it šŸ˜„ i just merged i dont know tf going on lol give me the fig gif*
ACiDCA7
ACiDCA7ā€¢2y ago
ACiDCA7
ACiDCA7ā€¢2y ago
hope you like it.. ignore that its in german^^
antimatter8189
antimatter8189OPā€¢2y ago
amazing dude thank you so much you 2 my gods will sacrifice goat tonight @ACiDCA7 @hengi šŸ˜„
Accord
Accordā€¢2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?