无法从另一个框架更新文本框,该如何解决?

时间:2019-05-15 08:48:06

标签: c# visual-studio xaml uwp

我正在创建一个像智能手机上的小数字键盘,当我按下一个键时,数字必须写在文本框中。 这是我遇到的问题,按按钮时无法更新TextBox。

我的窗口被分解为两个相同宽度的框架,键盘在左框架上,而TextBox在右框架上。

我在Clavier.xaml.cs页面中调用一个方法:

CallSection EtablissementAppel = new CallSection();

private void UpdateTextBox(string _UpdatePrefix, string _UpdateNumeroDestinataire)
{
    EtablissementAppel.UpdateEtablissementAppel(_UpdatePrefix, _UpdateNumeroDestinataire);
}

CallSection.xaml.cs文件中的方法:

public void UpdateEtablissementAppel(string p, string n)
{
     string Numero = p + n;
     EtablissementAppel.Text = Numero;
}

代码CallSection.xaml:

<TextBox x:Name="EtablissementAppel" Margin="5,5,5,5" PlaceholderText="ETABLISSEMENT D'APPEL" VerticalAlignment="Center" Grid.Column="0" Grid.ColumnSpan="2" Height="37"/>

但是TextBox不想自我更新。知道我可以在文件CallSection.xaml.cs中的任何地方更改其文本,但不能在此方法中更改。

编辑:

提供有关第一个评论的更多细节。

CallSection.xaml.cs中带有Debug.WriteLine()的方法:

public void UpdateEtablissementAppel(string p, string n)
{
    string Numero = p + n;
    Debug.WriteLine(Numero);
    EtablissementAppel.Text = Numero;
}

在这里,我可以在调试器中看到所有被点击的数字。但是文本框不会更新。

调试器输出:

12255
122552
1225525
12255255
122552555

CallSection.xaml:

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:PhonieMartha"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Custom="using:Microsoft.UI.Xaml.Controls"
    x:Class="PhonieMartha.CallSection"
    mc:Ignorable="d" Loaded="Page_Loaded" 
    >

    <Page.Resources>
        <SolidColorBrush x:Key="GraySelected" Color="#FFA3A3A3"/>
        <SolidColorBrush x:Key="GrayUnselected" Color="#33000000"/>
    </Page.Resources>

    <Page.Background>
        <ThemeResource ResourceKey="ApplicationPageBackgroundThemeBrush"/>
    </Page.Background>

    <Grid x:Name="GrillePrincipale">

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="3*"/>
            <RowDefinition Height="3*"/>
            <RowDefinition Height="3*"/>
            <RowDefinition Height="3*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <TextBox x:Name="EtablissementAppel" Margin="5,5,5,5" PlaceholderText="ETABLISSEMENT D'APPEL" VerticalAlignment="Center" Grid.Column="0" Grid.ColumnSpan="2" Height="37"/>
        <Rectangle x:Name="Call1" HorizontalAlignment="Stretch" Margin="5,5,5,5" VerticalAlignment="Stretch" Fill="#33000000" FocusVisualPrimaryBrush="#FFE03535" Grid.Row="1" Grid.Column="0" Tapped="Call1_Tapped"/>
        <Rectangle x:Name="Call2" HorizontalAlignment="Stretch" Margin="5,5,5,5" VerticalAlignment="Stretch" Fill="#33000000" FocusVisualPrimaryBrush="#FFE03535" Grid.Row="1" Grid.Column="1" Tapped="Call2_Tapped"/>
        <Rectangle x:Name="Call3" HorizontalAlignment="Stretch" Margin="5,5,5,5" VerticalAlignment="Stretch" Fill="#33000000" FocusVisualPrimaryBrush="#FFE03535" Grid.Row="2" Grid.Column="0" Tapped="Call3_Tapped"/>
        <Rectangle x:Name="Call4" HorizontalAlignment="Stretch" Margin="5,5,5,5" VerticalAlignment="Stretch" Fill="#33000000" FocusVisualPrimaryBrush="#FFE03535" Grid.Row="2" Grid.Column="1" Tapped="Call4_Tapped"/>
        <Rectangle x:Name="Call5" HorizontalAlignment="Stretch" Margin="5,5,5,5" VerticalAlignment="Stretch" Fill="#33000000" FocusVisualPrimaryBrush="#FFE03535" Grid.Row="3" Grid.Column="0" Tapped="Call5_Tapped"/>
        <Rectangle x:Name="Call6" HorizontalAlignment="Stretch" Margin="5,5,5,5" VerticalAlignment="Stretch" Fill="#33000000" FocusVisualPrimaryBrush="#FFE03535" Grid.Row="3" Grid.Column="1" Tapped="Call6_Tapped"/>
        <Rectangle x:Name="Call7" HorizontalAlignment="Stretch" Margin="5,5,5,5" VerticalAlignment="Stretch" Fill="#33000000" FocusVisualPrimaryBrush="#FFE03535" Grid.Row="4" Grid.Column="0" Tapped="Call7_Tapped"/>
        <Rectangle x:Name="Call8" HorizontalAlignment="Stretch" Margin="5,5,5,5" VerticalAlignment="Stretch" Fill="#33000000" FocusVisualPrimaryBrush="#FFE03535" Grid.Row="4" Grid.Column="1" Tapped="Call8_Tapped"/>
        <TextBox x:Name="RenvoiAppel" Margin="5,5,5,5" PlaceholderText="RENVOI D'APPEL" VerticalAlignment="Stretch" Grid.Column="0" Grid.ColumnSpan="2" Height="37" Grid.Row="5" InputScope="Number"/>

        <TextBox x:Name="MessageRecu" Margin="5,5,5,5" Text="" VerticalAlignment="Stretch" Grid.Column="2" Grid.Row="4" Grid.ColumnSpan="2"/>
        <ToggleSwitch x:Name="ToggleSwitchRenvoiAppel" Margin="5,5,5,5" OnContent="On" OffContent="Off" Grid.Column="2" Grid.Row="5" HorizontalAlignment="Center" Toggled="ToggleSwitchRenvoiAppel_Toggled"/>
        <TextBlock Grid.Column="3" Margin="5,5,5,5" Grid.Row="5" Text="PhonieMartha                                   Rev1.0" TextWrapping="Wrap" VerticalAlignment="Stretch" TextAlignment="Center" FontSize="12"/>


    </Grid>
</Page>

这是包含两个框架的MainPage的XAML代码:

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

    <Grid x:Name="GrilleMainPage">

        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Frame x:Name="CallsSection" Grid.Column="1"/>

        <NavigationView x:Name="NavView" SelectionChanged="NavView_SelectionChanged" IsBackButtonVisible="Collapsed" PaneDisplayMode="Auto" CompactModeThresholdWidth="300" ExpandedModeThresholdWidth="550" Grid.Column="0">

            <NavigationView.MenuItems>

                <NavigationViewItem Icon="PhoneBook" Content="Annuaire général" Tag="AnnuaireGeneral"/>

                <NavigationViewItem Icon="PhoneBook" Content="Annuaire personnel" Tag="AnnuairePersonnel"/>

                <NavigationViewItem Content="Clavier" Tag="Clavier">
                    <NavigationViewItem.Icon>
                        <FontIcon Glyph="" FontFamily="Segoe MDL2 Assets"/>
                    </NavigationViewItem.Icon>
                </NavigationViewItem>

                <NavigationViewItem Content="Mode conférence" Tag="Conference">
                    <NavigationViewItem.Icon>
                        <FontIcon Glyph="" FontFamily="Segoe MDL2 Assets"/>
                    </NavigationViewItem.Icon>
                </NavigationViewItem>

                <NavigationViewItem Icon="Help" Content="Aide" Tag="Aide"/>

                <NavigationViewItem Icon="Admin" Content="Mode instructeur" Tag="Instructeur"/>

            </NavigationView.MenuItems>

            <Frame x:Name="ContentFrame"/>

        </NavigationView>

    </Grid>
</Page>

1 个答案:

答案 0 :(得分:1)

您正在创建CallSection的新实例,然后调用该实例的UpdateEtablissementAppel方法。显然,这不会影响您在屏幕上看到的实例。您需要获取对现有实例的引用并调用其UpdateEtablissementAppel方法。

您可以使用以下帮助方法来获取对父MainPage的引用:

private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
    var parent = VisualTreeHelper.GetParent(dependencyObject);

    if (parent == null) return null;

    var parentT = parent as T;
    return parentT ?? FindParent<T>(parent);
}

然后强制转换“ CallsSection” Content的{​​{1}}:

Frame

您还需要使private void UpdateTextBox(string _UpdatePrefix, string _UpdateNumeroDestinataire) { MainPage mainPage = FindParent<MainPage>(this); if (mainPage != null) { CallSection cellSection = mainPage.CallsSection.Content as CallSection; if (cellSection != null) cellSection.UpdateEtablissementAppel(_UpdatePrefix, _UpdateNumeroDestinataire); } } 可以从其他类访问。您可以通过从Frame的公共属性返回引用,也可以通过在XAML标记中直接使用x:FieldModifier attribute来返回对它的引用:

MainPage.xaml.cs