我正在用C#和Xaml制作一個UWP 桌面應用程式,它是學校專案的銀行應用程式,我正在努力解決付款歷史邏輯。
我希望用戶在Page1上的文本框中輸入一些價格,然后按下按鈕,然后應用程式將導航到主螢屏。如果付款成功(這個邏輯已經在我的應用程式中,你不必關注它)在Page2中將創建文本塊(但用戶將無法看到它,除非他打開Page2 )
第 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>
頁面 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));
}
}
第 2 頁.xaml
<Grid>
<ScrollViewer>
<StackPanel x:Name="RootLayout" Orientation="Vertical" />
</ScrollViewer>
</Grid>
頁面 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);
}
}
主頁
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.
uj5u.com熱心網友回復:
如果沒有人注冊來處理它,那么引發事件就毫無意義。如果我正確理解您的問題,則Page2在您引發事件時沒有創建,Page1因此這將不起作用。
您可以將資訊存盤在某個全域物件中,而不是引發事件,該物件可以隨時隨地從應用程式中的任何位置訪問。
例如,您可以創建一個靜態類:
public static class AppInfo
{
public static string SharedInfo { get; set; }
}
...并從任何頁面獲取和設定它的屬性,例如:
第1頁:
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));
}
第2頁:
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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/355022.html
下一篇:當我在uwp應用程式中使用TextBlock樣式BodyTextBlockStyle和BodyStrongTextBlockStyle時,應用程式崩潰
