在mvvm中,视图不会更新 [英] In mvvm the view doesn't update
本文介绍了在mvvm中,视图不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是mvvm concept的初学者.所以我尝试了一个示例应用程序.它包含两个文本框:名称和ID,一个提交按钮,一个标签.当我单击提交按钮时,它将文本框中的两个字符串组合并显示在标签中. />
我可以提交值,并且在viewmodel中该属性包含result.但是在view.why ..?
中未显示
在视图中包含
< 网格 > ;
< TextBox =" 1" Grid.Row 1" 文本 {绑定名称}" 高度 =" 保证金 = " N ame =" 宽度 =" 120" ">/ >
< TextBox =" 1" Grid.Row 2" 文本 {Binding id}" 高度 =" 保证金 =" 9" 名称 txtid" 宽度 =" / < 按钮 =" {绑定提交}" 内容 =" Grid.Column =" 2" Grid.Row =" 高度 =" 名称 btnsubmit" 宽度 =" 75" ">/ < 标签 =" {绑定显示}" Grid.Row =" 高度 =" 38" HorizontalAlignment =" 名称 lbldisplay " 宽度 =" / < /grid >
view.cs代码为
public MainWindow()
{
InitializeComponent();
DataContext = 新 DemoViewModel();
}
在我的视图模型中包含两个.cs文件
一个是DemoViewModelInotify.cs.在这里,我编写了用于inotifypropertychanged的代码.
另一个是DemoViewModel.cs.其中包含属性和命令.
命名空间 mvvmdemonixon.ViewModel
{
公共 类 DemoViewModel:DemoViewModelInotify
{
公共 ICommand提交{ get ; 设置; }
公共 字符串 name { get ; 设置; }
公共 字符串 display { get ; 设置; }
公共 int id {获取; 设置;}
公共 DemoModel模型{ get ; 设置; }
公共 DemoViewModel()
{
提交= 新建 DelegateCommand< object>(此.添加);
}
公共 无效 添加(对象参数)
{
字符串 a = mvvmdemonixon.Model.DemoModel.addstring(name,id);
显示= a;
}
}
}
我的模型包含
命名空间 mvvmdemonixon.Model
{
公共 类 DemoModel
{
公共 静态 字符串 addstring(字符串 name1, int no1)
{
字符串 display = " +名称1 + " + no1 + " ;
返回显示;
}
}
}
在我的app.xaml.cs
私有 void OnStartup(对象发件人,StartupEventArgs e)
{
mvvmdemonixon.MainWindow视图= 新 MainWindow();
view.DataContext = 新 mvvmdemonixon.ViewModel.DemoViewModel();
view.Show();
}
在我的应用程序xaml
< 应用程序 x:Class ="
启动 =" < /Application >
谢谢..
解决方案
您需要实现INotifyPropertyChanged接口以更新MVVM中的视图.
http://www.dotnetspider.com/resources/43899-Learn-MVVM- Basics-With-Exmaple.aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/ms743695.aspx [ ^ ]
http://blog.micic.ch/net/easy-mvvm-example- with-inotifypropertychanged-and-inotifydataerrorinfo [ ^ ]
http://stackoverflow.com/a/6923833 [<grid> <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding name}" Height="23" Margin="9" Name="txtname" Width="120" /> <TextBox Grid.Column="1" Grid.Row="2" Text="{Binding id}" Height="23" Margin="9" Name="txtid" Width="120" /> <Button Command="{Binding submit}" Content="submit" Grid.Column="2" Grid.Row="2" Height="23" Name="btnsubmit" Width="75" /> <Label Content="{Binding display}" Grid.Row="3" Height="38" HorizontalAlignment="Left" Name="lbldisplay" Width="192" /> </grid>
view.cs code is
public MainWindow() { InitializeComponent(); DataContext = new DemoViewModel(); }
in my viewmodel contains two .cs file
one is DemoViewModelInotify.cs.in this i write the code for inotifypropertychanged.
another one is DemoViewModel.cs.this contain the property and commands.
namespace mvvmdemonixon.ViewModel { public class DemoViewModel :DemoViewModelInotify { public ICommand submit { get; set; } public string name { get; set; } public string display { get; set; } public int id{get;set;} public DemoModel model { get; set; } public DemoViewModel() { submit = new DelegateCommand<object>(this.add); } public void add(object paramter) { string a= mvvmdemonixon.Model.DemoModel.addstring(name, id); display = a; } } }
my model contains
namespace mvvmdemonixon.Model { public class DemoModel { public static string addstring(string name1, int no1) { string display = "The Student Name Is " + name1 + "and Id Is" + no1 + "."; return display; } } }
in my app.xaml.cs
private void OnStartup(object sender, StartupEventArgs e) { mvvmdemonixon.MainWindow view = new MainWindow(); view.DataContext = new mvvmdemonixon.ViewModel.DemoViewModel(); view.Show(); }
in my app xaml
<Application x:Class="mvvmdemonixon.App" Startup="OnStartup"> </Application>
advance thanks..解决方案You need to implement INotifyPropertyChanged interface for updating the View in MVVM.
http://www.dotnetspider.com/resources/43899-Learn-MVVM-Basics-With-Exmaple.aspx[^]
http://msdn.microsoft.com/en-us/library/ms743695.aspx[^]
http://blog.micic.ch/net/easy-mvvm-example-with-inotifypropertychanged-and-inotifydataerrorinfo[^]
http://stackoverflow.com/a/6923833[^]
Hope this links help :)
这篇关于在mvvm中,视图不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文