UserControl元素由于其保护级别而无法访问

时间:2015-01-23 04:51:52

标签: c# xaml user-controls windows-phone-8.1 access-modifiers

我正在编写Windows Phone 8.1应用程序(WinRT)。

我的用户控件: XAML:

<UserControl x:Name="LoadingProgressBarPage"
x:Class="Project1.Custom.General.UserControls.LoadingOverlayFullScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Merakyahoga.com.Custom.General.UserControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"

Foreground="{x:Null}"
d:DesignHeight="800" d:DesignWidth="480" >

<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>
    <StackPanel 
        Name="LayoutRoot_StackPanelMain"
        Grid.Row="1"
                Orientation="Vertical">
        <ProgressBar Name="ProgressBar" 
                     IsIndeterminate="True"
                     Foreground="{StaticResource DefaultTheme_DarkBlueColor}" 

                     Height="50" 
                     Width="480" 
                     VerticalAlignment="Center"/>

        <StackPanel Name="LayoutRoot_SubStackPanel"
                    Orientation="Horizontal"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center">

            <Image Name="ImageHolder_Main"
                   Width="54">    
            </Image>

            <TextBlock Name="ProgressBarText"
                   Text="Please Wait.." 
                       Margin="10,0,0,0"
                   FontWeight="Normal"
                   HorizontalAlignment="Center"
                       FontSize="18"
                       Foreground="{StaticResource DefaultTheme_DarkBlueColor}" VerticalAlignment="Center"/>

        </StackPanel>

    </StackPanel>
</Grid>

CS:

namespace Project1.Custom.General.UserControls
{
    public partial class LoadingOverlayFullScreen : UserControl
    {


        public LoadingOverlayFullScreen()      
        {
            InitializeComponent()

            //WINRT:
            this.LayoutRoot.Height = Window.Current.Bounds.Height;
            this.LayoutRoot.Width = Window.Current.Bounds.Width;

        }

    }

}

在我的mainpage.cs中:

LoadingOverlayFullScreen LoadingOverlayFullScreenObject = new LoadingOverlayFullScreen();
        //test:
LoadingOverlayFullScreenObject.ProgressBarText = "Hello"; //ERROR Here >> **element inaccessible due to its protection level**

1 个答案:

答案 0 :(得分:14)

通过命名TextBlock元素,可以使XAML编译器生成具有该名称的类成员,即ProgressBarText。但该成员的可访问性不是public,而是internal

如果您确实希望该字段为public,则可以添加x:FieldModifier属性:

<TextBlock Name="ProgressBarText"
           x:FieldModifier="public"
           Text="Please Wait.." 
           Margin="10,0,0,0"
           FontWeight="Normal"
           HorizontalAlignment="Center"
           FontSize="18"
           Foreground="{StaticResource DefaultTheme_DarkBlueColor}"
           VerticalAlignment="Center"/>

也就是说,最好将Text对象的TextBlock属性公开为代码隐藏中的string属性。或者甚至更好,使用数据绑定来控制Text的{​​{1}}值(但是您的代码示例不够完整,我无法说明您将如何做到这一点)。