question

sbastienDECORME-8081 avatar image
0 Votes"
sbastienDECORME-8081 asked sbastienDECORME-8081 action

change control from other page

Hi,
I'm coding an UDP client application with NavigationPage.
My UDP communication is coded in a class and I want to modify some control (Text or label) on page according the frame I receive.
My UDP works but I don't find the way to access control in page .
My navigationpage is instancied like this

MainPage = new NavigationPage( new MainPage());

and a control is declared like this in my stack

<Label Margin="50,0,0,0" x:FieldModifier="public" x:Name="LblConnected" Text="Disconnected" Style="{StaticResource LabelStyle}" WidthRequest="120" TextColor="Red" />

How can I make

MainPage.LblConnected.TextColor ="Green" ?


Thanks






windows-uwp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

NicoZhu-MSFT avatar image
0 Votes"
NicoZhu-MSFT answered NicoZhu-MSFT edited

Hello, Welcome to Micorosoft Q&A,

change control from other page

For your scenario, we suggest you use Xamarin.Forms MessagingCenter to approach,

If you want to edit A page content from B page, you just Subscribe B from A page , and call

Send method to tell A page to edit LblConnected text color.


For example

A

 MessagingCenter.Subscribe<B,string> (this, "Tag", (sender,arge) =>
 {
     // Do something set color for LblConnected
 })


B

  MessagingCenter.Send<B, string>(this, "Tag", "Green");


For more please refer this document


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


change control from other page


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

sbastienDECORME-8081 avatar image
0 Votes"
sbastienDECORME-8081 answered NicoZhu-MSFT commented

Hi,
Thanks good idea , don't know that.
In my Page I put the subscribe code with a parameter

         MessagingCenter.Subscribe<Page,bool>(this, "LblConnectedUpdate", (sender,arg) =>
        {
             if (arg)
             {
                 LblConnected.TextColor = Color.Green;
             }
             else
             {
                 LblConnected.TextColor = Color.Red;
            }
        })

In my class I put the sender but I've a problem

  MessagingCenter.Send<Page, bool>(this, "LblConnectedUpdate", Val);

On this I've an error:
class IHM.UDPFunctions
CS1503: Argument 1 : conversion impossible from IHM.UDPFunctions to xamarin.form.page

I understand what it means but I need to go from a class to a form.

Thanks

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Which line throw this error ?

0 Votes 0 ·
sbastienDECORME-8081 avatar image
0 Votes"
sbastienDECORME-8081 answered NicoZhu-MSFT commented

It is the line written in the class

  MessagingCenter.Send<Page, bool>(this, "LblConnectedUpdate", Val);

The error is made by the this.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Could you share more detail about IHM.UDPFunctions, and do you call Send in forms page ?

0 Votes 0 ·
sbastienDECORME-8081 avatar image
0 Votes"
sbastienDECORME-8081 answered NicoZhu-MSFT commented

I've a thread to listen UDP packet and this thread call the class UDPFunctions to decode the packet received.
In my class I've a function with a switch case on the packet ID.

UDPSocket Class

 _cancel = new CancellationTokenSource();
 Thread ThRxUDP = new Thread(TaskRx);
 ThRxUDP.Start();


 private void TaskRx()
         {
             while (!_cancel.IsCancellationRequested)
             {
                 try
                 {
                     var task = client.ReceiveAsync();
    
                     task.Wait(_cancel.Token);
                     if (task.Result.Buffer.Length > 0)
                     {
                         byte[] rxbuf = task.Result.Buffer;
                         foreach (byte DataByte in rxbuf)
                         {
                             _UDPComFunctions.ReceiveByte(DataByte);//******************************************
                             Console.WriteLine("{0:X} ", DataByte);
                         }
                     }
                 }
                 catch (OperationCanceledException)
                 { };
             }
         }

UDPFunction Class


         public void ReceiveByte(byte Data)
         {
         ....
         TestFrameID(rxbuf);
         ...
         }




         private void TestFrameID(byte[] trame)
         {
             byte IDPayload = trame[1];
             switch (IDPayload)
             {
             case Protocol.GetSoftVersion:
                 try
                 {
                     bool Val = true;
                     MessagingCenter.Send<Page, bool>(this, "LblConnectedUpdate", Val);

                 }
                 catch (Exception e)
                 {
                     Console.WriteLine(e.ToString());
                 }
                 break;
         }
     }


I received all my packet In my class now I want to update the different page of my form with the received data

Thanks






· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

please replace MessagingCenter.Send<Page, bool>(this, "LblConnectedUpdate", Val); with MessagingCenter.Send<UDPFunction, bool>(this, "LblConnectedUpdate", Val); And edit the Subscribe like the following

 MessagingCenter.Subscribe<UDPFunction,bool> (this, "Tag", (sender,arge) =>
  {
      // Do something set color for LblConnected
  })
0 Votes 0 ·
sbastienDECORME-8081 avatar image
0 Votes"
sbastienDECORME-8081 answered NicoZhu-MSFT commented

Now I've a thread exception error, this a inter thread operation forbidden in the main page when I try to change the color in line 5 or 9


 MessagingCenter.Subscribe<UDPComFunctions, bool>(this, "LblConnectedUpdate", (sender,arg) =>
             {
                 if (arg)
                 {
                     LblConnected.TextColor = Color.Green;
                 }
                 else
                 {
                     LblConnected.TextColor = Color.Red;
                 }
             });

System.Exception
HResult=0x8001010E
Message=L’application a appelé une interface qui était maintenue en ordre pour un thread différent. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
Source=Windows




· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Please edit above color in the UI thread like this

MainThread.BeginInvokeOnMainThread(() =>
{
// Code to run on the main thread
});


0 Votes 0 ·
sbastienDECORME-8081 avatar image
1 Vote"
sbastienDECORME-8081 answered

It works thanks ...
In form application I use

                     TxtBoxSoftLowerLimits.Invoke(new EventHandler(delegate { TxtBoxSoftLowerLimits.Text = Simulator.SoftLowerLimits.ToString(); }));


It is quite the same in xamarin

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.