简体   繁体   English

UWP C# MVVM 如何从其他页面访问 ViewModel

[英]UWP C# MVVM How To Access ViewModel from Other Page

I am tying to further understand MVVM with some example scenario.我想通过一些示例场景进一步了解 MVVM。 I have a rootpage with a 'maindisplay' textblock .我有一个带有 ' rootpage ' textblock的根页面。 I would like to display 'status' or 'scenarios' from activation of any form of UI eg.我想通过激活任何形式的 UI 来显示“状态”或“场景”,例如。 togglebutton on the 'maindisplay' textblock . ' togglebutton ' textblock上的切换按钮。

I am able to bind the the page navigation info in the rootpageviewmodel to the textblock .我能够将textblock中的页面导航信息绑定到rootpageviewmodel However, I am not able to achieve the result when displaying info from different page.但是,当显示来自不同页面的信息时,我无法获得结果。 I have checked another post multiple-viewmodels-in-same-view & Accessing a property in one ViewModel from another it's quite similar but it didn't work.我检查了另一篇文章multiple-viewmodels-in-same-view & Accessing a property in a ViewModel from another它非常相似,但它不起作用。

Please help.请帮忙。 Thanks.谢谢。

While accessing the RootPageViewModel should retain the instance?在访问RootPageViewModel应该保留实例吗?

View看法

<TextBlock Text="{x:Bind RootViewModel.MainStatusContent, Mode=OneWay}"/>

RootPage.xaml.cs RootPage.xaml.cs

        public sealed partial class RootPage : Page
        {
            private static RootPage instance;
            public RootPageViewModel RootViewModel { get; set; }
    
            public RootPage()
            {
                RootViewModel = new RootPageViewModel();
    
                this.InitializeComponent();
    
                // Always use the cached page
                this.NavigationCacheMode = NavigationCacheMode.Required;
            }
    
            public static RootPage Instance
            {
                get
                {
                    if (instance == null)
                    {
                        instance = new RootPage();
                    }
                    return instance;
                }
            }
         private void nvTopLevelNav_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
            {
                if (args.IsSettingsInvoked)
                {
                    contentFrame.Navigate(typeof(SettingsPage));
    
                    RootViewModel.MainStatusContent = "Settings_Page";
                }
                else
                { 
                    var navItemTag = args.InvokedItemContainer.Tag.ToString();        
                    
                    RootViewModel.MainStatusContent = navItemTag;
                                     
                    switch (navItemTag)
                    {
                        case "Home_Page":   
                            contentFrame.Navigate(typeof(HomePage));
                            break;
    
                        case "Message_Page":  
                            contentFrame.Navigate(typeof(MessagePage));
                            break;
                    }
                    
                }
             }
        }
        

RootPage ViewModel : RootPage 视图模型

public class RootPageViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private static RootPageViewModel instance = new RootPageViewModel();
    public static RootPageViewModel Instance
    {
        get
        {
            if (instance == null)
                instance = new RootPageViewModel();

            return instance;
        }
    }

    public RootPageViewModel()
    {
    }

    private string _mainStatusContent;
    public string MainStatusContent
    {
        get
        {
            return _mainStatusContent;
        }
        set
        {
            _mainStatusContent = value;
            OnPropertyChanged();
        }
    }

    protected void OnPropertyChanged([CallerMemberName] string name = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

MessagePage.xaml.cs - to access RootPage ViewModel MessagePage.xaml.cs - 访问 RootPage ViewModel

public sealed partial class MessagePage : Page
{
    public MessagePageViewModel MessageViewModel { get; set; }

    public MessagePage()
    {
        MessageViewModel = new MessagePageViewModel();

        this.InitializeComponent();

        // Always use the cached page
        this.NavigationCacheMode = NavigationCacheMode.Required;
    }

    private void Message1_Checked(object sender, RoutedEventArgs e)
    {
        RootPageViewModel.Instance.MainStatusContent = "Message 1 Selected";
    }

    private void Message1_Unchecked(object sender, RoutedEventArgs e)
    {
        RootPageViewModel.Instance.MainStatusContent = "Message 1 De-Selected";
    }
}

When I debug the value did write to the instance but did't update the TextBlock .当我调试该值确实写入实例但没有更新TextBlock Did I do anything wrong in my XAML binding?我在XAML绑定中做错了吗? 在此处输入图像描述 在此处输入图像描述

UWP C# MVVM How To Access ViewModel from Other Page UWP C# MVVM 如何从其他页面访问 ViewModel

The better way is make static variable for RootPage, but not make singleton instance for RootPage and RootPageViewModel .更好的方法是为 RootPage 制作 static 变量,但不要为RootPageRootPageViewModel制作 singleton 实例。

For example:例如:

public RootPage ()
{
    this.InitializeComponent();
    this.NavigationCacheMode = NavigationCacheMode.Required;
    Instance = this;
    RootViewModel = new RootPageViewModel();
}

public static RootPage Instance;

Usage用法

private void Message1_Checked(object sender, RoutedEventArgs e)
{

    RootPage.Instance.RootViewModel.MainStatusContent = "Message 1 Selected";
}

private void Message1_Unchecked(object sender, RoutedEventArgs e)
{
    RootPage.Instance.RootViewModel.MainStatusContent = "Message 1 De-Selected";
}

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

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