Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to rais RichTextBlock_UpdatedLayout event manually in Windows 8 Metro App

Here is a sample code actual code is vary big

C#
public void rchblknew_LayoutUpdated(object sender, object e)
       {

           if (rchblknew.HasOverflowContent == true)
           {
               myspan.FontSize = 5;
               rchblknew.UpdateLayout();
           }

       }


at the same page i am raising that event

for(int i=1; i <= 3; i++)
{
 rchblknew.LayoutUpdated += new EventHandler<object>(mytest_LayoutUpdated);
}


Here i am expecting my event should rais three time but it is raising only one time when loop is completed

Please help me to rais my event three time
Posted
Comments
pramod.hegde 23-May-13 7:48am    
First of all,
rchblknew.LayoutUpdated += new EventHandler<object>(mytest_LayoutUpdated);
this line of code, only subscribes to the event. It tells what is the handler (mytest_LayoutUpdated) when the event is raised.
Since you have subscribed the event thrice, mytest_LayoutUpdated method should get executed 3 times.

Just check whether you have unsubscribed the event anywhere in the handler code..
do you have something like this in the actual code.
rchblknew.LayoutUpdated -= new EventHandler<object>(mytest_LayoutUpdated);
[no name] 23-May-13 8:00am    
No i have not unsubscribed the event anywhere in my code
Should i write that code to unsubscribe the event
pramod.hegde 23-May-13 11:45am    
No. If you want the event handler to execute many times, then do not unsubscribe.

Did you put a breakpoint in the method rchblknew_LayoutUpdated to check if it is raised 3 times? UpdateLayout won't do anything if nothing changed. Check msdn for more info:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.updatelayout.aspx[^]

Good luck!
 
Share this answer
 
You are not actually raising the event but instead listening to the event 3 times.

To raise an event you just call on it and pass any appropriate arguments.
e.g.
C#
MySpeacialEventForThisObject(this, EventArgs.Empty);


Also, most people wrap it for safety and cleanliness
e.g.
C#
private void RaiseMySpeacialEventForThisObject(MySpeacialEventData data)
{
   var handler = MySpeacialEventForThisObject;
   if(handler != null)
   {
      handler(this, data);
   }
}
<pre>


Now we have that out of the way it seems you are trying to raise a protected/private event (most events are.. in fact it is bad design likely to have the event raisable publicly). You can not do that for the UI components. They are controlled by their base classes and should be. If you need speacial eventing you can often inherit from them and add your own events.
 
Share this answer
 
v6
Comments
[no name] 24-May-13 4:04am    
thanks for your help but i got some new issue
you are right i found that my event is raising three time but in actual code i am dynamically creating some rochtextblock and i am giving name of that richtextblock as loop value increment
like Rchbk1,Rchbk2,Rchbk3....(Rchbk is common string) this countinew until loop finishes and for each richtextblock i want to rais that event but when all richtextblocks get created and loop get finish then debugger goes to that event and execute so only last richtextblock enent get raised
[no name] 24-May-13 10:25am    
I would guess that the event is being supressed until all UI updates are received and then it is raising the event. This makes sense for performance, but may not meet your requirements.

Maybe if you explain what it is you are trying to accomplish someone can give a recommendation on something that is more solid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900