关于MVVMLight模式下silverlight+wcf新增数据的问题,紧急求助,在线等啊~
最近在用silverlight开发一个项目,由于接触silverlight比较浅,真是碰到太多问题。汗~
这2天一直在纠结这个问题:单表数据更新问题,高手别喷啊,呵呵
表结构:tproject (fguid,..)
想法是一个datagrid显示数据,然后点击添加按钮新增,弹出childwindow新增数据。
数据显示,弹窗都可以了,但是子窗体上输入的数据就是保存不了,不知道是绑定的问题呢,还是我实现的方式本来就是错的,自己找了实在找不出来了,求助啊~
附上代码:
主窗体view(.xaml):
<UserControl x:Class="ProjSL.UI.Views.Pro_info"
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:telerik ="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:gasoft="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"
xmlns:vm="clr-namespace:ProjSL.UI.ViewModels"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.DataContext>
<vm:Proj_infoViewModel></vm:Proj_infoViewModel>
</UserControl.DataContext>
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<Button Width="100" Height="25" Content="新增">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<gasoft:EventToCommand Command="{Binding NewProjectCommand}"></gasoft:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
<telerik:RadGridView x:Name="gList" Margin="0" Grid.Row="1"
RowIndicatorVisibility="Collapsed" IsReadOnly="False" AutoGenerateColumns="False" CanUserFreezeColumns="False"
CanUserResizeColumns="False" ItemsSource="{Binding Projects}" >
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="项目名称" DataMemberBinding="{Binding FPJNAME}" />
<telerik:GridViewDataColumn Header="项目类型" DataMemberBinding="{Binding FPJTYPENAME}" />
<telerik:GridViewDataColumn Header="项目预算" DataMemberBinding="{Binding FPJBUDGET}" />
<telerik:GridViewDataColumn Header="项目范围" DataMemberBinding="{Binding FPJARRANGE}" />
<telerik:GridViewDataColumn Header="责任人" DataMemberBinding="{Binding FPJDIRECTPERID}" />
<telerik:GridViewDataColumn Header="立项时间" DataMemberBinding="{Binding FCREATETIME}" />
<telerik:GridViewDataColumn Header="状态" DataMemberBinding="{Binding FPJSTATE}" />
<telerik:GridViewDataColumn Header="计划开始时间" DataMemberBinding="{Binding FPJPLANBEGTIME}" />
<telerik:GridViewDataColumn Header="计划完成时间" DataMemberBinding="{Binding FPJPLANENDTIME}" />
<telerik:GridViewDataColumn Header="计划工时" DataMemberBinding="{Binding FPJPLANHOUR}" />
<telerik:GridViewDataColumn Header="计划参与人数" DataMemberBinding="{Binding FPJNUMBER}" />
<telerik:GridViewDataColumn Header="实际开始时间" DataMemberBinding="{Binding FPJACTBEGTIME}" />
<telerik:GridViewDataColumn Header="实际完成时间" DataMemberBinding="{Binding FPJACTENDTIME}" />
<telerik:GridViewDataColumn Header="实际工时" DataMemberBinding="{Binding FPJACTHOUR}" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
</Grid>
</UserControl>
主窗体(.cs)
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ProjSL.Web.DataModel;
using ProjSL.Web.DomainService;
using System.ServiceModel.DomainServices.Client;
using GalaSoft.MvvmLight.Messaging;
namespace ProjSL.UI.Views
{
public partial class Pro_info : UserControl
{
public Pro_info()
{
InitializeComponent();
Messenger.Default.Register<string>(this, "Main", msg =>
{
Proj_infoNew NewProject = new Proj_infoNew();
NewProject.Title = msg;
NewProject.Show();
});
}
}
}
主窗体viewmodel:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight;
using System.ServiceModel.DomainServices.Client;
using ProjSL.Web.DataModel;
using ProjSL.Web.DomainService;
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
namespace ProjSL.UI.ViewModels
{
public class Proj_infoViewModel : ViewModelBase
{
#region 构造器
public Proj_infoViewModel()
{
projects = new ObservableCollection<TPROJECT>();
project = new TPROJECT();
NewProjectCommand = new RelayCommand(NewProject);
OKButtonCommand = new RelayCommand<TPROJECT>(OkClick);
Messenger.Default.Register<TPROJECT>(this, "Child", AddProject);
if (!IsInDesignMode)
{
LoadOperation<TPROJECT> lop = Context.Load(Context.GetTPROJECTsQuery());
lop.Completed += new EventHandler(lop_Completed);
}
}
#endregion
#region 字段
private ProjectDomainServices Context = new ProjectDomainServices();
private ObservableCollection<TPROJECT> projects;
private TPROJECT project;
#endregion
#region 属性
public ObservableCollection<TPROJECT> Projects
{
get { return projects; }
set
{
if (value!=null)
{
projects = value;
RaisePropertyChanged("Projects");
}
}
}
public TPROJECT Project
{
get { return project; }
set
{
if (value != null)
{
project = value;
RaisePropertyChanged("Project");
}
}
}
/// <summary>
/// 主窗体新增按钮命令
/// </summary>
public RelayCommand NewProjectCommand{ get; private set; }
/// <summary>
/// 新增窗体确定按钮
/// </summary>
public RelayCommand<TPROJECT> OKButtonCommand { get; private set; }
#endregion
#region 方法
public void lop_Completed(object sender,EventArgs e)
{
LoadOperation<TPROJECT> loadop = (LoadOperation<TPROJECT>)sender;
if (!loadop.HasError)
{
projects.Clear();
foreach (var item in loadop.Entities)
{
projects.Add(item);
}
}
}
/// <summary>
/// 主窗体新增按钮
/// </summary>
private void NewProject()
{
Messenger.Default.Send("New Project", "Main");
}
private void AddProject(TPROJECT param)
{
projects.Add(param);
RaisePropertyChanged("Projects");
Context.SubmitChanges(OnInsertCompleted, param);
}
private void OnInsertCompleted(SubmitOperation so)
{
if (!so.HasError)
{
Projects.Insert(0, so.UserState as TPROJECT);
}
}
/// <summary>
/// 新增窗口确定按钮
/// </summary>
/// <param name="param"></param>
private void OkClick(TPROJECT param)
{
//MessageBox.Show(Student.Name);
Messenger.Default.Send(param, "Child");
}
#endregion
}
}
[解决办法]
Context.SubmitChanges(OnInsertCompleted, param);
确定OnInsertCompleted是否执行,确定param是一个新New的Project对象,才有可能插入成功,如果你2个View使用同一个ViewModel,点新增弹出新窗体不一定会新增一个Project实例