C# 简单计算器应用程序按钮单击事件上的铁路超高更新屏幕(文本框)
我是c#windows phone开发的新手,我试图使用mvvm设计模式构建一个简单的计算器应用程序。我有一个模型类,当前有一个字符串变量。这是绑定到我的计算器屏幕,这是一个文本框。我还有一个数字“1”按钮。按下此按钮时,我希望文本框“屏幕”更新该值。这是在实现ICommand接口的命令类中完成的。不幸的是,这目前不起作用,我无法找出问题所在。下面是我的模型课。这是非常基本的C# 简单计算器应用程序按钮单击事件上的铁路超高更新屏幕(文本框),c#,mvvm,windows-phone-8,C#,Mvvm,Windows Phone 8,我是c#windows phone开发的新手,我试图使用mvvm设计模式构建一个简单的计算器应用程序。我有一个模型类,当前有一个字符串变量。这是绑定到我的计算器屏幕,这是一个文本框。我还有一个数字“1”按钮。按下此按钮时,我希望文本框“屏幕”更新该值。这是在实现ICommand接口的命令类中完成的。不幸的是,这目前不起作用,我无法找出问题所在。下面是我的模型课。这是非常基本的 namespace PhoneApp2.model { public class Sum : INotifyProper
namespace PhoneApp2.model
{
public class Sum : INotifyPropertyChanged
{
private string _enteredVal;
public string EnteredVal
{
get { return _enteredVal ; }
set
{
_enteredVal = value;
RaisePropertyChanged("EnteredVal");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
接下来是主页的xaml文件。目前,我有一个1号按钮链接到一个命令类execute()方法。我的屏幕文本框绑定到我输入的值字符串。很明显,这看起来不是一个理想的方式,但我是这样学习的,因为我在实习的地方开发了一些应用程序
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.Resources>
<commands:UpdateScreenCommand x:Key="myCommand"/>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="Calculator" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,143,0,0" >
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Button Content="1" Command="{StaticResource myCommand}" CommandParameter="{Binding}" Height="Auto" Width="Auto" Grid.Row="0" Grid.Column="0"/>
<Button Content="2" Height="Auto" Width="Auto" Grid.Row="0" Grid.Column="1"/>
<Button Content="3" Height="Auto" Width="Auto" Grid.Row="0" Grid.Column="2"/>
<Button Content="+" Height="Auto" Width="Auto" Grid.Row="0" Grid.Column="3"/>
<Button Content="4" Height="Auto" Width="Auto" Grid.Row="1" Grid.Column="0"/>
<Button Content="5" Height="Auto" Width="Auto" Grid.Row="1" Grid.Column="1"/>
<Button Content="6" Height="Auto" Width="Auto" Grid.Row="1" Grid.Column="2"/>
<Button Content="-" Height="Auto" Width="Auto" Grid.Row="1" Grid.Column="3"/>
<Button Content="7" Height="Auto" Width="Auto" Grid.Row="2" Grid.Column="0"/>
<Button Content="8" Height="Auto" Width="Auto" Grid.Row="2" Grid.Column="1"/>
<Button Content="9" Height="Auto" Width="Auto" Grid.Row="2" Grid.Column="2"/>
<Button Content="/" Height="Auto" Width="Auto" Grid.Row="2" Grid.Column="3"/>
<Button Content="0" Height="Auto" Width="Auto" Grid.Row="3" Grid.Column="1"/>
<Button Content="*" Height="Auto" Width="Auto" Grid.Row="3" Grid.Column="3"/>
<Button Content="C" Height="Auto" Width="Auto" Grid.Row="3" Grid.Column="0"/>
<Button Content="=" Height="Auto" Width="Auto" Grid.Row="3" Grid.Column="2"/>
</Grid>
<Grid MinHeight="107" Margin="10,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="458" RenderTransformOrigin="0.467,-0.089">
<TextBox Height="Auto" Text="{Binding EnteredValue, Mode=OneWay}" TextWrapping="Wrap" VerticalAlignment="Center" Width="Auto" />
</Grid>
</Grid>
我也知道这有点像mvvm的伪善,但我正在努力学习如何在我工作的地方实现它。非常感谢您的帮助。从您当前的MVVM实现中,最让我困扰的是命令。基本上,我们只需要创建一个实现ICommand的类。然后我们可以从该类创建多个属性,每个属性可以执行不同的方法/代码。ICommand的示例实现来自: 然后,我们通常将上述命令属性放在ViewModel中:
public class Sum : INotifyPropertyChanged
{
private ICommand _addOneCommand;
public ICommand AddOneCommand
{
get
{
return _addOneCommand
?? (_addOneCommand = new ActionCommand(() =>
{
EnteredVal += "1";
}));
}
}
.....
.....
}
假设页面的DataContext已正确设置为Sum
,我们可以将按钮绑定到Sum
中的命令属性,如下所示:
<Button Content="1" Command="{AddOneCommand}" Height="Auto" Width="Auto" Grid.Row="0" Grid.Column="0"/>
有关在Windows Phone应用程序中使用MVVM的入门教程:
- (这个包括命令绑定)
Execute
方法中放置断点以确保它被触发?或者如果有人可以为我指出一个好的mvvm教程的方向,该教程将非常好,没有异常并且_enteredvalue设置为1。当我调试this.propertychanged等于null时,它跳过if(this.propertychanged!=null)),因此在调用RaisePropertyChanged()方法之前,基本上一切都在运行。另外,我认为可能不需要一个命令,只需在claculator屏幕上添加一个数字,因为这只是更改视图。可能是使用了错误的设计模式我认为这是正确的模式。它正在更改数据,并查看和显示更新的数据。例如,仅更改视图意味着更改按钮的宽度/高度、更改背景颜色等,而不更改数据。在这种情况下,最好实现接受参数的ICommand。因此,您可以将所有按钮绑定到单个命令,每个按钮设置为向命令传递不同的参数。是的,这里使用的MVVMIAM的特定实现显然是错误的
public class Sum : INotifyPropertyChanged
{
private ICommand _addOneCommand;
public ICommand AddOneCommand
{
get
{
return _addOneCommand
?? (_addOneCommand = new ActionCommand(() =>
{
EnteredVal += "1";
}));
}
}
.....
.....
}
<Button Content="1" Command="{AddOneCommand}" Height="Auto" Width="Auto" Grid.Row="0" Grid.Column="0"/>