![](/img/trans.png)
[英]How, WCF Ria Services can make a customised entity, known at client-side?
[英]Can a WCF Ria Services entity be edited while submitting?
我有一個使用WCF RIA服務的silverlight應用程序。
我想在每次更改屬性時提交更改。
但是,如果用戶在提交完成之前嘗試更改同一實體中的另一個屬性,則會收到以下錯誤:
“此實體當前是只讀的。存在以下條件之一:已調用自定義方法,正在進行提交操作,或實體類型不支持編輯操作”
場景:
是否可以更改DataContext的行為,以便我可以在提交過程中更新實體?
請注意 - 在這個回復中我可能會非常關閉:
讓我試着得到你的意思:如果我是正確的,在界面上,值被改變,這些值被綁定到某個實體。 此實體是您希望在服務器上實時更新的內容。 如果是這種情況,我建議這樣做:假設我有一個文本框,當用戶在文本框中鍵入值時,它會更新綁定實體,我希望在服務器上更新該實體同時 - 定義一個行為來獲取keyup方法並觸發例如( AssociatedObject.GetBindingExpression(TextBox.TextProperty).UpdateSource();
)另外,在這個行為中,我會給文本框定義一個綁定方法的能力對於OnKeyUp事件,允許我說,一旦用戶按下“Enter”,然后實際在服務器上執行udpate。 另外,我會創建一個加載屏幕/子窗口來說“保存” - 然后會導致“阻塞”,以防止在保存期間進一步的用戶干預。 在回電話。 我會關閉說裝載的窗口。
現在,就像我說的那樣,我不確定這是否是你正在尋找的答案,但這個想法背后的理論,特別是在進入或最終改變時的封鎖似乎是你最好的選擇。 下面是文本框行為的示例:
包括你的庫: using CustomControlsUI.Extensions;
定義文本框的類: public class TTextBoxKeyUpBehavior:Behavior<TextBox>
定義要動態綁定到的ontextkeyup的方法指針: protected MethodInfo OnTextBoxKeyUp { get; set; }
protected MethodInfo OnTextBoxKeyUp { get; set; }
protected MethodInfo OnTextBoxKeyUp { get; set; }
定義公共字符串名稱的OnTextKeyUpMethod綁定到: OnTextBoxKeyUpMethod
在行為中定義TextboxKeyUp事件:
/// <summary>
/// Method impl. for the onkey up event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TextBoxKeyUp(object sender, KeyEventArgs e)
{
if (!object.ReferenceEquals(AssociatedObject.GetBindingExpression(TextBox.TextProperty), null))
{
AssociatedObject.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
if (!string.IsNullOrEmpty(OnTextBoxKeyUpMethod) && !object.ReferenceEquals(AssociatedObject,null) &&
!object.ReferenceEquals(AssociatedObject.DataContext,null))
{
try
{
//use reflection to try and find the method being pointed to.
if (object.ReferenceEquals(OnTextBoxKeyUp, null))
{
MethodInfo _m = null;
_m = AssociatedObject.DataContext.GetType().GetMethod(OnTextBoxKeyUpMethod, new Type[] { typeof(object), typeof(KeyEventArgs) });
if (!object.ReferenceEquals(_m, null))
//set the pointer to the on text box key up method
OnTextBoxKeyUp = _m;
}
OnTextBoxKeyUp.Invoke(AssociatedObject.DataContext, new object[] { sender, e });
}
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); }
}
}
覆蓋On Attached和On Detached方法:
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.KeyUp += TextBoxKeyUp;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.KeyUp -= TextBoxKeyUp;
}
用法:包括存儲行為的命名空間: xmlns:ccui="clr-namespace:CustomControlsUI.Behaviors;assembly=CustomControlsUI"
包括System.Windows.Interactivity和Microsoft.Expression.Interactions的名稱空間:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
在Viewmodel中附加OnKeyUpTo所需的方法:
<TextBox Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="6,4,0,0"
Name="txtFirstName" VerticalAlignment="Top" Width="200" Text="{Binding FirstName, Mode=TwoWay}">
<i:Interaction.Behaviors>
<ccui:TTextBoxKeyUpBehavior OnTextBoxKeyUpMethod="OnSearchKeyUp" />
</i:Interaction.Behaviors>
</TextBox>
在視圖模型中實現應該實現在System.ComponentModel中找到的INotifyPropertyChanged的方法和屬性
public string FirstName
{
get
{
return __fFirstName;
}
set
{
__fFirstName = value;
//this is a custom extension on INotifyPropertyChanged
this.NotifyPropertyChanged("FirstName", PropertyChanged);
}
}
最后定義key up方法以通知服務器更改:
#region "Helper Method"
public void OnSearchKeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter && IsSearchStateValid==true)
{
OnFindCustomers(sender);
}
}
#endregion
請注意:我的回答可能是非常友好的。 但它只是基於我認為你正在尋找的......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.