【问题标题】:CustomControl TextBox is inaccesible自定义控件文本框无法访问
【发布时间】:2013-09-09 17:31:28
【问题描述】:

我正在开发 Windows 8 平板应用程序

这是我的自定义用户控件:

<Grid>
    <TextBox x:Name="Points" Visibility="Collapsed" IsHitTestVisible="False" Grid.ColumnSpan="3" Grid.Column="4" HorizontalAlignment="Left" Margin="48,10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="0" VerticalAlignment="Top" TextChanged="TextBox_TextChanged"/>
</Grid>

我放在 MainPage 表单上:

<local:MyUserControl1 x:Name="Test" HorizontalAlignment="Left" Margin="280,493,0,0" VerticalAlignment="Top" Width="584" Height="166"/>

在 C# 中(MAinPage.xaml.cs):

Test.Points.Text = "";

错误:

Gamification.MyUserControl1.Points' is inaccessible due to its protection level

在 Windows Phone 8 中我没有这些问题...

如何解决?

编辑 2:

控件已添加:右键单击 -> 添加 -> 新建项目 -> 用户控件

完全控制 XAML:

<UserControl
x:Class="Gamification.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Gamification"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="200"
d:DesignWidth="800">

<Grid>
    <TextBox x:Name="Points" Visibility="Collapsed" IsHitTestVisible="False" Grid.ColumnSpan="3" Grid.Column="4" HorizontalAlignment="Left" Margin="48,10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="0" VerticalAlignment="Top" TextChanged="TextBox_TextChanged"/>
</Grid>
</UserControl>

完全控制 C#:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace Gamification
{
public sealed partial class MyUserControl1 : UserControl
{
    public MyUserControl1()
    {
        this.InitializeComponent();
    }

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {

    }
}
}

主页 XAML:

<Page
x:Class="Gamification.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Gamification"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <local:MyUserControl1 x:Name="Test" HorizontalAlignment="Left" Margin="280,493,0,0" VerticalAlignment="Top" Width="584" Height="166"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="670,692,0,0" VerticalAlignment="Top" Click="Button_Click"/>
</Grid>
</Page>

主页 C#:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        Test.Points.Text = "";
    }

【问题讨论】:

  • 在使用任何基于 XAML 的技术编写单行代码之前学习 MVVM。
  • 向我们展示Gamification.MyUserControl1.Points 或整个班级以及您如何访问该成员。
  • 更新了我的问题。有两个主要问题,如何使这种构造工作,以及为什么相同的构造可以在 Windows Phone 上工作

标签: c# xaml windows-8 windows-phone-8 windows-8.1


【解决方案1】:

您的 UserControl 本身就是一个类型。它只会暴露那些为自己公开的元素。您的控件中的 x:Name'd 元素只能对其自身进行访问,除非您将它们公开给 UserControl。

您需要公开一个属性,以允许 UserControl 的使用者设置该属性,进而更改您的内部元素。这通常通过该 DP 上的 DependencyProperty 和 PropertyChange 通知来完成。

【讨论】:

    【解决方案2】:

    您可以在 TextBlock 上添加 x:FieldModifier="public" 以使其在用户控件之外可用。
    请记住,这不是一个非常好的做法,因为它会破坏 UserControl 的封装(在您的情况下UserControl 应该公开一个 PointText 依赖属性,这样使用该控件的页面就不需要知道这个文本是如何显示的,使用依赖属性也可以让你使用这个属性的绑定。

    【讨论】:

    • 谢谢,这是我想要的。也许您可以向我推荐一些针对初学者的 MVVM 书籍/视频课程? artjomgsd@inbox.lv
    • 我觉得 mvvmlight creator 制作的两个视频:galasoft.ch/mvvmvideo1galasoft.ch/mvvmvideo2 可以是一个好的开始。
    猜你喜欢
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多