How can a UWP User Control communicate with it's containing UWP Page

Jack Detrick 1 Reputation point
2021-08-14T01:52:36.677+00:00

I'm using visual Studio 2022 Preview, C++/winRT. My app has 8 pages and one User Control that contains code common to all pages. It needs to access the page that has focus

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,861 Reputation points
    2021-08-16T01:50:53.1+00:00

    Hello, Welcome to Micorosoft Q&A,

    My app has 8 pages and one User Control that contains code common to all pages.

    For this scenario, the better way is using Messenger notification tool that comes from mvvmlight to send the message from UserrControl to focued page.

    For example

    using GalaSoft.MvvmLight.Messaging;  
    
    private void Button_Click(object sender, RoutedEventArgs e)  
    {  
        var message = "Test";  
        Messenger.Default.Send<string,TestPage>(message);  
        Tbk.Text = message;  
    }  
    

    Target Page

    public TestPage()  
    {  
        this.InitializeComponent();  
        this.Loaded += TestPage_Loaded;  
    }  
      
    private void TestPage_Loaded(object sender, RoutedEventArgs e)  
    {  
        Messenger.Default.Register<string>(this, (s) =>  
        {  
            MyTextBlock.Text = s;  
        });  
    }   
    

    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.