简体   繁体   English

更改不同页面Xamarin的XAML

[英]Change XAML of different Page Xamarin

I have two pages in my Xamarin Forms App, MainPage and Dashboard . 我的Xamarin Forms应用程序中有两个页面, MainPageDashboard I am looking to change the name of a label on my Dashboard page, when I change from my MainPage to my Dashboard. 当我从MainPage更改为Dashboard时,我想更改Dashboard页面上标签的名称。 Changing page is achieved using this function on my MainPage . 可以使用我的MainPage上的此功能来更改页面。

String labelText = "Hello World"; 

public async void openPage(Page page)
    {
        await Navigation.PushAsync(page);

    }

How do I get my labelText to update a <Label/> which I have in my Dashboard.xaml` page? 如何获取我的labelText来更新Dashboard.xaml页面中的<Label/> which I have in my

Use MVVM pattern as it is mostly preferred for xamarin. 使用MVVM模式,因为它是xamarin的首选。

In xaml for Dashboard page, assign binding for label text as follows: 在xaml for Dashboard页面中,为标签文本分配绑定,如下所示:

In the view model declare the property as follows 在视图模型中声明属性,如下所示

private string _labelText="<default string to appear>";
public string LabelText
{
    get { return _labelText; }
    set
    {
        _labelText= value;
        OnPropertyChanged ();
    }
}

Now when you navigation to this Dashboard page, property for LabelText will be assigned with default value you give. 现在,当您导航到该“仪表板”页面时, LabelText属性将分配为您提供的默认值。

If you want to show value passed from page1 as label, pass the value on 如果要显示从page1传递的值作为标签,请将该值传递给

pushAsync(Navigation.PushAsync(new Page2("<string to be passed>"))

and then assign the corresponding value to label in constructor/ OnAppearing method/property directly. 然后将相应的值直接分配给构造函数/ OnAppearing方法/属性中的label。

Like is so often the case there is more than one way to accomplish your goal. 通常情况下,有多种方法可以实现您的目标。 To handle this using the base Xamarin Forms classes you should look at the MessagingCenter . 要使用基本Xamarin Forms类处理此问题,您应该查看MessagingCenter That said to really develop out a Xamarin Forms app, I would suggest you use an MVVM framework like Prism which has been used for years with XAML markup. 也就是说,要真正开发出Xamarin Forms应用程序,我建议您使用像Prism这样的MVVM框架,该框架已经与XAML标记一起使用了多年。

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

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