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

前端之家收集整理的这篇文章主要介绍了c# – UserControl元素由于其保护级别而无法访问前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在写一个 Windows Phone 8.1应用程序(WinRT).

我的用户控件:
XAML:

  1. <UserControl x:Name="LoadingProgressBarPage"
  2. x:Class="Project1.Custom.General.UserControls.LoadingOverlayFullScreen"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Merakyahoga.com.Custom.General.UserControls"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d"
  9. FontFamily="{StaticResource PhoneFontFamilyNormal}"
  10.  
  11. Foreground="{x:Null}"
  12. d:DesignHeight="800" d:DesignWidth="480" >
  13.  
  14. <Grid x:Name="LayoutRoot">
  15. <Grid.RowDefinitions>
  16. <RowDefinition Height="1*"/>
  17. <RowDefinition Height="1*"/>
  18. <RowDefinition Height="1*"/>
  19. </Grid.RowDefinitions>
  20. <StackPanel
  21. Name="LayoutRoot_StackPanelMain"
  22. Grid.Row="1"
  23. Orientation="Vertical">
  24. <ProgressBar Name="ProgressBar"
  25. IsIndeterminate="True"
  26. Foreground="{StaticResource DefaultTheme_DarkBlueColor}"
  27.  
  28. Height="50"
  29. Width="480"
  30. VerticalAlignment="Center"/>
  31.  
  32. <StackPanel Name="LayoutRoot_SubStackPanel"
  33. Orientation="Horizontal"
  34. HorizontalAlignment="Center"
  35. VerticalAlignment="Center">
  36.  
  37. <Image Name="ImageHolder_Main"
  38. Width="54">
  39. </Image>
  40.  
  41. <TextBlock Name="ProgressBarText"
  42. Text="Please Wait.."
  43. Margin="10,0"
  44. FontWeight="Normal"
  45. HorizontalAlignment="Center"
  46. FontSize="18"
  47. Foreground="{StaticResource DefaultTheme_DarkBlueColor}" VerticalAlignment="Center"/>
  48.  
  49. </StackPanel>
  50.  
  51. </StackPanel>
  52. </Grid>

CS:

  1. namespace Project1.Custom.General.UserControls
  2. {
  3. public partial class LoadingOverlayFullScreen : UserControl
  4. {
  5.  
  6.  
  7. public LoadingOverlayFullScreen()
  8. {
  9. InitializeComponent()
  10.  
  11. //WINRT:
  12. this.LayoutRoot.Height = Window.Current.Bounds.Height;
  13. this.LayoutRoot.Width = Window.Current.Bounds.Width;
  14.  
  15. }
  16.  
  17. }
  18.  
  19. }

在我的mainpage.cs中:

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

解决方法

@H_502_18@ 通过命名TextBlock元素,可以使XAML编译器生成具有该名称的类成员,即ProgressBarText.但该成员的可访问性不是公开的,而是内部的.

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

  1. <TextBlock Name="ProgressBarText"
  2. x:FieldModifier="public"
  3. Text="Please Wait.."
  4. Margin="10,0"
  5. FontWeight="Normal"
  6. HorizontalAlignment="Center"
  7. FontSize="18"
  8. Foreground="{StaticResource DefaultTheme_DarkBlueColor}"
  9. VerticalAlignment="Center"/>

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

猜你在找的C#相关文章