I have a Silverlight form that performs exception-based data validation. I learned how to do this data validation the following way:
Set controls to be validated as follows:
<TextBox Text="{Binding Mode=TwoWay,NotifyOnValidationError=True, Source={StaticResource docSan}, Path= metadati.paziente.residenza, ValidatesOnExceptions=True}"/>
Make the target property work as follows
public new string residenza
{
get { return base.residenza; }
set
{
if (string.IsNullOrEmpty(value)) throw new ArgumentNullException("value");
base.residenza = value;
}
}
Where the base class defines a non-validating property in an INotifyPropertyChanged way
Unfortunately VS2010 at design time warns me about the exception for each text box. This doesn't prevent the application from running (it works fine) but it's just annoying.
Somebody knows how to tell VS that it's OK if at design time no value is specified thus the code throws naturally?
If I understand correctly, it is the if ... throw statement in the setter that causes the warnings in the designer?
I think you can use the DesignerProperties.IsInDesignTool to prevent this line from running in design-time:
set
{
if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
{
if (string.IsNullOrEmpty(value)) throw new ArgumentNullException("value");
}
base.residenza = value;
}
Related
I try to handle a Summernote Keyup event with this:
myEditor.addSummernoteKeyUpHandler(new SummernoteKeyUpHandler() {
#Override
public void onSummernoteKeyUp(final SummernoteKeyUpEvent event) {
// TODO Auto-generated method stub
log.fine("hello");
}
});
I get a UmbrellaException which is IMHO a class cast exception.
This is the call stack
I identified the following spot where te cast failes:
#HasNoSideEffects
static native boolean canCast(Object src, JavaScriptObject dstId) /*-{
if (#com.google.gwt.lang.Cast::instanceOfString(*)(src)) {
return !!#com.google.gwt.lang.Cast::stringCastMap[dstId];
} else if (src.#java.lang.Object::castableTypeMap) {
return !!src.#java.lang.Object::castableTypeMap[dstId]; //<-- this returns false!!!
} else if (#com.google.gwt.lang.Cast::instanceOfDouble(*)(src)) {
return !!#com.google.gwt.lang.Cast::doubleCastMap[dstId];
} else if (#com.google.gwt.lang.Cast::instanceOfBoolean(*)(src)) {
return !!#com.google.gwt.lang.Cast::booleanCastMap[dstId];
}
return false;
}-*/;
dstId contains:
Any help greatly appreciated!
I tested this with a small demo which actually works. But in my large application, I get this exception and I don't see why.
Do you have any idea whats wrong here?
Best regards
Hannes
As Andrei suggested I set the style to DETAILED. I use Eclipse as a development environment. I decided to clean build the system (which I had done before). Now the problem has simply vanished !! Furthermore, I use SDBG (see: https://sdbg.github.io/) to debug my GWT application. This works pretty well (even without -style DETAILED). Now the very very strange thing remains. I can set breakpoints for my application and they all work well, except setting a breakpoint within the event handling method. I use a logger to print some text to the console, so I see that the event handler for summernote is actually called but the debugger will not stop. I checked whether the breakpoint is listed in the tab "Breakpoints" and it is and it is checked. I don't get it. Perhaps I have to rebuild all again.
But to keep long things short:
The solution to the problem is probably to really issue a clean build and then hope for the best.
I know there are several topics already on stackoverflow, but nothing that actually solves the problem. Here it is:
Because of some inherent problems with Ribbon Designer I decided to build my next Excel AddIn using XML Ribbon.
However, occasionally I need to make changes to the controls in the ribbon based on user selections. For example I need to change the text of a label, and also make some of the controls disabled in some cases. And here's where I hit a brick wall. It looks like there's no way to do it. I tried to put the logic in the onAction callback as follows:
public void LabelAction(IRibbonControl control)
{
LabelControl label = (LabelControl)control;
label.Label = "changed text";
}
But this cast doesn't work because apparently IRibbonControl interface has nothing to do with the RibbonControl class that LabelConrol inherits from.
I was also not able to find any other way to access any of the XML ribbon controls. Is there even a solution to this? Or should I stick to Ribbon Designer?
You need to do this in a routine that sets the item label.
The xml would look like this:
<button id="SkLabelTest1" getLabel="GetLabelTest" onAction="SkLabelTest1"/>
<button id="SkLabelTest2" getLabel="GetLabelTest" onAction="SkLabelTest2"/>
The routine you are interested in is getLabel
I've done a noddy routine to demonstrate this.
First I added a property to ThisAddin.cs for it to read:
public string _labelTest = string.Empty;
public string LabelTest { get { return _labelTest; } set { _labelTest = value; } }
Then in my ribbon handling code I added the getLabel routine:
public string GetLabelTest(Office.IRibbonControl control)
{
switch (control.Id.ToLower())
{
case "sklabeltest2":
if (Globals.ThisAddIn.LabelTest != string.Empty)
return Globals.ThisAddIn.LabelTest;
else
return "Label Test 2";
default:
return "Label Test 1";
}
}
This works by the SkLabelTest1 button changing the text of SkLabelTest2 and then invalidating the control to force the ribbon to reload it:
public void SkLabelTest1(Office.IRibbonControl control)
{
Globals.ThisAddIn._labelTest = "Changed text";
Globals.ThisAddIn._ribbon.InvalidateControl("SkLabelTest2");
}
I've tested just in case and it changes the text OK. Hope this helps
I couldn't make a comment because of my reputation. As a comment to Charlie's post, it is a perfect solution but on my side, I had to change one part.
I changed public void SklabelTest1 function to this one below:
public void SkLabelTest1(Office.IRibbonControl control)
{
Globals.ThisAddIn._labelTest = "Changed text";
this.ribbon.InvalidateControl("SkLabelTest2");
}
And also added this in the beginning of my ribbon class.
private Office.IRibbonUI ribbon;
I hope it helps.
I get this error when opening one specific form. The rest is working fine and I have no clue why this one isn't.
Error: An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported.
I get the error at _oDBConnection when I try to save. When I watch _oDBConnection while running through the code, it does not exist. Even when I open the main-window it does not exist. So this form is where the DataContext is built for the very first time.
Every class inherits from clsBase where the DataContext is built.
My collegue is the professional one who built it all. I am just expanding and using it (learned it by doing it). But now I'm stuck and he is on holiday. So keep it simple :-)
What can it be?
clsPermanency
namespace Reservation
{
class clsPermanency : clsBase
{
private tblPermanency _oPermanency;
public tblPermanency PermanencyData
{
get { return _oPermanency; }
set { _oPermanency = value; }
}
public clsPermanency()
: base()
{
_oPermanency = new tblPermanency();
}
public clsPermanency(int iID)
: this()
{
_oPermanency = (from oPermanencyData in _oDBConnection.tblPermanencies
where oPermanencyData.ID == iID
select oPermanencyData).First();
if (_oPermanency == null)
throw new Exception("Permanentie niet gevonden");
}
public void save()
{
if (_oPermanency.ID == 0)
{
_oDBConnection.tblPermanencies.InsertOnSubmit(_oPermanency);
}
_oDBConnection.SubmitChanges();
}
}
}
clsBase
public class clsBase
{
protected DBReservationDataContext _oDBConnection;
protected int _iID;
public int ID
{
get { return _iID; }
}
public DBReservationDataContext DBConnection
{
get { return _oDBConnection; }
}
public clsBase()
{
_oDBConnection = new DBReservationDataContext();
}
}
Not a direct answer, but this is really bad design, sorry.
Issues:
One context instance per class instance. Pretty incredible. How are you going to manage units of work and transactions? And what about memory consumption and performance?
Indirection: every entity instance (prefixed o) is wrapped in a cls class. What a hassle to make classes cooperate, if necessary, or to access their properties.
DRY: far from it. Does each clsBase derivative have the same methods as clsPermanency?
Constructors: you always have to call the base constructor. The constructor with int iID always causes a redundant new object to be created, which will certainly be a noticeable performance hit when dealing with larger numbers. A minor change in constructor logic may cause the sequence of constructor invocations to change. (Nested and inherited constructors are always tricky).
Exception handling: you need a try-catch everywhere where classes are created. (BTW: First() will throw its own exception if the record is not there).
Finally, not a real issue, but class and variable name prefixes are sooo 19xx.
What to do?
I don't think you can change your colleague's design in his absence. But I'd really talk to him about it in due time. Just study some linq-to-sql examples out there to pick up some regular patterns.
The exception indicates that somewhere between fetching the _oPermanency instance (in the Id-d constructor) and saving it a new _oDBConnection is created. The code as shown does not reveal how this could happen, but I assume there is more code than this. When you debug and check GetHashCode() of _oDBConnection instances you should be able to find where it happens.
Using the INotifyDataErrorInfo I have validation methods in my setters of my properties, this works fine if I change the field value and then leave the control (change focus) the setter gets fired and validation occurs and the UI is notified, but using that interface, if the user just presses submit how can I do something similar to RequiredField like in asp.net, I can't seem to find a clear cut example on any forum or blog :(
I'm using Silverlight 4 WCF RIA and the mvvm-light toolkit, thats it. I hope im not over complexing this, because it seems like it should be so simple but can't seem to figure out a solution.
Thank you for all your help, suggestions and pointers!!!
Finally found a simple solution... Please let me know if anyone has a more efficient way of doing this :)
private void Validate()
{
ValidationContext validationContext = new ValidationContext(this, null, null);
ICollection vr = new List();
Validator.TryValidateObject(this, validationContext, vr, true);
if (vr.Count >= 1)
{
foreach (var item in vr)
{
ManageErrors(((string[])item.MemberNames)[0], new List() { item.ErrorMessage }, true);
}
}
}
The Manage Errors method is the simple implementation of the INotifyDataErrorInfo that Jesse Liberty did here
any ways now when my submit method gets called (using mvvm-light) in my viewModel i call this and bam all properties validated using simple dataAnnotations
ie
[Required(ErrorMessage = "Is Required", AllowEmptyStrings = false)]
public string SelectedStatus
{
get { return _selectedStatus; }
set
{
_selectedStatus = value;
RaisePropertyChanged("SelectedStatus");
}
}
Not the most elegant way of doing it but... by god I could not find anyone validating on a submit!?!?
I am trying to extend the Silverlight Validation to include severity. I am taking the approach of adding a special character at the beginning of the message to indicate if it is an error or a warning, and changing the style to either red or blue. I have successfully implemented this for the Validation Summery control, input controls and ValidationToolTips by specifying a custom Style and using value converters. But I can't seem to get it to work with labels.
My problem is that I can’t seem to bind the validation message. I have tried the following without any success:
OR
Can someone please provide some help or suggest an alternative approach.
Links and or sample code is greatly appreciated.
I've solved this by throwing exceptions of different types (the ValidationWarning exception for warnings). Each time when validation exception is thrown, the BindingValidationError event is triggered. In event handler I check for exception type and change the style depending on exception:
private void OnBindingValidationError(object sender, ValidationErrorEventArgs e)
{
var textBox = sender as TextBox;
if (textBox != null)
{
if (e.Error.Exception is ValidationWarning)
{
UpdateStyle(textBox, "TextBoxWithWarning");
}
else
{
UpdateStyle(textBox, "TextBoxWithError");
}
}
}
private static void UpdateStyle(TextBox textBox, string styleName)
{
var newStyle = (Style)Application.Current.Resources[styleName];
if (textBox.Style != newStyle)
{
textBox.Style = newStyle;
textBox.Focus();
}
}