C# · 12月 31, 2021

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

我正在写一个 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″ 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.但该成员的可访问性不是公开的,而是内部的.

如果您确实希望该字段是公共的,则可以添加x:FieldModifier属性:

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

也就是说,最好将TextBlock对象的Text属性公开为代码隐藏中的字符串属性.或者甚至更好,使用数据绑定来控制TextBlock的Text值(但是你的代码示例不够完整,我不能说你将如何做到这一点).