【问题标题】:Impossible to update textbox from another frame, how I can fix that?无法从另一个框架更新文本框,我该如何解决?
【发布时间】:2019-05-15 08:48:05
【问题描述】:

我正在创建一个类似于我们智能手机上的小型数字键盘,当我按下一个键时,数字必须写在一个 TextBox 中。 这是我遇到问题的地方,当我按下按钮时,我无法更新我的 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 文件中的任何位置更改其文本,但不能在此方法中更改。

编辑:

提供有关第一个 cmets 的更多详细信息。

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>

【问题讨论】:

  • 在我看来,您正在显示的 CallSection 页面和 CallSection 是不同的对象
  • 如果我在我的 UpdateEtablissementAppel 方法中添加一个 Debug.WriteLine(Numero),我可以看到所有的数字......但我的文本框没有更新......
  • 您正在创建CallSection 的新实例并调用此实例的UpdateEtablissementAppel 方法。这显然不会影响您在屏幕上看到的实例。您需要获取对现有实例的引用并调用其UpdateEtablissementAppel 方法。这取决于您的 UI 和 XAML 的结构。请发布您的 XAML。
  • @mm8 看上面的帖子,我刚刚更新了
  • @mm8 我刚刚添加了 MainPage xaml 代码。

标签: c# visual-studio xaml uwp


【解决方案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”FrameContent

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 可以从其他类访问。您可以从 MainPage.xaml.cs 的公共属性返回对它的引用,或者直接在 XAML 标记中使用 x:FieldModifier attribute

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

【讨论】:

  • 我将 UpdatetextBox 放在我的 Clavier.xaml.cs 或 MainPage.xaml.cs 中?
  • @ValentinDubois-Pivot:在 Clavier.xaml.cs 中。
  • 我有一个错误:MainPage.CallsSection 由于其保护级别而无法访问
  • @ValentinDubois-Pivot:在MainPage.xaml 中将Framex:FieldModifier 设置为public,或者使用代码隐藏中的属性公开Frame。查看我的编辑。
  • 感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多