自定义控件TextBox无法访问

时间:2013-09-09 17:31:28

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

我正在开发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:

添加了控件:右键单击 - &gt;添加 - &gt;新项目 - &gt;用户控制

完全控制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 = "";
    }

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

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