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

标签 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**

最佳答案

通过命名 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 属性。或者甚至更好,使用数据绑定(bind)来控制 TextBlockText 值(但您的代码示例不够完整,我无法准确说明您将如何做到这一点) .

关于c# - UserControl 元素由于其保护级别而无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28103245/

相关文章:

wpf - 如何在 WPF 中为用户控件创建用户定义(新)事件?一个小示例

c# - 如何使用适用于 .NET 的 Google 客户端 API 获取已解码的 Play Integrity API token

c# - 如何注释java/c#代码

c# - 分隔包含对象的 JSON 字符串

c# - 可以在 XAML 中选择 AncestorType 的名称

asp.net - 用户控制javascript

c# - 如何使用反射创建通用对象

xaml - 如何在具有 MVVM 体系结构的 WinRT 应用程序中重用 XAML 中的交互行为

c# - 如何实现带有页眉和页脚的 WrapPanel

asp.net - 如何从javascript控制用户控件