简体   繁体   English

如何使用MVVM从其他xaml页面进行属性更改?

[英]How can I make a property change from a different xaml Page with the use of MVVM?

I'm newbie in MVVM . 我是MVVM的新手。 I have two xaml pages. 我有两个xaml页面。 the second one cannot be accessed ( Locked ) unless the button from the first page (Introduction page) has been clicked. 除非已单击第一页(“简介”页)中的按钮,否则无法访问第二个(“锁定”)。 How can I do that? 我怎样才能做到这一点?

The lock page has this code. 锁定页面具有此代码。

<Frame Grid.Row="0" Grid.Column="1" BackgroundColor="LightGray" IsVisible="{Binding LockPage}">
                <Frame.GestureRecognizers>
                    <TapGestureRecognizer Tapped="Tap_Lock" />
                </Frame.GestureRecognizers>
                <Label Text="Locked"/>
            </Frame>

The Introduction page has this code. “简介”页面包含此代码。

<StackLayout>
        <Label Text="This is only a simple Introduction Text."/>
        <Label Text=""/>
        <Button Text="Lets Go!!" Command="{Binding UnlockPageCommand}" Clicked="Tap_Next"/>
</StackLayout>

This is the the class LockModule.cs 这是类LockModule.cs

 public class LockModule : INotifyPropertyChanged
{

    public LockModule()
    {
        UnlockPageCommand = new Command(UnlockPage);
    }

    bool lockPage = true;
    public event PropertyChangedEventHandler PropertyChanged;

    void OnPropertyChanged(string lockpage)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(lockpage));
    }

    public bool LockPage
    {
        get { return lockPage; }
        set
        {
                lockPage = value;

            OnPropertyChanged(nameof(LockPage));
        }
    }

    public Command UnlockPageCommand { get; }

    void UnlockPage()
    {
        if (lockPage == true)
        {
            lockPage = false;
        }
        else
        {
            lockPage = true;
        }
        OnPropertyChanged(nameof(LockPage));
    }
}

and it's not working...... 而且不起作用......

I'm assuming both of your pages are bound to the same instance of your LockModule ViewModel, which at least the first misstep I see in your provided code is the UnlockPage() method isn't setting the correct variable. 我假设您的两个页面都绑定到您的LockModule ViewModel的相同实例,至少我在您提供的代码中看到的第一个错误是UnlockPage()方法未设置正确的变量。 It would prevent OnPropertyChanged from firing and keep the bindings from updating automatically. 这将阻止OnPropertyChanged触发,并使绑定保持自动更新。 Your method for your command should at least be something more like this: 您的命令方法至少应该是这样的:

void UnlockPage()
{
    if (lockPage == true)
    {
        LockPage = false;
    }
    else
    {
        LockPage = true;
    }
    OnPropertyChanged(nameof(LockPage));
}

One way of communicating between 2 viewmodels would be using the same instance of same object across viewmodels. 在两个视图模型之间进行通信的一种方式是跨视图模型使用相同对象的相同实例。

Else have your LockPage property in viewmodels base. 否则在viewmodels库中具有LockPage属性。

However I would suggest you to have such values stored in app's shared preferences namely app setting. 但是我建议您将这样的值存储在应用程序的共享首选项即应用程序设置中。 You can use this plugin to make your life simpler around app settings. 您可以使用此插件来简化应用设置的生活。 Ref Link: https://github.com/jamesmontemagno/SettingsPlugin/blob/master/README.md 参考链接: https : //github.com/jamesmontemagno/SettingsPlugin/blob/master/README.md

Last option would be to consider using messageing centre to communicate between pages and pass values to viewmodels. 最后一个选择是考虑使用消息传递中心在页面之间进行通信并将值传递给视图模型。 Ref: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center 参考: https : //docs.microsoft.com/zh-cn/xamarin/xamarin-forms/app-fundamentals/messaging-center

However the easiest and finest would be, update the value of lock on page one and pass it via navigation parameters to page two. 但是,最简单,最有效的方法是,更新第一页上的lock的值,然后通过导航参数将其传递给第二页。

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

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