将自定义事件从一页发送到另一页
Sending custom event form one page to another
我正在使用 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);
}
}
主页
在我的应用程序中只有 6 个按钮可供用户交互以在页面之间导航,因此我认为没有必要显示代码
我试图通过自定义事件来实现(如示例所示),但没有成功。
我是应用程序开发的新手,所以如果这是愚蠢的做法,我愿意接受其他选择
注意:我的应用要复杂得多,我只是想知道如何做这个具体的事情,所以我创建了这个简单的例子:)
如果不发表评论,希望您已经清楚了,我会尽力改进问题。
非常感谢您的时间和答复。
如果没有人注册处理事件,则引发事件毫无意义。如果我对你的问题的理解正确,那么在你从 Page1
引发事件时没有创建 Page2
,所以这不会起作用。
您可以将信息存储在某个全局对象中,以便随时从应用程序中的任何位置访问,而不是引发事件。
例如,您可以创建静态 class:
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);
}
我正在使用 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);
}
}
主页
在我的应用程序中只有 6 个按钮可供用户交互以在页面之间导航,因此我认为没有必要显示代码
我试图通过自定义事件来实现(如示例所示),但没有成功。
我是应用程序开发的新手,所以如果这是愚蠢的做法,我愿意接受其他选择
注意:我的应用要复杂得多,我只是想知道如何做这个具体的事情,所以我创建了这个简单的例子:)
如果不发表评论,希望您已经清楚了,我会尽力改进问题。
非常感谢您的时间和答复。
如果没有人注册处理事件,则引发事件毫无意义。如果我对你的问题的理解正确,那么在你从 Page1
引发事件时没有创建 Page2
,所以这不会起作用。
您可以将信息存储在某个全局对象中,以便随时从应用程序中的任何位置访问,而不是引发事件。
例如,您可以创建静态 class:
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);
}