简体   繁体   English

WinRT应用程序

[英]WinRT Application

I am busy developing a WinRT Application. 我正在忙着开发WinRT应用程序。

I want to access the value of RichEditBox defined in page BasicPage1.xaml into the code behind the page BasicPage2.xaml ie in BasicPage2.xaml.cs ? 我想访问页面BasicPage1.xaml定义的RichEditBox的值到代码的页面背后BasicPage2.xamlBasicPage2.xaml.cs?

Is there anyway to get the value of the RichEditBox(defined in BasicPage1.xaml ) in BasicPage2.xaml.cs ? 有没有在BasicPage2.xaml.cs获取 RichEditBox(在BasicPage1.xaml中定义)的

Thanks in anticipation. 谢谢你的期待。

Are you familiar with MVVM ? 你熟悉MVVM吗? Basically the idea is to not rely to much on the control layer for business data, instead share these information on another layer, in this case the model or view model. 基本上,我们的想法是不要依赖于业务数据的控制层,而是在另一层上共享这些信息,在本例中是模型或视图模型。 So lets say you want to want to load a project and have a dialog with a textbox containing the path to a project, which the user can modify. 因此,假设您想要加载项目并使用包含项目路径的文本框的对话框,用户可以修改该项目。 So you would store the path in a model called ProjectInformation , this object you can now pass to other views (to be more precise, view models and then views) and use the data there. 因此,您将路径存储在名为ProjectInformation的模型中,此对象现在可以传递给其他视图(更准确地说,查看模型,然后查看视图)并在那里使用数据。 The important part here is lifetime, your model propably lives much longer than your view, so the data is stored and reused in the places where its necessary. 这里的重要部分是生命周期,您的模型比您的视图更长寿,因此数据在必要的地方存储和重用。

A simple way to do this is to give your textbox a name in the XAML and then access that textbox via the name in the code behind. 一种简单的方法是在XAML中为文本框指定一个名称,然后通过后面代码中的名称访问该文本框。

<TextBox Name="myTextBox"/>

then in the code behind you can do this 然后在你背后的代码中你可以做到这一点

myTextBox.Text = "blah";

A better way is to use binding so that updating the textbox automatically updates the property you are bound to. 更好的方法是使用绑定,以便更新文本框会自动更新您绑定的属性。 Have a look at this post textbox binding example 看看这篇帖子文本框绑定示例

For a rich edit textbox you should be able to do this: 对于丰富的编辑文本框,您应该能够这样做:

set

myTextBox.Document.SetText(Windows.UI.Text.TextSetOptions.None, "Here is text");

get 得到

  string value = string.Empty;
  myTextBox.Document.GetText(Windows.UI.Text.TextGetOptions.AdjustCrlf, out value);

See this post for more information 有关更多信息,请参阅此帖子

Do you need to send it through when navigating to the other page? 导航到其他页面时是否需要发送? Then you can do it like this: 然后你可以这样做:

this.Frame.Navigate(typeof(BasicPage2),textbox.Text);

and at the BasicPage2.xaml.cs: 在BasicPage2.xaml.cs:

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    var textbox= e.Parameter; 
    ...    
} 

But i also highly recommend using MVVM in your application. 但我也强烈建议在您的应用程序中使用MVVM。 With MVVMLight you can implement this quite easy and quick. 使用MVVMLight,您可以非常轻松快速地实现这一点。

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

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