简体   繁体   English

如何将数据从子视图传递到父视图或在子视图之间传递?

[英]How to pass data from child to parent view or between child views?

Hello I am currently migrating a C# WPF project to Silverlight and I stumbled on a problem also related to my lack of Silverlight knowledge: 您好,我目前正在将C#WPF项目迁移到Silverlight,但偶然发现了一个与我缺乏Silverlight知识有关的问题:

I have my MainPage.xaml which accepts several Views, for example 我有MainPage.xaml,它接受多个视图,例如

I want to pass a String from View1 to View2 programatically. 我想以编程方式将字符串从View1传递到View2。 Depending on the value that is passed from View1 , View2 will change. 根据从View1传递的值,View2将更改。 I don't know how to access/transmit values between views (or atleast a workaround - back to parent, and then to the other child? ) 我不知道如何在视图之间访问/传输值 (或至少有一种解决方法-返回父级,然后再返回另一个子级?)

I have researched this topic for some time now, but I couldn't figure it out. 我已经研究了这个话题一段时间,但我无法弄清楚。 If my question allready has an answer somewhere on forum || 如果我的问题已经在论坛某处找到答案|| my question is poorly written || 我的问题写得不好 my question is stupid || 我的问题很愚蠢|| other cases , I am deeply sorry - don't want to irritate/offend anyone. 其他情况下,我深表歉意-不想激怒/冒犯任何人。

Thank you. 谢谢。

Ok by view, you mean navigation pages. 按查看,您的意思是导航页面。

Using navigation application, there is many ways to do that but I think the best way is using url parameters (just as html parameters) if your parameters are simple string values, then you can retrieve the value from code using 使用导航应用程序,有很多方法可以做到这一点,但是我认为最好的方法是使用url参数(就像html参数一样),如果您的参数是简单的字符串值,则可以使用

this.NavigationContext.QueryString["ProductId"];

for more details check here : http://msdn.microsoft.com/en-us/library/cc838245(VS.95).aspx#Y3804 有关更多详细信息,请参见此处: http : //msdn.microsoft.com/zh-cn/library/cc838245(VS.95).aspx#Y3804

If you need to pass complex types, use a public property in your view. 如果需要传递复杂类型,请在视图中使用公共属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM