将Page1文本传递给WPF MVVM

时间:2018-03-14 15:31:59

标签: c# wpf xaml mvvm

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

如何将Page1的文本框中的文本传递给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 Code-Behind:

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));
        }
    }
}

Page1 ViewModel:

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();   
        }
    }
}

第2页查看模型:

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");
            }
        }
    }
}

1 个答案:

答案 0 :(得分: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");
        }
    }
}