【问题标题】:Passing Page1 Text to Page 2 WPF MVVM将第 1 页文本传递到第 2 页 WPF MVVM
【发布时间】:2018-08-23 05:19:12
【问题描述】:

我一直在互联网上寻找明确的答案,但找不到。我过去创建了 Window Forms 应用程序,并决定尝试 WPF。我希望我不必回到 WinForms,但无法找出最简单的任务。

如何将 Page1 的 Textbox 中的文本传递到 Page2 的 TextBox。

如果这是重复的,我深表歉意,但我无法在任何地方找到明确的答案。大多数初学者 mvvm 教程似乎都坚持使用一页应用程序。

MainWindo.xaml

<Window x:Class="WpfApp3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Frame Source="/Page1.xaml"/>
    </Grid>
</Window>

Page1 Xaml:

<Page x:Class="WpfApp3.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:WpfApp3"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Title="Page1"
      xmlns:vm="clr-namespace:WpfApp3">
    <Page.DataContext>
        <vm:Page1ViewModel/>
    </Page.DataContext>
    <Grid>

        <StackPanel>
            <TextBlock Margin="20" FontSize="36">Welcome Home</TextBlock>
            <TextBox x:Name="UserName" Margin="10 0 10 0" Text=""/>
            <Button x:Name="Next" Margin="10 10" Content="Next" Click="Next_Click"/>
        </StackPanel>

    </Grid>
</Page>

Page1 代码隐藏:

using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp3
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
        }
        private void Next_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(
                new Uri("/Page2.xaml", UriKind.Relative));
        }
    }
}

第 1 页视图模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp3
{
    class Page1ViewModel : Notifier
    {
        private string username;
        public string UserName
        {
            get { return username; }
            set
            {
                username = value;
                OnPropertyChanged("UserName");
            }
        }

    }
}

Page2 Xaml:

<Page x:Class="WpfApp3.Page2"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:WpfApp3"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Title="Page2"
      xmlns:vm="clr-namespace:WpfApp3>
      <Page.DataContext>
          <vm:Page2ViewModel/>
      <Page.Data.Context>


    <Grid>
        <StackPanel>
            <TextBlock Margin="10">Hello there: </TextBlock>
            <TextBox x:Name="TextBox_Name" Margin="10" Text="{Binding UserName}"/>
        </StackPanel>
    </Grid>
</Page>

Page2 代码隐藏:

using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp3
{
    /// <summary>
    /// Interaction logic for Page2.xaml
    /// </summary>
    public partial class Page2 : Page
    {
        public Page2()
        {
            InitializeComponent();   
        }
    }
}

Page2 视图模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp3
{
    class Page2ViewModel : Notifier
    {
        private string textbox_name;
        public string TextBox_Name
        {
            get { return textbox_name; }
            set
            {
                textbox_name = value;
                OnPropertyChanged("TextBox_Name");
            }
        }
    }
}

【问题讨论】:

  • 我没有使用多个窗口/页面的经验 我通常只有一个窗口,其中更改的是我的视图,这些视图又被分配给一个视图模型,所以当我需要通过时从一个视图到另一个视图我通过将接收和处理数据的视图模型的构造函数发送它,我认为这称为依赖注入
  • 和我这里的一样吗?我有一个从 Page1 开始的窗口,然后是一个导航到 Page2 的按钮。
  • 您应该正确实现导航服务,我建议您使用 mvvm 库(mvvmlight 是最简单的库之一),然后在这里查看答案stackoverflow.com/questions/28966819/…

标签: c# wpf xaml mvvm


【解决方案1】:

首先,您不需要回到 WinForms:您可以完全避免 MVVM,并使用您知道的传统代码隐藏编码模型,即使是 WPF。如果您对 MVVM 不放心,我建议您为您的第一个 WPF 应用程序这样做。

至于您的问题,解决方案可能是使用静态属性来存储常用值。

public class MyCommonValues
{
  public static string SharedText { get; set; }
}

class Page1ViewModel : Notifier
{
    private string username;
    public string UserName
    {
        get { return username; }
        set
        {
            username = value;
            OnPropertyChanged("UserName");
            OnUserNameChanged(); // added to your code
        }
    }

    // following is an addition to your code
    void OnUserNameChanged()
    {
      MyCommonValues.SharedText = UserName;
    }
}

class Page2ViewModel : Notifier
{
    // following is an addition to your code
    public Page2ViewModel()
    {
      TextBox_Name = MyCommonValues.SharedText;
    }

    private string textbox_name;
    public string TextBox_Name
    {
        get { return textbox_name; }
        set
        {
            textbox_name = value;
            OnPropertyChanged("TextBox_Name");
        }
    }
}

【讨论】:

  • 非常感谢,共享值是秘诀,现在如何将 UI 值绑定到 VM 变量变得更有意义,反之亦然。
  • 这需要更多的练习,但你很快就会到达那里!
猜你喜欢
  • 1970-01-01
  • 2014-03-22
  • 1970-01-01
  • 2021-03-24
  • 1970-01-01
  • 2020-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多