title: Windows Runtime Component: Variable for the UWP Image element and how to get it from XAML
body: <p>Suppose that I have a UWP app where in MainPage.xaml, what I have is an image control (or image pane) as an instance of Windows.UI.Xaml.Controls.Image (called testImage), and I have a Windows Runtime component in C++. MainPage.xaml also has a button and a button listener to run a function in the component to change the image control's source.</p>
  
  <p>Using code for the Windows Runtime component, how do I get that element that's already defined in the XAML file to change its source to a WriteableBitmap instance (holding an image) and display it in the image control? In this case, the image control is put in place by using the Visual Studio 2017 XAML Designer.</p>
  
  <p>Using this line</p>
  
  <pre><code>auto testImage = ref new Windows::UI::Xaml::Controls::Image();
  </code></pre>
  
  <p>only instantiates a new image control to be placed in MainPage.xaml itself, even when the variable name matches the value of the Name attribute for the image control as an XAML element.</p>
  
  <p>I do remember seeing the documentation and I think one StackOverflow question where it is possible to reference an XAML element between a script and a UWP app page, but that's in C#. Here, I want it in C++ because I am using OpenCV.</p>
  
  <p>EDIT: Hmm, perhaps one alternative would be to have the component simply process the image using OpenCV, pass it back to the button listener in C#, then set the source attribute of that image control to the processed image.</p>