C
C#ā€¢17mo 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ā€¢17mo 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
antimatter8189ā€¢17mo ago
Meaning what? i tried just now running it didnt hit the breakpoint of the command so it aint working for sure
ACiDCA7
ACiDCA7ā€¢17mo ago
ok well since you didnt copy the part of the button i cant help further
antimatter8189
antimatter8189ā€¢17mo 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ā€¢17mo ago
click and command maybe clashing?
antimatter8189
antimatter8189ā€¢17mo 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ā€¢17mo ago
try setting datacontext in codebehind/where oyu are creating the window
antimatter8189
antimatter8189ā€¢17mo 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ā€¢17mo 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ā€¢17mo ago
Should be called CalculateAndSaveCommand
antimatter8189
antimatter8189ā€¢17mo ago
I have this code in place, it does nothing
ACiDCA7
ACiDCA7ā€¢17mo ago
like hengi said Command is missing in the binding name dont change the methodname tho just where you are binding
antimatter8189
antimatter8189ā€¢17mo ago
GitHub
GitHub - KostaKing/Calculator
Contribute to KostaKing/Calculator development by creating an account on GitHub.
antimatter8189
antimatter8189ā€¢17mo 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ā€¢17mo ago
The CalculatorVM needs to be partial so it can generate the code And add it to the class
antimatter8189
antimatter8189ā€¢17mo ago
meaning?
public partial class CalculatorVM: ObservableObject
public partial class CalculatorVM: ObservableObject
whats next still doesnt work
hengi
hengiā€¢17mo 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
antimatter8189ā€¢17mo 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ā€¢17mo ago
1 sec
ACiDCA7
ACiDCA7ā€¢17mo ago
grr i couldnt get it to work with the attribute aswell but if you go the "oldshool" way it works
antimatter8189
antimatter8189ā€¢17mo ago
but i dont wanna go oldschool lol thats the point why doesnt the attribute works? like tf šŸ˜„
hengi
hengiā€¢17mo ago
seems like the source generator doesn't work? need ilspy oof
antimatter8189
antimatter8189ā€¢17mo ago
im not on that lvl mate hah btw can u give code?
hengi
hengiā€¢17mo ago
yes it seems like .net 4.8 doesn't like the source generator of mvvm toolkit
antimatter8189
antimatter8189ā€¢17mo ago
thats interesting any fix?
ACiDCA7
ACiDCA7ā€¢17mo ago
oh i didnt even check which target was set.. good point
antimatter8189
antimatter8189ā€¢17mo ago
yup am stuck on that 4.8
ACiDCA7
ACiDCA7ā€¢17mo 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
antimatter8189ā€¢17mo ago
Yup That works but how do we make source generators work with 4.8?
ACiDCA7
ACiDCA7ā€¢17mo 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
antimatter8189ā€¢17mo ago
legacy project mate not my choice
hengi
hengiā€¢17mo ago
ohhhh
antimatter8189
antimatter8189ā€¢17mo ago
millions of lines of code wont be switched to .net core overnight
hengi
hengiā€¢17mo ago
you need to do right click on the calculator project and migrate to PackageReference it's packages.config not working
antimatter8189
antimatter8189ā€¢17mo ago
hengi sorry how do i migrate? what? give more details mate šŸ˜„
ACiDCA7
ACiDCA7ā€¢17mo ago
hengi
hengiā€¢17mo 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ā€¢17mo ago
net framework is stuck on 7.3
antimatter8189
antimatter8189ā€¢17mo ago
yup it is
hengi
hengiā€¢17mo ago
it's not stuck, just edit the csproj
antimatter8189
antimatter8189ā€¢17mo ago
shit lol
ACiDCA7
ACiDCA7ā€¢17mo ago
i dont think srcgens are included in latest but lemme try
hengi
hengiā€¢17mo 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
antimatter8189ā€¢17mo 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ā€¢17mo 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ā€¢17mo ago
indeed it does work
antimatter8189
antimatter8189ā€¢17mo ago
sec doing it
ACiDCA7
ACiDCA7ā€¢17mo ago
i was under the impression that the MS sourcegens were jsut for net5+.. well TIL
antimatter8189
antimatter8189ā€¢17mo ago
soz am kinda lost lol trying but
hengi
hengiā€¢17mo ago
it's actually .NET Standard 2.0 I guess..
antimatter8189
antimatter8189ā€¢17mo ago
no Default package management format exsist in the setting
antimatter8189
antimatter8189ā€¢17mo 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ā€¢17mo ago
oh you only need to do the right click thing you don't need to change the setting
antimatter8189
antimatter8189ā€¢17mo 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ā€¢17mo ago
@AntiMatter you can either look at the branch or just merge, it's the new LangVersion and changing to ProjectReference
antimatter8189
antimatter8189ā€¢17mo ago
I merged jesus lol thank you so much i will go over what u did and learn from it
hengi
hengiā€¢17mo 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ā€¢17mo ago
meeh i was just about to create a quick gif how to do it..xD
antimatter8189
antimatter8189ā€¢17mo 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ā€¢17mo ago
ACiDCA7
ACiDCA7ā€¢17mo ago
hope you like it.. ignore that its in german^^
antimatter8189
antimatter8189ā€¢17mo ago
amazing dude thank you so much you 2 my gods will sacrifice goat tonight @ACiDCA7 @hengi šŸ˜„
Accord
Accordā€¢17mo 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.
Want results from more Discord servers?
Add your server
More Posts
ā” Razor Page - Select Tag Helper (form-select): Need help with onchange event for drop-down listIn my APR.cshtml page I have the following drop down list present: `<select asp-for="@Model.ServerNaā” EF Core setting property with value converter to nulli have a value object ```cs public record ShippingOrderNumber(int Number);``` that i'm mapping to ā” Some bug i guess**Code that works** ``` Task.Run(() => Static_class_name.START(param1, param2, param3)); ``` *insideā” Visual Studio not producing templates?Hey everyone, I'm attempting to teach myself C# via this website " https://www.w3schools.com/cs/cs_gā” Confusing ClientWebSocket.ReceiveAsync() behaviorPicture the following class: ```cs private ClientWebSocket _ws = new(); private Memory<byte> _buffeā” Better way to do this random password generation programThis program generates a random password then removes duplicates and excludes some chars. I need sugāœ… Validation Error with Discord.Addons.HostingI'm trying to use Discord.Addons.Hosting, but it throws an OptionsValidationException . My main lookā” Need help with zooming a user control.I want to zoom a control based on the current cursor location. I have attached two files: In Form2āœ… stateful versus stateless methods?Could someone please give me a simple example of these 2 methods as I don't really get what they doā” IL weaving with Mono.Cecil in Unity isn't affecting Unity's assembly.Hello, I wrote a simple program that finds methods decorated with an [Rpc] attributes, which works a