0

on my researches regarding "Selecting elements inside a Hub", I found this thread. The FindChildControl function works for me in most cases, but unfortunately it seems, that this function cannot address elements like Ellipses or Images. As I'm a very beginner with C# and UWP, is there any possibility to address those elements?

For example, here's a bit of code, that should address an image inside the "weatherSec" hub-section, but it's still null.

        Image ResImage = FindChildControl<Image>(weatherSec, "ResultImage") as Image;
        TextBlock ResultTextBlock = FindChildControl<TextBlock>(weatherSec, "ResultTextBlock") as TextBlock;
        //var position = await LocationManager.GetPosition();

        RootObject myWeather =
            await OpenWeatherMapProxy.GetWeather(
                51.43,
                6.75);

        string icon = String.Format("ms-appx:///Assets/Weather/{0}.png", myWeather.weather[0].icon);
        ResImage.Source = new BitmapImage(new Uri(icon, UriKind.Absolute));
        ResultTextBlock.Text = myWeather.name + " - " + ((int)myWeather.main.temp).ToString() + "°C - " + myWeather.weather[0].description;

Greetings Dada

Community
  • 1
  • 1
Dada93
  • 1
  • 2
  • I made a demo from your codes and the `FindChildControl` but didn't reproduce your problem, the image changed correctly. Could you please share a demo that can reproduce this problem? – Elvis Xia - MSFT Oct 25 '16 at 05:53
  • I've uploaded the complete project here: https://1drv.ms/u/s!AnAzwO51R3Ve71gSfFGII951rfVR Maybe this helps. – Dada93 Oct 25 '16 at 08:27

1 Answers1

0

but unfortunately it seems, that this function cannot address elements like Ellipses or Images.

I checked your demo and reproduced the problem. The problem is you placed the OnLoaded inside the MainPage Constructor, in which the XAML hasn't been fully loaded. So you didn't get the Image element in weatherSec(actually you can't get any element there, not just the Image or Eclipses).

To fix the problem, you need to put the Onloaded inside MainPage.Loaded like below:

public MainPage()
{
    this.InitializeComponent();
    InitGPIO();
    this.Loaded += MainPage_Loaded;// add event
    //OnLoaded(); //comment out this line, the xaml hasn't been fully loaded
}

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    OnLoaded();
}
Elvis Xia - MSFT
  • 10,801
  • 1
  • 13
  • 24