c# - InvalidOperationException, the calling thread should be STA because -
i developping project in order manipulate lot of objects several modalities (mouse, leapmotion, touch ...). made using mvvm pattern soi have several views , viewmodels components use. create easier develop chose have canvas component in manipulate grids. each grid can contain type of object (shape, text, image, documents...).
to able have modalities linked method, decided build 1 listener per modality (1 mouse, 1 leapmotion...) , create them observe basic gestures (as click, doubleclick ...). gestures chose observe associate method via dictionary. anyway linking working expected executes right method. t o give illustration have action calling in mouse listener:
if (_leftclickcounter == 1 && _capturedleft == false) { if (_dic.containskey(key.onclick)) { action<object> action = _dic[key.onclick]; action.invoke(null); } }
where:
_dic dictionary key enumeration of gestures (as onclick, ondoubleclick ...) action method executein illustration method executed is:
public void add(object sender) { objectmodel objectmodel = new objectmodel(); objectview objectview = new objectview(objectmodel); this.objectviews.add(objectview); }
where sender used test purpose. remains unused in method. execution stops when tries instanciate objectview saying:
invalidoperationexception calling thread must sta, because many ui components require
my objectview.xaml.cs class is:
public partial class objectview : usercontrol { public objectview(objectmodel obj) { initializecomponent(); eventlinker linker = new eventlinker(this.visualbox); objectviewmodel objectvm = new objectviewmodel(obj, linker); this.datacontext = objectvm; } }
and objectview.xaml defining usercontrol utilize basic:
<usercontrol x:class="ausytouchmultimodal_v1.view.objectview" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:ignorable="d" d:designheight="300" d:designwidth="300"> <grid x:name="visualbox" background="blue"/> </usercontrol>
i dont have compilation errors, invalidoperationexception. can explain issue me?
thanks!
try calling actions in ui thread, this
if (_leftclickcounter == 1 && _capturedleft == false) { if (_dic.containskey(key.onclick)) { action<object> action = _dic[key.onclick]; // action.invoke(null); system.windows.application.current.dispatcher.begininvoke( phone call action ) } }
c# wpf mvvm invalidoperationexception
No comments:
Post a Comment