我在不同视图之间进行简单的变量共享时遇到了一些问题。
我有第一个名为 MainPage.xaml 的主视图,第二个名为 Page2.xaml 。
我想检查选中 MainPage.xaml 上的哪个radiobutton,并将带有该日期的变量发送到 Page2.xaml 。
的MainPage:
namespace IDG
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public string choice;
public MainPage()
{
this.InitializeComponent();
}
private void bt_start_Click(object sender, RoutedEventArgs e)
{
if (rb_choice1.IsChecked == true)
{
choice = "choice1";
}
if (rb_quiz.IsChecked == true)
{
this.Frame.Navigate(typeof(Page2), choice);
}
}
}
}
和Page2:
namespace IDG
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class Page2 : Page
{
private string x;
public Page2()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var param = e.Parameter as string;
x = param;
textBlock1.Text = x;
}
private void button_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(MainPage));
}
}
}
我希望这个参数存储在主类中,怎么做?
答案 0 :(得分:0)
在RewriteEngine on
RewriteCond %{REQUEST_URI} !^/home/
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*) /home/$1 [L,R=302]
事件的第2页中,检索如下值:OnNavigatedTo
修改强>
将retreived参数指定给OnNavigatedTo中的文本块。在构造页面时,x的值是&#34;&#34;。
var param = e.Parameter as string
答案 1 :(得分:0)
最近,我正在研究WPF项目,但我们将它与DevExpress库一起使用,对于您的问题,在DevExpress中使用Messenger很容易修复。
我们只是注册您想要接收数据的信使,
public class Message {
//...
}
public class Recipient {
public Recipient() {
Messenger.Default.Register<string>(this, OnMessage1);
Messenger.Default.Register<Message>(this, OnMessage2);
}
void SendMessages() {
Messenger.Default.Send("test");
Messenger.Default.Send(new Message());
}
void OnMessage1(string message) {
//...
}
void OnMessage2(Message message) {
//...
}
}
然后你可以从另一个视图发送它,
public class InheritedMessage : Message {
//...
}
public class Recipient {
public Recipient() {
//Inherited messages are not processed with this subscription
Messenger.Default.Register<Message>(
recipient: this,
action: OnMessage);
//Inherited messages are processed with this subscription
Messenger.Default.Register<Message>(
recipient: this,
receiveInheritedMessagesToo: true,
action: OnMessage);
}
void SendMessages() {
Messenger.Default.Send(new Message());
Messenger.Default.Send(new InheritedMessage());
}
void OnMessage(Message message) {
//...
}
}
有了它,你可以在模块之间传递数据(或视图,但建议使用MVVM) 如果您想了解有关DevExpress的更多信息,请访问https://documentation.devexpress.com/#WPF/CustomDocument17474
希望它可以帮到你。 :)