【问题标题】:Create a component and reuse it in Windows phone 8.1创建组件并在 Windows phone 8.1 中重用它
【发布时间】:2015-01-09 17:40:35
【问题描述】:

我想创建一些组件并在不同的页面中重用它们,在一个页面中放置多个组件等等。

例如,我想创建一个包含图像、一些文本等的组件。元素的位置是固定的,但我会更改图像、文本...我的意思是,在同一页面中我想放三个不同图片和文字的圆圈...

最好的方法是什么?我找到了 UserControl,但我无法从另一个页面调用方法来更改某些内容。

这是我的组件 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>

它的代码:

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

            Height = 300;
            Width = 400;
        }

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

在我放的其他页面中:

<local:CircularGraph Name="circularGraph"/>

我已经尝试将它放在 .cs 中:

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

但我有一个错误:名称 'circularGraph' 在当前上下文中不存在。

我该怎么做?

抱歉,这是一个简单的问题。我是 Windows 手机的新手。 非常感谢!

【问题讨论】:

    标签: xaml user-controls windows-phone-8.1 components


    【解决方案1】:

    尝试x:Name 而不是Name。 “所有x:Name 对 XAML 的含义是生成一个字段来将值存储在类后面的代码中。”

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多