【问题标题】:WPF MVVM ErrorCode: CS1061WPF MVVM 错误代码:CS1061
【发布时间】:2018-02-13 15:00:19
【问题描述】:

- the red markups are for Inputs(xAxis and yAxis) - after i press the button (Calculate), the inputs should be calculated. - then outputed at the blue markup(texbox called Output)

在我用元素绑定制作了一些 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 数据包

标签: c# xml wpf mvvm binding


【解决方案1】:

您正在使用CalculateClick 作为按钮的单击事件的事件处理程序。 默认情况下,WPF 在代码隐藏 (MainWindow.cs) 中查找事件处理程序,但该事件处理程序不存在。

因为您已经在后面的代码中初始化了 DataContext。让您的 ViewModel 成为 MainWindow 的私有成员。

在 MainWindow Code Behind 中创建一个事件处理程序,并从那里调用 ViewModel 中的事件处理程序。

public partial class MainWindow : Window
{
    private _viewModel = new ViewModel();
    public MainWindow()
    {
        InitializeComponent();
        this.InitializeComponent();
        this.DataContext = _viewModel;
    }
}

public void CalculateClick(object sender, RoutedEventArgs e)
{
    _viewModel.CalculateClick(Sender, e);
}

您可能还想查看命令,它可以直接在 ViewModel 内部创建然后绑定到。有一个 ActionCommand 或 GenericCommand,它们采用 Action 或 Action 并且非常易于使用。

【讨论】:

  • 这实际上并没有起作用,但是告诉我关于 ActionCommand 和 GenericComand 的事情
猜你喜欢
  • 2021-03-08
  • 2021-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-12
相关资源
最近更新 更多