【问题标题】:how to set value in text box on button click on other page in uwp?如何在按钮上的文本框中设置值单击uwp中的其他页面?
【发布时间】:2016-05-30 13:19:04
【问题描述】:

在我的页面右侧有一些文本框(第 1 页),在左侧有一些按钮,如文本框上的 1、2、3(第 2 页)让焦点第 2 页出现在框架中。 现在我的问题是如何在按钮单击时在第 1 页的文本框中设置值,在 uwp 的第 2 页。

【问题讨论】:

    标签: xaml c#-4.0 win-universal-app uwp uwp-xaml


    【解决方案1】:

    您可以使用FrameworkElement.FindName method 从页面 2 中获取页面 1 中的 TextBox

    例如这里: MainPage 有两个Frame:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="2*" />
        </Grid.ColumnDefinitions>
        <Frame x:Name="frame1"></Frame>
        <Frame x:Name="frame2" Grid.Column="1"></Frame>
    </Grid>
    

    后面的代码:

    public MainPage()
    {
        this.InitializeComponent();
        this.frame1.Navigate(typeof(MenuPage));
        this.frame2.Navigate(typeof(Page1));
    }
    

    MenuPage 在我的测试中是一个空白页。

    第 1 页:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Tapped="Grid_Tapped">
        <TextBlock Name="textBlock" Text="This is Page 1" VerticalAlignment="Top" FontSize="25" />
        <Button VerticalAlignment="Top" Width="1"></Button>
        <StackPanel VerticalAlignment="Center">
            <TextBox Name="tb1" GotFocus="tb_GotFocus" />
            <TextBox Name="tb2" Margin="0,30" GotFocus="tb_GotFocus" />
            <TextBox Name="tb3" GotFocus="tb_GotFocus" />
        </StackPanel>
    </Grid>
    

    后面的代码:

    public Page1()
    {
        this.InitializeComponent();
        this.Loaded += Page1_Loaded;
    }
    
    private Frame frame;
    
    private void Page1_Loaded(object sender, RoutedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        Page mainPage = rootFrame.Content as MainPage;
        frame = mainPage.FindName("frame1") as Frame;
    }
    
    private void tb_GotFocus(object sender, RoutedEventArgs e)
    {
        frame.Navigate(typeof(Page2));
    }
    
    private void Grid_Tapped(object sender, TappedRoutedEventArgs e)
    {
        if (frame.CanGoBack)
            frame.GoBack();
    }
    

    第 2 页:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock Text="This is Page 2" VerticalAlignment="Top" FontSize="25" />
        <StackPanel VerticalAlignment="Center">
            <Button Content="Button 1" Click="Button_Click_1" />
            <Button Content="Button 2" Click="Button_Click_2" Margin="0,30" />
            <Button Content="Button 3" Click="Button_Click_3" />
        </StackPanel>
    </Grid>
    

    后面的代码:

    public Page2()
    {
        this.InitializeComponent();
        this.Loaded += Page2_Loaded;
    }
    
    private TextBox tb1;
    private TextBox tb2;
    private TextBox tb3;
    
    private void Page2_Loaded(object sender, RoutedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        Page mainPage = rootFrame.Content as MainPage;
        Frame frame = mainPage.FindName("frame2") as Frame;
        Page page1 = frame.Content as Page1;
        tb1 = page1.FindName("tb1") as TextBox;
        tb2 = page1.FindName("tb2") as TextBox;
        tb3 = page1.FindName("tb3") as TextBox;
    }
    
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        tb1.Text = "Button 1 Clicked!";
    }
    
    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        tb2.Text = "Button 2 Clicked!";
    }
    
    private void Button_Click_3(object sender, RoutedEventArgs e)
    {
        tb3.Text = "Button 3 Clicked!";
    }
    

    渲染图像:

    更新: MainPage 调用 Page 1:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button Content="Navigate to Page 1" Click="Button_Click" />
    </Grid>
    

    后面的代码:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.Frame.Navigate(typeof(Page1));
    }
    

    第1页有一个Frame并调用第2页:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="2*" />
        </Grid.ColumnDefinitions>
        <Frame Name="myframe"></Frame>
        <Grid Grid.Column="1">
            <TextBlock Name="textBlock" Text="This is Page 1" VerticalAlignment="Top" FontSize="25" />
            <Button VerticalAlignment="Top" Width="1"></Button>
            <StackPanel VerticalAlignment="Center">
                <TextBox Name="tb1" GotFocus="tb_GotFocus" />
                <TextBox Name="tb2" Margin="0,30" GotFocus="tb_GotFocus" />
                <TextBox Name="tb3" GotFocus="tb_GotFocus" />
            </StackPanel>
        </Grid>
    </Grid>
    

    后面的代码:

    private void tb_GotFocus(object sender, RoutedEventArgs e)
    {
        myframe.Navigate(typeof(Page2));
    }
    

    第 2 页:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock Text="This is Page 2" VerticalAlignment="Top" FontSize="25" />
        <StackPanel VerticalAlignment="Center">
            <Button Content="Button 1" Click="Button_Click_1" />
            <Button Content="Button 2" Click="Button_Click_2" Margin="0,30" />
            <Button Content="Button 3" Click="Button_Click_3" />
        </StackPanel>
    </Grid>
    

    后面的代码有点不同:

    public Page2()
    {
        this.InitializeComponent();
        this.Loaded += Page2_Loaded;
    }
    
    private TextBox tb1;
    private TextBox tb2;
    private TextBox tb3;
    
    private void Page2_Loaded(object sender, RoutedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        Page page1 = rootFrame.Content as Page1;
        tb1 = page1.FindName("tb1") as TextBox;
        tb2 = page1.FindName("tb2") as TextBox;
        tb3 = page1.FindName("tb3") as TextBox;
    }
    
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        tb1.Text = "Button 1 Clicked!";
    }
    
    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        tb2.Text = "Button 2 Clicked!";
    }
    
    private void Button_Click_3(object sender, RoutedEventArgs e)
    {
        tb3.Text = "Button 3 Clicked!";
    }
    

    【讨论】:

    • 我在主页中只有一个框架,它调用了 page1(文本框)。在 page1 我有另一个框架调用 page2 有按钮 1,按钮 2 .plz 告诉我如何用第 2 页代码编写 page1 代码。
    • @TrishitBera,好的,请参阅我答案的更新部分。简单多了,不过关键还是用FindName来获取控制权,只是这里rootFrameContent不一样。
    • Page page1 = rootFrame.Content as Page1;在错误出现“对象引用未设置为对象的实例”之后,page1 显示 null。请告诉是否缺少某些东西。
    • @TrishitBera,你测试了我的样品还是把我的样品换成了你的?是rootFrameContent的问题,可以看到我从MainPage导航到Page1,这两个页面的frame这里都是rootFrame,但是如果你在MainPage中设置了frame控件并导航进去这个框架到第1页,你应该首先找到MainPage,然后找到这个框架,然后找到Page1。
    • 它解决了我的大问题..非常感谢...向您致敬先生.. :)
    【解决方案2】:

    这是我找到的解决方案。它是上述部分的组合。 ShellPage 是一个框架页面。它有一个文本块,此代码位于 app.xaml.cs 页面中。

    ShellPage currentPage = Window.Current.Content as ShellPage;
    TextBlock tb1 = currentPage.FindName("InternetConnection") as TextBlock;
    tb1.Text = InternetConnectionText;
    tb1.Foreground = InternetConnectionForeground;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      • 2013-08-26
      • 1970-01-01
      相关资源
      最近更新 更多