0

I want to create some components and reuse them in different pages, put more than one in a page, etc.

For example, I want to create a component that contains an image, some text, etc. The position of the elements are fixed, but I will change the image, the text... I mean, in a same page I want to put three circles with different image and text...

What is the best way to do it? I've found UserControl, but I'm unable to call a method from another page to change something.

This is my component XAML

<UserControl
    x:Class="aa.Components.CircularGraph"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Components"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="300">

    <Grid Name="view">
        <Image Name="imageGraph" Source="../Assets/aa/circuloGris.png" 
       />
        <StackPanel Orientation="Vertical">
            <TextBlock Name="firstLine" Text="1" FontFamily="Arial" FontSize="9"></TextBlock>
            <TextBlock Name="secondLine" Text="2" FontFamily="Arial" FontSize="9"></TextBlock>
            <TextBlock Name="thirdLine" Text="3" FontFamily="Arial" FontSize="9"></TextBlock>
        </StackPanel>
    </Grid>
</UserControl>

Its code:

 public sealed partial class CircularGraph: UserControl {
        public CircularGraph() {
            this.InitializeComponent();

            Height = 300;
            Width = 400;
        }

        public void changeFirstLine(string var) {
            firstLine.Text = var;
        }
    }

In other page I put:

<local:CircularGraph Name="circularGraph"/>

And I've tried to put this in .cs:

protected override void OnNavigatedTo(NavigationEventArgs e) {            
             circularGraph.changeFirstLine("aaa");
}

But I have an error: The name 'circularGraph' does not exits in the current context.

How can I do this?

Sorry if it's a simple question. I'm newbie at Windows phone. Thank you very much!

A.Vila
  • 1,456
  • 4
  • 21
  • 32

1 Answers1

1

Try x:Name instead of Name. "All x:Name means to XAML is generate a field to store the value in the code behind class."

<local:CircularGraph x:Name="circularGraph"/>

In WPF, what are the differences between the x:Name and Name attributes?

Community
  • 1
  • 1
Paul Abbott
  • 7,065
  • 3
  • 27
  • 45