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

9
我正在编写一个 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个回答

21

通过给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"/>

话虽如此,最好在你的代码后端将 TextBlock 对象的 Text 属性公开为 string 属性。甚至更好的是,使用数据绑定来控制 TextBlockText 值(但你的代码示例不够完整,我无法确切地说明如何做到这一点)。


2
也适用于UWP(Windows 10)。 - Muster Station
这个也适用于Xamarin Forms(至少4.8,未测试任何更低版本)。 - wislon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接