【问题标题】:Windows Universal Can't Reference XAML Control in C#?Windows Universal 不能在 C# 中引用 XAML 控件?
【发布时间】:2016-04-18 17:18:05
【问题描述】:

这似乎不是什么大问题,但无论出于何种原因,我在 XAML 中创建了一个简单的矩形,现在无法引用它以在运行时以编程方式更改它。它应该做的是检查是否已选择国家面板之一,如果所选国家的字段之一与中心部分的名称匹配,它将突出显示中心部分底部的矩形以指示选择中心部分的用户。我可以很好地引用集线器部分本身,但我无法访问矩形的子对象。首先是其中一个中心部分的 XAML:

        <HubSection x:Name="China" Width="440" Height="460" Background="#FF343434" Header="China" IsHeaderInteractive="True" Tapped="{x:Bind HubSectionTapped}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="50,0,0,0">
            <DataTemplate>
                <Grid Height="460" Width="410" VerticalAlignment="Bottom" x:Name="ChinaBackground">
                    <Image Source="Assets/chinaFlag.bmp" x:Name="ChinaFlag"/>
                    <Rectangle x:Name="ChinaSelected_Rect" Width="410" Height="30" VerticalAlignment="Bottom" Fill="BlueViolet" Opacity="0"/>
                </Grid>
            </DataTemplate>
        </HubSection>

这是它背后的代码:

    private void Timer_SelectionIndicator(object sender, object e)
    {
        if (currentCountry1 == "China" || currentCountry2 == "China")
        {
            //this line is the one that throws an error:
            //it says it needs an object reference to the rectangle, but...
            //I don't need a reference to reference the hub section itself... why?
            ChinaSelected_Rect.Opacity = 100;
        }
     }

【问题讨论】:

  • 不确定 XAML,但在 Web 表单中,如果您有绑定控件,则必须从容器中执行 FindControl 以获取对模板中控件的引用。

标签: c# xaml reference controls win-universal-app


【解决方案1】:

你不能因为它是DataTemplate。您将不得不使用数据绑定。

我有一个非常好的教程网站的链接,但它已关闭。但是,快速谷歌搜索你可以找到一些非常好的例子。

Channel 9 - Part 17 - Understanding Data Binding, Data Sources and Data Contexts

Data binding in depth

【讨论】:

    猜你喜欢
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多