Good day!
Most recently, I switched from console programming to forms, or rather trying to write an application on Windows Phone 8. Different nonsense is obtained without difficulty, but if something is more complicated ... In general, that is the question:

There is a form Page1.xaml.cs, on which the elements are located: 4 TextBox and Button1. I decided to add a new Car.cs class to the project with the following content:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PhoneApp1 { public class Car { public string marka; public string model; public int maxspeed; public int horsepower; } } 

In Page1.xaml.cs, I create an instance of the class:

 Car auto = new Car(); 

And I begin to assign through the usual point "." class fields data from textboxes. There are no problems.

Next, I create a new form / class Page2.xaml.cs, where I have 4 elements of the TextBlock type and the Button2 button. So, the question is: how do I get access to the auto variable, or rather to its fields in the new form? (I want to fill TextBlock'and data from auto when I click on Button2).

Content Page1:

 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Navigation; using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; namespace PhoneApp1 { public partial class Page1 : PhoneApplicationPage { public Page1() { InitializeComponent(); } public void Button_Click(object sender, RoutedEventArgs e) { Car auto = new Car(); auto.marka = TextBox_marka.Text; auto.model = TextBox_model.Text; auto.maxspeed = Convert.ToInt32(TextBox_maxspeed.Text); auto.horsepower = Convert.ToInt32(TextBox_horsepower.Text); MessageBox.Show("Информация добавлена успешна!"); } } } 

Content Page2:

 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Navigation; using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; namespace PhoneApp1 { public partial class Page2 : PhoneApplicationPage { public ShowRandomCar() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { *Код кнопки* } } } 

I just want to say that I am just learning, so please do not shout at me about curved-implemented encapsulation, etc. I just want to understand how to properly establish a connection between the forms.

  • The simplest thing is to make a Car property on page2 and pass auto there after opening the page. static class, after all, can be used - Veikedo
  • 2
    Usually, Gui and Logic are shared, i.e. by pressing a button you change the context somewhere. Select a class separately (you can even singelton) of the context and communicate with it from anywhere. - JEcho
  • + Read about IoC containers - Veikedo

1 answer 1

Do wrong.

First, WPF is not Winforms. Therefore, you need to make a Car ViewModel and forget about retrograde data transfer manually, and use the Binding 'om.

So Car class:

 public class Car : DependencyObject { public static readonly DependencyProperty ManufacturerProperty = DependencyProperty.Register("Manufacturer", typeof(string), typeof(Car)); public string Manufacturer { get { return (string)GetValue(ManufacturerProperty); } set { SetValue(ManufacturerProperty, value); } } public static readonly DependencyProperty ModelProperty = DependencyProperty.Register("Model", typeof(string), typeof(Car)); public string Model { get { return (string)GetValue(ModelProperty); } set { SetValue(Property, value); } } // ну и т. д. public Model.Car Create() { return new Model.Car(Manufacturer, Model, ...); } static Car FromModel(Car.Model car) { return new Car() { Manufacturer = car.Manufacturer, ... }; } } 

Now your properties can participate in Binding 'e, and it's time to take advantage of this.

 <PhoneApplicationPage x:Class="PhoneApp1.Page1" ... > ... <TextBlock Text="{Binding Manufacturer}"/> ... <Button Command="New">Add new car</Button> ... </PhoneApplicationPage> 

Do not forget to specify the desired DataContext when launching Page1 :

 car = new Car(); new Page1() { DataContext = car }.Show(); 

You have a team that needs to be processed. Register the handler at the beginning of the program execution:

 CommandManager.RegisterClassCommandBinding( typeof(PhoneApplicationPage), new CommandBinding(ApplicationCommands.New, AddCar, CanAddCar)); void AddCar(object sender, ExecutedRoutedEventArgs e) { model.Cars.Add(car.Create()); } 

Well, now it’s clear how to display another window corresponding to this model car:

 new Page2() { DataContext = Car.FromModel(modelCar) }.Show(); 

Read about MVVM, nowhere without it.

  • Just do not understand what you mean wpf? - jimpanzer
  • Oh, yes, the file name of Page1.xaml.cs inspired. And under WP8 is not the same? - VladD