繁体   English   中英

Windows Universal无法在C#中引用XAML控件?

[英]Windows Universal Can't Reference XAML Control in C#?

看来这不是什么大问题,但是出于任何原因,我都在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;
        }
     }

您不能因为它是一个DataTemplate 您将必须使用数据绑定。

我有一个指向非常好的教程网站的链接,但该链接已关闭。 但是,快速的Google搜索可以找到一些很好的例子。

渠道9-第17部分-了解数据绑定,数据源和数据上下文

深度数据绑定

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM