Not able to add OnManipulationEnded via Script(MRTK) - hololens

I am trying to add the Manipulation event (OnManipulationEnded) via script.
Below is the event I wanted to add.
I am trying to add this via below script:
mainobject.AddComponent<ObjectManipulator>();
mainobject.GetComponent<ObjectManipulator>().OnManipulationEnded.AddListener(() => new_position());
But this is the error that I am getting:
CS1593 Delegate 'UnityAction' does not take 0 arguments.
I do not know what arguments I need to pass
I want a new position after Gameobject is manipulated from Position A to Position B.
I have created a new function to calculate the new Positions. I want this function to be called once Manipulation is ended.
I am very new to C# and Unity. Can someone please help?
Regards,
Mayank

Please refer to ManipulationEvent Class (Microsoft.MixedReality.Toolkit.UI) | Microsoft Learn. This event has the default callback argument ManipulationEventData. So, this argument must be included in your custom function. You can refer to Unity - Scripting API: UnityEvent (unity3d.com) and the following methods.
ObjectManipulator objectManipulator;
void Start()
{
objectManipulator = GetComponent<ObjectManipulator>();
objectManipulator.OnManipulationEnded.AddListener(TouchEnded);//1
objectManipulator.OnManipulationEnded.AddListener((data) => TouchEnded(data));//2
objectManipulator.OnManipulationEnded.AddListener((data) =>{ });//3
}
private void TouchEnded(ManipulationEventData arg)
{
}

Related

How to decorate the final class DocumentGenerator

I am having problems to decorate the final class "DocumentGenerator" (in vendor/shopware/core/Checkout/Document/Service/DocumentGenerator.php) and overwrite the "generate" function inside of it.
I tried to decorate it the usual way, but an error is thrown because the "DocumentController" class excepts the original class and not my decorated one?
Argument 2 passed to Shopware\Core\Checkout\Document\DocumentGeneratorController::__construct() must be an instance of Shopware\Core\Checkout\Document\Service\DocumentGenerator
Its also not possible to extend from the class in my decorated class, because the "DocumentGenerator" is a final class.
My goal is to execute additional code, after an order document is generated. Previously I successfully used to decorate the "DocumentService" Class, but its marked as deprecated and shouldnt be used anymore. Also the "DocumentGenerator" class is used for the new "bulkedit" function for documents as of Version 6.4.14.0
I'm grateful for every tip.
As #j_elfering already wrote it's by design that you should not extend that class and therefore also shouldn't decorate it.
To offer a potential alternative:
Depending on what you want to do after a document has been generated it might be enough to add a subscriber to listen to document.written, check if it was a new document created and then work with the data from the payload for fetching/persisting data depending on that.
public static function getSubscribedEvents()
{
return [
'document.written' => 'onDocumentWritten',
];
}
public function onDocumentWritten(EntityWrittenEvent $event): void
{
foreach ($event->getWriteResults() as $result) {
if ($result->getOperation() !== EntityWriteResult::OPERATION_INSERT) {
// skip if the it's not a new document created
continue;
}
$payload = $result->getPayload();
// do something with the payload
}
}
Probably not what you want to hear but: The service is final in purpose as it is not intended to be decorated.
So the simple answer is you can't. Depending on your use case there may be other ways that don't rely on decoration.

Acumatica - An object Reference is Required or the non-static field, method, or property

hi does anyone encountered this error? everytime I use PXSelect on a foreach loop in which on the other source code does but on my code does not, could anyone identify the cause? the code below is also the the original source code from Acumatica but I only changed the Datamember from PaymentCharges to OtherCharges
[PXOverride]
public void VoidCheckProc(ARPayment doc)
{
foreach (PXResult<ARPaymentChargeTran> paycharge in PXSelect<ARPaymentChargeTran, Where<ARPaymentChargeTran.docType, Equal<Required<ARPayment.docType>>, And<ARPaymentChargeTran.refNbr, Equal<Required<ARPayment.refNbr>>>>>.
Select(this, doc.DocType, doc.RefNbr))
{
ARPaymentChargeTran charge = PXCache<ARPaymentChargeTran>.CreateCopy((ARPaymentChargeTran)paycharge);
charge.DocType = Document.Current.DocType;
charge.CuryTranAmt = -1 * charge.CuryTranAmt;
charge.Released = false;
charge.CuryInfoID = Document.Current.CuryInfoID;
charge.CashTranID = null;
//PaymentCharges.Insert(charge);
OtherCharges.Insert(charge);
}
}
I believe, you are writing this method in an extension for the base BLC
So instead of using 'this', use 'this.Base'
The Select method is non-static, as the error message says, but you call it on the PXSelect<...>-type. You need to have an instance of that type.
Based on Hybridzz answer, I assume you used the wrong overload of the Select-method. Probably your arguments do not have the correct type, so the compiler selects the best fitting overload of the method. In this case, it selects the one accepting only the argument params object[] o, which is non-static. A bit misleasing design of the API you use.

Func delegate passed over a function

I have the following code:
private void Example(Func<string,int> myDelegate)
{
int length = myDelegate(RECEIVEDSTRING).; //How to access received string??
}
When I execute the method : Example(x=>"Hello".Length); How can I access to the string "Hello" inside the method Example?
Thanks.
Regards.
Jose.
That is not how you should be processing this delegate. It takes an input parameter and Example needs to know some details about the input parameter in order to execute the delegate. Here Example "acts" as an interface to external code and that external code MUST provide their own implementation.
If Example needs to execute the delegate then it needs to provide the REQUIRED string parameter. IMHO, Example needs to provide a second parameter in its signature as below...
private void Example(string s, Func<string,int> myDelegate)
{
int length = myDelegate(s);
}
Then external code can provide is own implementation and parameter...
Example("Hello", x => { return x.Length; });
What you are trying to do is not possible in C#, the delegate needs to an explicit context to run, that context is the string input parameter.
Hope it makes sense

How to call a function in mfc...?

I am new to MFC PROGRAMMING. I use vs 2008, in a Dialog Based App. I want to call bellow function on a button click event...?
When I call like SortList(listboxone); is giving an error that SortList not found...!
Please help me..!!
void SortList(CListBox& templistbox)
{
DWORD_PTR abc;
int a=templistbox.GetCurSel();// Select current Item Index
if(a<templistbox.GetCount()-1)
{
abc = (DWORD_PTR )templistbox.GetItemData(a);
a++;
templistbox.SetItemData(a,(DWORD_PTR) templistbox.GetItemData(templistbox.GetCurSel()));
}
}
Sorry now I changed the function to as above but still gives same error.
You probably are calling the function above the function definition. In C/C++, you need to define the function (or it's prototype at least) before calling the function. Put this:
void SortList(CListBox& templistbox);
at the the top of the source file.

Get parameter values from method at run time

I have the current method example:
public void MethodName(string param1,int param2)
{
object[] obj = new object[] { (object) param1, (object) param2 };
//Code to that uses this array to invoke dynamic methods
}
Is there a dynamic way (I am guessing using reflection) that will get the current executing method parameter values and place them in a object array? I have read that you can get parameter information using MethodBase and MethodInfo but those only have information about the parameter and not the value it self which is what I need.
So for example if I pass "test" and 1 as method parameters without coding for the specific parameters can I get a object array with two indexes { "test", 1 }?
I would really like to not have to use a third party API, but if it has source code for that API then I will accept that as an answer as long as its not a huge API and there is no simple way to do it without this API.
I am sure there must be a way, maybe using the stack, who knows. You guys are the experts and that is why I come here.
Thank you in advance, I can't wait to see how this is done.
EDIT
It may not be clear so here some extra information. This code example is just that, an example to show what I want. It would be to bloated and big to show the actual code where it is needed but the question is how to get the array without manually creating one. I need to some how get the values and place them in a array without coding the specific parameters.
Using reflection you can extract the parameters name and metadata but not the actual values :
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.testMethod("abcd", 1);
Console.ReadLine();
}
public void testMethod(string a, int b)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
StackFrame sf = st.GetFrame(0);
ParameterInfo[] pis = sf.GetMethod().GetParameters();
foreach (ParameterInfo pi in pis)
{
Console.Out.WriteLine(pi.Name);
}
}
}

Resources