【发布时间】:2018-02-13 15:00:19
【问题描述】:
在我用元素绑定制作了一些 kata 之后,我只是想 启动 MVVM 属性绑定。
由于某种原因,我收到了 ErrorCode: CS1061
错误 CS1061
MainWindow不包含CalculateClick并且没有扩展方法CalculateClick接受 可以找到MainWindow类型的第一个参数(您是否缺少 使用指令还是程序集引用?)
这其中奇怪的部分是当我在我的按钮上的MainWindow.xml 上使用 resharper 事件处理程序时,它会在我的MainWindow.cs 中创建一个事件。
但之前没有。这些事件是在我的 ViewModel.cs 中自动创建的
我不确定是什么导致了这个错误 当有人可以帮助我的时候提前 ty,我已经坐在这个 kata 上超过 8 个小时了。
这是我的MainWindow.xml:
<Window x:Class="Coordinates.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:local="clr-namespace:Coordinates"
mc:Ignorable="d"
Title="MainWindow" Height="250" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Insert X-Coordiante"/>
<Label Grid.Row="0" Grid.Column="1" Content="Insert Y-Coordinate"/>
<TextBox Grid.Row="1" Grid.Column="0" Name="TxtXaxis" Text="{Binding Xaxis}"/>
<TextBox Grid.Row="1" Grid.Column="1" Name="TxtYaxis" Text="{Binding Xaxis}"/>
<TextBox Grid.Row="2" Grid.Column="0" Text="{Binding Output}"/>
<Button Grid.Row="2" Grid.Column="1" Name="Calculate" Click="CalculateClick">Calculate</Button>
</Grid>
这是我的MainWinow.xml.cs:
using System.Windows;
namespace Coordinates
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.InitializeComponent();
this.DataContext = new ViewModel();
}
}
}
这是我的ViewModel.cs:
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
namespace Coordinates
{
public class ViewModel : INotifyPropertyChanged
{
#region Inputs
private double _xaxis;
public double Xaxis
{
get => this._xaxis;
set
{
if (value == this._xaxis)
{
return;
}
this._xaxis = value;
this.OnPropertyChanged();
}
}
private double _yaxis;
public double Yaxis
{
get => this._yaxis;
set
{
if (value == this._yaxis)
{
return;
}
this._yaxis = value;
this.OnPropertyChanged();
}
}
#endregion
public void CalculateClick(object sender, RoutedEventArgs e)
{
Output = Math.Sqrt(Math.Pow(Xaxis,2)+Math.Pow(Yaxis,2));
}
private double _output;
public double Output
{
get => this._output;
set
{
if (value == this._output)
{
return;
}
this._output = value;
this.OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
编辑:这在我的视图模型中的 CalculateClick 中丢失了: 输出 = Math.Sqrt(Math.Pow(Xaxis,2)+Math.Pow(Yaxis,2));
【问题讨论】:
-
它正在搜索
MainWindow中的CalculateClick。要将按钮绑定到 ViewModel,请参阅 stackoverflow.com/questions/12422945/… -
按钮的名称应该是CalculateClick,并且不要设置点击处理程序。你用的是什么 mvvm 框架?
-
我正在使用 resharper 和 PropertyChangedAnalyzers 数据包