C# 如何在uwp中从主页检索文本框字符串?

C# 如何在uwp中从主页检索文本框字符串?,c#,uwp,C#,Uwp,我是UWP的新手 我已经创建了一个带有主页和用户控件的应用程序 我在用户控件中有一个文本框,我想从主页访问它的数据 有人能建议一种方法吗 感谢在用户控件的构造函数中,初始化UserControl类的静态全局实例 public UserControl() { //Initialise control } public static UserControl UserControlInstance { get { return m_UserControlIns

我是UWP的新手

我已经创建了一个带有主页和用户控件的应用程序

我在用户控件中有一个文本框,我想从主页访问它的数据

有人能建议一种方法吗


感谢

在用户控件的构造函数中,初始化UserControl类的静态全局实例

public UserControl()
{
    //Initialise control
}

public static UserControl UserControlInstance
{
    get
    {
        return m_UserControlInstance ?? new UserControl();
    }
}

private static UserControl m_UserControlInstance;
然后通过实例访问UserControl的属性

string x = UserControl.UserControlInstance.TextBox.Text;

顺便说一下,这叫singelton,以防你想查一下

在用户控件的构造函数中,初始化UserControl类的静态全局实例

public UserControl()
{
    //Initialise control
}

public static UserControl UserControlInstance
{
    get
    {
        return m_UserControlInstance ?? new UserControl();
    }
}

private static UserControl m_UserControlInstance;
然后通过实例访问UserControl的属性

string x = UserControl.UserControlInstance.TextBox.Text;

顺便说一下,这叫singelton,以防你想查一下

While@Peter有一个很好的答案,但我认为您也可以使用
FieldModifier

我的UserControl是Averili,我的xaml是:

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

    <Grid>
       <TextBox x:Name="TextBox" x:FieldModifier="public"></TextBox>
    </Grid>
</UserControl>
我还可以使用后台代码中的文本框

    public MainPage()
    {
        InitializeComponent();
        Averieli.TextBox.Text = "I can use it";
    }

虽然@Peter有一个很好的答案,但我认为你也可以使用
FieldModifier

我的UserControl是Averili,我的xaml是:

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

    <Grid>
       <TextBox x:Name="TextBox" x:FieldModifier="public"></TextBox>
    </Grid>
</UserControl>
我还可以使用后台代码中的文本框

    public MainPage()
    {
        InitializeComponent();
        Averieli.TextBox.Text = "I can use it";
    }

您好,谢谢您的回答,但是当我尝试使用UserControl.UserControlInstance.textbox从主页访问textbox时,我找不到textbox属性。但是在我的userControl中,我有一个带有x:name的文本框。您好,谢谢您的回答,但是当我尝试使用userControl.UserControlInstance.textbox从主页访问textbox时,我找不到textbox属性。但是在我的userControl中,我有一个带有x:name的文本框。