简体   繁体   English

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

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

This seems like it wouldn't be much of an issue, but for whatever reason I created a simple rectangle in XAML and now can't reference it to change it programatically at runtime. 看来这不是什么大问题,但是出于任何原因,我都在XAML中创建了一个简单的矩形,现在无法引用它来在运行时以编程方式对其进行更改。 What it's supposed to do is check whether one of the country panels has been selected and if one of the fields for a selected country matches the name of the hub section, it will highlight that rectangle at the bottom of the hub section to indicate to the user that the hub section is selected for as long as it is selected for. 要做的是检查是否选择了一个国家/地区面板,并且所选国家/地区的字段之一与中心部分的名称匹配,它将突出显示中心部分底部的矩形以指示用户选择轮毂部分的时间与选择时一样长。 I can reference the hub sections themselves just fine, but I can't access the child object that is the rectangle. 我可以很好地引用集线器部分,但是无法访问作为矩形的子对象。 Here's the XAML for one of the hub sections first: 这是中心部分之一的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>

and here's the code behind it: 这是其背后的代码:

    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;
        }
     }

You cant because its a DataTemplate . 您不能因为它是一个DataTemplate You will have to use Databindings. 您将必须使用数据绑定。

I had a link to a very good tutorial site but its down. 我有一个指向非常好的教程网站的链接,但该链接已关闭。 But, a quick google search you can find some very good examples. 但是,快速的Google搜索可以找到一些很好的例子。

Channel 9 - Part 17 - Understanding Data Binding, Data Sources and Data Contexts 渠道9-第17部分-了解数据绑定,数据源和数据上下文

Data binding in depth 深度数据绑定

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

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