I am making an UWP desktop application with C# & Xaml it is banking application for school project and I am struggling with payment history logic.
I want from user to enter some price into the text box which is on Page1 , than he will press the button, after that the app will navigate to the main screen. If the payment was successful (this logic is already in my app and you don't have to focuse on that) in the Page2 the text block will be created (but the user won't be able to see it unless he opens the Page2)
Page 1.xaml
<Grid>
<StackPanel Orientation="Vertical" VerticalAlignment="Top">
<TextBox x:Name="Money" Height="100" Header="Enter your money"/>
<Button
x:Name="MyBtn"
Click="Button_Click"
Content="Ok"
Margin="30"/>
</StackPanel>
</Grid>
Page 1.xaml.cs
public partial class Page1 : Page
{
public Page1()
{
this.InitializeComponent();
}
public delegate void PaymentEventHandler(object sender, EventArgs args);
public event PaymentEventHandler SuccessfullPayment;
protected virtual void OnSuccessfullPayment()
{
if (SuccessfullPayment != null)
{
SuccessfullPayment(this, EventArgs.Empty);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//in case that payment is successful (this logic is unnecesary in this example)
OnSuccessfullPayment();
this.Frame.Navigate(typeof(MainBankPage));
}
}
Page 2.xaml
<Grid>
<ScrollViewer>
<StackPanel x:Name="RootLayout" Orientation="Vertical" />
</ScrollViewer>
</Grid>
Page 2.xaml.cs
public sealed partial class Page2 : Page
{
public Page2()
{
this.InitializeComponent();
}
public void Initialization()
{
var payment= new Page1();
payment.SuccessfullPayment = CreateTextBlock;
}
public void CreateTextBlock(object sender, EventArgs e)
{
var textblock = new TextBlock();
textblock.Margin = new Thickness(0, 20, 15, 0);
textblock.Width = 400;
textblock.Height = 60;
textblock.FontSize = 20;
textblock.HorizontalAlignment = HorizontalAlignment.Left;
textblock.Text = "Payment was successfull";
RootLayout.Children.Add(textblock);
}
}
Main page
There are only 6 buttons which user can interact with to navigate between pages in my app so I think it is unnecessary to show code
I was trying to do it through custom events (it is shown in the example), but it didn't work.
I am kinda new in app development so if this is stupid way to do it I am open for other options
Note: my app is much more complex I just want to know how to do this specific thing so I have created this simple example :)
Hope everything is clear to you if not comment and I will try to improve the question as much as I can.
Thank you so much for your time and answers.
CodePudding user response:
It's no point of raising an event if no one has registered to handle it. If I understand your issue correctly, there is no Page2
created by the time you raise the event from Page1
so this won't work.
Instead of raising an event, you could store the information in some global object that can be accessed from anywhere in the application whenever.
You could for example create a static class:
public static class AppInfo
{
public static string SharedInfo { get; set; }
}
...and get and set properties of it from any page, e.g.:
Page1:
private void Button_Click(object sender, RoutedEventArgs e)
{
//in case that payment is successful (this logic is unnecesary in this example)
AppInfo.SharedInfo = "Payment was successfull";
OnSuccessfullPayment();
this.Frame.Navigate(typeof(MainBankPage));
}
Page2:
public void Initialization()
{
var textblock = new TextBlock();
textblock.Margin = new Thickness(0, 20, 15, 0);
textblock.Width = 400;
textblock.Height = 60;
textblock.FontSize = 20;
textblock.HorizontalAlignment = HorizontalAlignment.Left;
textblock.Text = AppInfo.SharedInfo;
RootLayout.Children.Add(textblock);
}